-
Notifications
You must be signed in to change notification settings - Fork 4
/
eventemitter_impl.hpp
175 lines (148 loc) · 5.86 KB
/
eventemitter_impl.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright 2017 Scoop Technologies
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#ifndef _NODE_EVENT_EVENTEMITTER_IMPL_H
#define _NODE_EVENT_EVENTEMITTER_IMPL_H
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <nan.h>
#include <uv.h>
#include "uv_rwlock_adaptor.hpp"
#include "shared_lock.hpp"
#include "shared_ringbuffer.hpp"
namespace NodeEvent {
#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif
/// A type for implementing things that behave like EventEmitter in node
class EventEmitter {
public:
/// A report type, consisting of a key and a value
typedef std::pair<std::string, std::string> ProgressReport;
/// An error indicating the event name is not known
class InvalidEvent : std::runtime_error {
public:
explicit InvalidEvent(const std::string& msg) : std::runtime_error(msg) {}
};
EventEmitter() : receivers_lock_(), receivers_() {}
virtual ~EventEmitter() noexcept = default;
/// Set a callback for a given event name
///
/// @param[in] ev - event name
/// @param[in] cb - callback
virtual void on(const std::string& ev, Nan::Callback* cb) {
std::unique_lock<uv_rwlock> master_lock{receivers_lock_};
auto it = receivers_.find(ev);
if (it == receivers_.end()) {
receivers_.emplace(ev, std::make_shared<ReceiverList>());
it = receivers_.find(ev);
}
master_lock.unlock();
it->second->emplace_back(cb);
}
/// Remove all listeners for a given event
///
/// @param[in] ev - event name
virtual void removeAllListenersForEvent(const std::string& ev) {
std::unique_lock<uv_rwlock> master_lock{receivers_lock_};
auto it = receivers_.find(ev);
if (it != receivers_.end()) {
receivers_.erase(it);
}
}
/// Remove all listeners for all events
virtual void removeAllListeners() {
std::unique_lock<uv_rwlock> master_lock{receivers_lock_};
receivers_.clear();
}
// Return a list of all eventNames
virtual std::vector<std::string> eventNames() {
std::unique_lock<uv_rwlock> master_lock{receivers_lock_};
std::vector<std::string> keys;
for (auto it : receivers_) {
keys.emplace_back(it.first);
}
return keys;
}
/// Emit a value to any registered callbacks for the event
///
/// @param[in] ev - event name
/// @param[in] value - a string to emit
///
/// @returns true if the event has listeners, false otherwise
virtual bool emit(const std::string& ev, const std::string& value) const {
shared_lock<uv_rwlock> master_lock{receivers_lock_};
auto it = receivers_.find(ev);
if (it == receivers_.end()) {
return false;
}
master_lock.unlock();
it->second->emit(value);
return true;
}
private:
/// Receiver represents a callback that will receive events that are fired
class Receiver {
public:
/// @param[in] callback - the callback to fire, should take a single argument (which will be a string)
explicit Receiver(Nan::Callback* callback) : callback_(callback) {}
/// notify the callback by building an AsyncWorker and scheduling it via Nan::AsyncQueueWorker()
///
/// @param[in] value - the string value to send to the callback
void notify(const std::string& value) const {
v8::Local<v8::Value> info[] = {Nan::New<v8::String>(value).ToLocalChecked()};
callback_->Call(1, info);
}
~Receiver() {
callback_->Reset();
delete callback_;
}
private:
Nan::Callback* callback_;
};
/// ReceiverList is a list of receivers. Access to the list is proteced via shared mutex
class ReceiverList {
public:
ReceiverList() : receivers_list_(), receivers_list_lock_() {}
/// Adds a callback to the end of the receivers_list
///
/// @param[in] cb - the callback to add to the list
void emplace_back(Nan::Callback* cb) {
std::lock_guard<uv_rwlock> guard{receivers_list_lock_};
receivers_list_.emplace_back(std::make_shared<Receiver>(cb));
}
/// notify all receivers
///
/// @param[in] value - the string to send to all receivers
void emit(const std::string& value) const {
shared_lock<uv_rwlock> guard{receivers_list_lock_};
for (auto& receiver : receivers_list_) {
receiver->notify(value);
}
}
private:
std::vector<std::shared_ptr<Receiver>> receivers_list_;
mutable uv_rwlock receivers_list_lock_;
};
mutable uv_rwlock receivers_lock_;
std::unordered_map<std::string, std::shared_ptr<ReceiverList>> receivers_;
};
} // namespace NodeEvent
#endif