-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): add fromEventPattern creator function
- Loading branch information
Showing
5 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* globals describe, it, expect, jasmine */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
var Promise = require('promise'); | ||
|
||
describe('Observable.fromEventPattern', function(){ | ||
it('should call addHandler on subscription', function () { | ||
var addHandlerCalledWith; | ||
var addHandler = function (h) { | ||
addHandlerCalledWith = h; | ||
}; | ||
|
||
var removeHandler = function () { }; | ||
|
||
Observable.fromEventPattern(addHandler, removeHandler) | ||
.subscribe(function () { }); | ||
|
||
expect(typeof addHandlerCalledWith).toBe('function'); | ||
}); | ||
|
||
it('should call removeHandler on unsubscription', function () { | ||
var removeHandlerCalledWith; | ||
var addHandler = function () { }; | ||
var removeHandler = function (h) { | ||
removeHandlerCalledWith = h; | ||
}; | ||
|
||
var subscription = Observable.fromEventPattern(addHandler, removeHandler) | ||
.subscribe(function () { }); | ||
|
||
subscription.unsubscribe(); | ||
|
||
expect(typeof removeHandlerCalledWith).toBe('function'); | ||
}); | ||
|
||
it('should send errors in addHandler down the error path', function () { | ||
Observable.fromEventPattern(function (handler) { | ||
throw 'bad'; | ||
}, function () { }) | ||
.subscribe(function () { }, | ||
function (err) { | ||
expect(err).toBe('bad'); | ||
}); | ||
}); | ||
|
||
it('should accept a selector that maps outgoing values', function (done) { | ||
var target; | ||
var trigger = function () { | ||
if (target) { | ||
target.apply(null, arguments); | ||
} | ||
}; | ||
|
||
var addHandler = function (handler) { | ||
target = handler; | ||
}; | ||
var removeHandler = function (handler) { | ||
target = null; | ||
}; | ||
var selector = function (a, b) { | ||
return a + b + '!'; | ||
}; | ||
|
||
Observable.fromEventPattern(addHandler, removeHandler, selector) | ||
.subscribe(function (x) { | ||
expect(x).toBe('testme!'); | ||
done(); | ||
}); | ||
|
||
trigger('test', 'me'); | ||
}); | ||
|
||
it('should send errors in the selector down the error path', function (done) { | ||
var target; | ||
var trigger = function (value) { | ||
if (target) { | ||
target(value); | ||
} | ||
}; | ||
|
||
var addHandler = function (handler) { | ||
target = handler; | ||
}; | ||
var removeHandler = function (handler) { | ||
target = null; | ||
}; | ||
var selector = function (x) { | ||
throw 'bad'; | ||
}; | ||
|
||
Observable.fromEventPattern(addHandler, removeHandler, selector) | ||
.subscribe(function () { }, | ||
function (err) { | ||
expect(err).toBe('bad'); | ||
done(); | ||
}); | ||
|
||
trigger('test'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Scheduler from '../Scheduler'; | ||
import Observable from '../Observable'; | ||
import Subscription from '../Subscription'; | ||
import tryCatch from '../util/tryCatch'; | ||
import {errorObject} from '../util/errorObject'; | ||
|
||
export default class FromEventPatternObservable<T, R> extends Observable<T> { | ||
|
||
static create<T>(addHandler: (handler: Function) => any, removeHandler: (handler: Function) => void, selector?: (...args:Array<any>) => T) { | ||
return new FromEventPatternObservable(addHandler, removeHandler, selector);; | ||
} | ||
|
||
constructor(private addHandler: (handler:Function) => any, private removeHandler: (handler:Function) => void, private selector?: (...args:Array<any>) => T) { | ||
super(); | ||
} | ||
|
||
_subscribe(subscriber) { | ||
const addHandler = this.addHandler; | ||
const removeHandler = this.removeHandler; | ||
const selector = this.selector; | ||
|
||
const handler = selector ? function(e) { | ||
let result = tryCatch(selector).apply(null, arguments); | ||
if (result === errorObject) { | ||
subscriber.error(result.e); | ||
} else { | ||
subscriber.next(result); | ||
} | ||
} : function(e) { subscriber.next(e); } | ||
|
||
let result = tryCatch(addHandler)(handler); | ||
if (result === errorObject) { | ||
subscriber.error(result.e); | ||
} | ||
subscriber.add(new Subscription(() => { | ||
//TODO: determine whether or not to forward to error handler | ||
removeHandler(handler) | ||
})); | ||
} | ||
} |