Skip to content

Commit

Permalink
fix(fromEvent): added spread operator for emitters that pass multiple…
Browse files Browse the repository at this point in the history
… arguments
  • Loading branch information
earlsioson authored and kwonoj committed Dec 28, 2015
1 parent 29aa3af commit 3f8eabb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
73 changes: 72 additions & 1 deletion spec/observables/fromEvent-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,75 @@ describe('Observable.fromEvent', function () {

send('test');
});
});

it('should not fail if no event arguments are passed and the selector does not return', function (done) {
var send;
var obj = {
on: function (name, handler) {
send = handler;
},
off: function () {
}
};

function selector() {
}

Observable.fromEvent(obj, 'click', selector).take(1)
.subscribe(function (e) {
expect(e).toBeUndefined();
}, function (err) {
done.fail('should not be called');
}, done);

send();
});

it('should return a value from the selector if no event arguments are passed', function (done) {
var send;
var obj = {
on: function (name, handler) {
send = handler;
},
off: function () {
}
};

function selector() {
return 'no arguments';
}

Observable.fromEvent(obj, 'click', selector).take(1)
.subscribe(function (e) {
expect(e).toBe('no arguments');
}, function (err) {
done.fail('should not be called');
}, done);

send();
});

it('should pass multiple arguments to selector from event emitter', function (done) {
var send;
var obj = {
on: function (name, handler) {
send = handler;
},
off: function () {
}
};

function selector(x, y, z) {
return [].slice.call(arguments);
}

Observable.fromEvent(obj, 'click', selector).take(1)
.subscribe(function (e) {
expect(e).toEqual([1,2,3]);
}, function (err) {
done.fail('should not be called');
}, done);

send(1, 2, 3);
});
});
4 changes: 2 additions & 2 deletions src/observable/fromEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class FromEventObservable<T, R> extends Observable<T> {
const sourceObj = this.sourceObj;
const eventName = this.eventName;
const selector = this.selector;
let handler = selector ? (e) => {
let result = tryCatch(selector)(e);
let handler = selector ? (...args) => {
let result = tryCatch(selector)(...args);
if (result === errorObject) {
subscriber.error(result.e);
} else {
Expand Down

0 comments on commit 3f8eabb

Please sign in to comment.