Skip to content

Commit

Permalink
EventEmitter: add off without arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcard committed Feb 8, 2016
1 parent 9a7ad19 commit cacb6eb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 24 deletions.
7 changes: 6 additions & 1 deletion ably-ios/ARTEventEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ART_ASSUME_NONNULL_BEGIN

- (void)off:(EventType)event listener:(__GENERIC(ARTEventListener, ItemType) *)listener;
- (void)off:(__GENERIC(ARTEventListener, ItemType) *)listener;
- (void)off;

- (void)emit:(EventType)event with:(ItemType)data;

Expand All @@ -45,7 +46,8 @@ ART_ASSUME_NONNULL_BEGIN
- (__GENERIC(ARTEventListener, ItemType) *)once:(void (^)(ItemType __art_nullable))cb;\
\
- (void)off:(EventType)event listener:(__GENERIC(ARTEventListener, ItemType) *)listener;\
- (void)off:(__GENERIC(ARTEventListener, ItemType) *)listener;
- (void)off:(__GENERIC(ARTEventListener, ItemType) *)listener;\
- (void)off;

// This macro adds methods to a class implementation that just bridge calls to an internal
// instance variable, which must be called _eventEmitter, of type ARTEventEmitter *.
Expand Down Expand Up @@ -74,6 +76,9 @@ return [_eventEmitter once:cb];\
- (void)off:(__GENERIC(ARTEventListener, ItemType) *)listener {\
[_eventEmitter off:listener];\
}\
- (void)off {\
[_eventEmitter off];\
}\
\
- (void)emit:(EventType)event with:(ItemType)data {\
[_eventEmitter emit:event with:data];\
Expand Down
12 changes: 10 additions & 2 deletions ably-ios/ARTEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ @implementation ARTEventEmitter
- (instancetype)init {
self = [super init];
if (self) {
_listeners = [[NSMutableDictionary alloc] init];
_anyListeners = [[NSMutableArray alloc] init];
[self resetListeners];
}
return self;
}
Expand Down Expand Up @@ -130,6 +129,15 @@ - (void)off:(ARTEventListener *)listener {
}
}

- (void)off {
[self resetListeners];
}

- (void)resetListeners {
_listeners = [[NSMutableDictionary alloc] init];
_anyListeners = [[NSMutableArray alloc] init];
}

- (void)emit:(id)event with:(id)data {
NSMutableArray *listenersForEvent = [self.listeners objectForKey:event];
NSMutableArray *toCall = [[NSMutableArray alloc] init];
Expand Down
79 changes: 58 additions & 21 deletions ablySpec/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,67 @@ class Utilities: QuickSpec {
expect(receivedAllOnce).to(equal(123))
}

it("should stop receiving events when calling off") {
eventEmitter.off(listenerFoo1!)
eventEmitter.emit("foo", with: 123)

expect(receivedFoo1).to(beNil())
expect(receivedFoo2).to(equal(123))
expect(receivedAll).to(equal(123))

eventEmitter.emit("bar", with: 222)

expect(receivedFoo2).to(equal(123))
expect(receivedAll).to(equal(222))

eventEmitter.off(listenerAll!)
eventEmitter.emit("bar", with: 333)

expect(receivedAll).to(equal(222))
context("calling off with a single listener argument") {
it("should stop receiving events when calling off with a single listener argument") {
eventEmitter.off(listenerFoo1!)
eventEmitter.emit("foo", with: 123)

expect(receivedFoo1).to(beNil())
expect(receivedFoo2).to(equal(123))
expect(receivedAll).to(equal(123))

eventEmitter.emit("bar", with: 222)

expect(receivedFoo2).to(equal(123))
expect(receivedAll).to(equal(222))

eventEmitter.off(listenerAll!)
eventEmitter.emit("bar", with: 333)

expect(receivedAll).to(equal(222))
}
}

context("calling off with listener and event arguments") {
it("should still receive events if off doesn't match the listener's criteria") {
eventEmitter.off("foo", listener: listenerAll!)
eventEmitter.emit("foo", with: 111)

it("should receive events if off doesn't match the listener's criteria") {
eventEmitter.off("foo", listener: listenerAll!)
eventEmitter.emit("foo", with: 111)
expect(receivedFoo1).to(equal(111))
expect(receivedAll).to(equal(111))
}

it("should stop receive events if off matches the listener's criteria") {
eventEmitter.off("foo", listener: listenerFoo1!)
eventEmitter.emit("foo", with: 111)

expect(receivedAll).to(equal(111))
expect(receivedFoo1).to(beNil())
expect(receivedAll).to(equal(111))
}
}

context("calling off with no arguments") {
it("should remove all listeners") {
eventEmitter.off()
eventEmitter.emit("foo", with: 111)

expect(receivedFoo1).to(beNil())
expect(receivedFoo2).to(beNil())
expect(receivedAll).to(beNil())

eventEmitter.emit("bar", with: 111)

expect(receivedBar).to(beNil())
expect(receivedBarOnce).to(beNil())
expect(receivedAll).to(beNil())
}

it("should allow listening again") {
eventEmitter.off()
eventEmitter.on("foo", call: { receivedFoo1 = $0 as? Int })
eventEmitter.emit("foo", with: 111)
expect(receivedFoo1).to(equal(111))
}
}
}
}
Expand Down

0 comments on commit cacb6eb

Please sign in to comment.