Skip to content

Commit

Permalink
feat(fromEvent): will now emit an array when event emits multiple arg…
Browse files Browse the repository at this point in the history
…uments

If an event has a callback with multiple arguments, they will be emitted as an array
  • Loading branch information
benlesh committed Mar 2, 2018
1 parent 197f449 commit 51b37fd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
23 changes: 23 additions & 0 deletions spec/observables/fromEvent-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,29 @@ describe('fromEvent', () => {
send('test');
});

it('should emit multiple arguments from event as an array', (done: MochaDone) => {
let send;
const obj = {
on: (name: string, handler: Function) => {
send = handler;
},
off: () => {
//noop
}
};

fromEvent(obj, 'click').take(1)
.subscribe((e: any) => {
expect(e).to.deep.equal([1, 2, 3]);
}, (err: any) => {
done(new Error('should not be called'));
}, () => {
done();
});

send(1, 2, 3);
});

it('should not throw an exception calling toString on obj with a null prototype', (done: MochaDone) => {
// NOTE: Can not test with Object.create(null) or `class Foo extends null`
// due to TypeScript bug. https://github.com/Microsoft/TypeScript/issues/1108
Expand Down
8 changes: 7 additions & 1 deletion src/internal/observable/fromEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ export function fromEvent<T>(
): Observable<T> {

return new Observable<T>(subscriber => {
const handler = (e: T) => subscriber.next(e);
function handler(e: T) {
if (arguments.length > 1) {
subscriber.next(Array.prototype.slice.call(arguments));
} else {
subscriber.next(e);
}
}
setupSubscription(target, eventName, handler, subscriber, options as EventListenerOptions);
});
}
Expand Down

0 comments on commit 51b37fd

Please sign in to comment.