Skip to content

Commit

Permalink
fix(EventTargetInterruptSource): subscribing to multiple events separ…
Browse files Browse the repository at this point in the history
…ated by space no longer breaks
  • Loading branch information
grbsk committed Feb 16, 2016
1 parent 897fc16 commit c552d07
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
32 changes: 32 additions & 0 deletions src/eventtargetinterruptsource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,45 @@ export function main() {
});
}), 300);

it('emits onInterrupt event when multiple events are specified and one is triggered', injectAsync([], () => {
let source = new EventTargetInterruptSource(document.body, 'click touch');
source.attach();

return new Promise((pass, fail) => {
let expected = new Event('click');

source.onInterrupt.subscribe(() => { pass(); });

document.body.dispatchEvent(expected);
});
}), 300);

it('does not emit onInterrupt event when detached and event is fired', injectAsync([], () => {
let source = new EventTargetInterruptSource(document.body, 'click');

// make it interesting by attaching and detaching
source.attach();
source.detach();

return new Promise((pass, fail) => {
let expected = new Event('click');

source.onInterrupt.subscribe((actual) => { fail(); });

document.body.dispatchEvent(expected);

// HACK: try to give it a chance to fail first, if it's going to fail
setTimeout(pass, 200);
});
}), 300);

it('does not emit onInterrupt event when multiple events are detached and one is triggered', injectAsync([], () => {
let source = new EventTargetInterruptSource(document.body, 'click touch');

// make it interesting by attaching and detaching
source.attach();
source.detach();

return new Promise((pass, fail) => {
let expected = new Event('click');

Expand Down
23 changes: 16 additions & 7 deletions src/eventtargetinterruptsource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import {Observable, Subscription} from 'rxjs/Rx';
* An interrupt source on an EventTarget object, such as a Window or HTMLElement.
*/
export class EventTargetInterruptSource extends InterruptSource {
private eventSrc: Observable<any>;
private eventSubscription: Subscription<any>;
private eventSrc: Array<Observable<any>> = new Array;
private eventSubscription: Array<Subscription<any>> = new Array;

constructor(protected target, protected events: string) {
super(null, null);

this.eventSrc = Observable.fromEvent(target, this.events);

let self = this;

events.split(' ').forEach(function(event) {
self.eventSrc.push(Observable.fromEvent(target, event));
});

let handler = function(innerArgs: any): void {
if (self.filterEvent(innerArgs)) {
return;
Expand All @@ -23,11 +26,17 @@ export class EventTargetInterruptSource extends InterruptSource {
self.onInterrupt.emit(args);
};

this.attachFn = () => { this.eventSubscription = this.eventSrc.subscribe(handler); };
this.attachFn = () => {
this.eventSrc.forEach((src: Observable<any>) => {
self.eventSubscription.push(src.subscribe(handler));
});
};

this.detachFn = () => {
this.eventSubscription.unsubscribe();
this.eventSubscription = null;
this.eventSubscription.forEach((sub: Subscription<any>) => {
sub.unsubscribe();
});
this.eventSubscription.length = 0;
};
}

Expand Down

0 comments on commit c552d07

Please sign in to comment.