Skip to content

Commit 51b37fd

Browse files
committedMar 2, 2018
feat(fromEvent): will now emit an array when event emits multiple arguments
If an event has a callback with multiple arguments, they will be emitted as an array
1 parent 197f449 commit 51b37fd

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
 

‎spec/observables/fromEvent-spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,29 @@ describe('fromEvent', () => {
178178
send('test');
179179
});
180180

181+
it('should emit multiple arguments from event as an array', (done: MochaDone) => {
182+
let send;
183+
const obj = {
184+
on: (name: string, handler: Function) => {
185+
send = handler;
186+
},
187+
off: () => {
188+
//noop
189+
}
190+
};
191+
192+
fromEvent(obj, 'click').take(1)
193+
.subscribe((e: any) => {
194+
expect(e).to.deep.equal([1, 2, 3]);
195+
}, (err: any) => {
196+
done(new Error('should not be called'));
197+
}, () => {
198+
done();
199+
});
200+
201+
send(1, 2, 3);
202+
});
203+
181204
it('should not throw an exception calling toString on obj with a null prototype', (done: MochaDone) => {
182205
// NOTE: Can not test with Object.create(null) or `class Foo extends null`
183206
// due to TypeScript bug. https://github.com/Microsoft/TypeScript/issues/1108

‎src/internal/observable/fromEvent.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,13 @@ export function fromEvent<T>(
144144
): Observable<T> {
145145

146146
return new Observable<T>(subscriber => {
147-
const handler = (e: T) => subscriber.next(e);
147+
function handler(e: T) {
148+
if (arguments.length > 1) {
149+
subscriber.next(Array.prototype.slice.call(arguments));
150+
} else {
151+
subscriber.next(e);
152+
}
153+
}
148154
setupSubscription(target, eventName, handler, subscriber, options as EventListenerOptions);
149155
});
150156
}

0 commit comments

Comments
 (0)
Please sign in to comment.