Skip to content

Commit

Permalink
add removeAllListeners API (#16)
Browse files Browse the repository at this point in the history
* add removeAllListeners and eventNames API
  • Loading branch information
jbeverly authored Sep 10, 2018
1 parent 68ef2cb commit 3d1c42a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
29 changes: 29 additions & 0 deletions eventemitter_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,35 @@ class EventEmitter {
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
Expand Down
38 changes: 38 additions & 0 deletions test/cpp/eventemitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class EmittingThing : public Nan::ObjectWrap {
Nan::SetPrototypeMethod(constructor, "on", On);
Nan::SetPrototypeMethod(constructor, "run", Run);
Nan::SetPrototypeMethod(constructor, "runReentrant", RunReentrant);
Nan::SetPrototypeMethod(constructor, "removeAllListeners", RemoveAllListeners);
Nan::SetPrototypeMethod(constructor, "eventNames", EventNames);

Nan::Set(target, clsName, Nan::GetFunction(constructor).ToLocalChecked());
};
Expand Down Expand Up @@ -125,6 +127,42 @@ class EmittingThing : public Nan::ObjectWrap {
Nan::AsyncQueueWorker(worker);
}

static NAN_METHOD(RemoveAllListeners) {
if (info.Length() > 1) {
info.GetIsolate()->ThrowException(Nan::TypeError("Wrong number of arguments"));
return;
}
auto thing = Nan::ObjectWrap::Unwrap<EmittingThing>(info.Holder());

if (info.Length() == 1) {
if (!info[0]->IsString()) {
info.GetIsolate()->ThrowException(Nan::TypeError("First argument must be string name of an event"));
return;
}
auto ev = std::string(*v8::String::Utf8Value(info[0]->ToString()));
thing->emitter_->removeAllListenersForEvent(ev);
} else {
thing->emitter_->removeAllListeners();
}
}

static NAN_METHOD(EventNames) {
if (info.Length() > 0) {
info.GetIsolate()->ThrowException(Nan::TypeError("Wrong number of arguments"));
return;
}
auto thing = Nan::ObjectWrap::Unwrap<EmittingThing>(info.Holder());
v8::Local<v8::Array> v = v8::Array::New(info.GetIsolate());

size_t i = 0;
for (auto s : thing->emitter_->eventNames()) {
v->Set(i++, v8::String::NewFromUtf8(info.GetIsolate(), s.c_str()));
}

info.GetReturnValue().Set(v);
}


static NAN_METHOD(RunReentrant) {
Nan::Callback* fn(nullptr);
if (info.Length() < 1 || info.Length() > 2) {
Expand Down
21 changes: 21 additions & 0 deletions test/js/eventemitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ describe('Verify EventEmitter', function() {

thing.run(n)
})

it('should remove listeners for a specific test event', function(done) {
let thing = new bindings.EmitterThing()
thing.on('test1', function(ev) { })
thing.on('test2', function(ev) { })

expect(thing.eventNames()).to.be.equal(['test2', 'test1'])
thing.removeAllListeners('test1')
expect(thing.eventNames()).to.be.equal(['test2'])
done()
})

it('should remove all listeners for all events', function(done) {
let thing = new bindings.EmitterThing()
thing.on('test1', function(ev) { })
thing.on('test2', function(ev) { })

thing.removeAllListeners()
expect(thing.eventNames()).to.be.equal([])
done()
})
})

describe('Verify EventEmitter Multi', function() {
Expand Down

0 comments on commit 3d1c42a

Please sign in to comment.