Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor from event pattern #3363

Merged
merged 2 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 13 additions & 74 deletions spec/observables/fromEventPattern-spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import * as Rx from '../../src/Rx';
import { noop } from '../../src/internal/util/noop';
import { expectObservable } from '../helpers/marble-testing';

import { fromEventPattern, noop, never, timer } from '../../src';
import { TestScheduler } from '../../src/testing';

declare function asDiagram(arg: string): Function;
declare const rxTestScheduler: Rx.TestScheduler;
const Observable = Rx.Observable;
declare const rxTestScheduler: TestScheduler;

/** @test {fromEventPattern} */
describe('Observable.fromEventPattern', () => {
describe('fromEventPattern', () => {
asDiagram('fromEventPattern(addHandler, removeHandler)')
('should create an observable from the handler API', () => {
function addHandler(h) {
Observable.timer(50, 20, rxTestScheduler)
timer(50, 20, rxTestScheduler)
.mapTo('ev')
.take(2)
.concat(Observable.never())
.concat(never())
.subscribe(h);
}
const e1 = Observable.fromEventPattern(addHandler);
const e1 = fromEventPattern(addHandler);
const expected = '-----x-x---';
expectObservable(e1).toBe(expected, {x: 'ev'});
});

it('should call addHandler on subscription', () => {
const addHandler = sinon.spy();
Observable.fromEventPattern(addHandler, noop).subscribe(noop);
fromEventPattern(addHandler, noop).subscribe(noop);

const call = addHandler.getCall(0);
expect(addHandler).calledOnce;
Expand All @@ -36,7 +36,7 @@ describe('Observable.fromEventPattern', () => {
it('should call removeHandler on unsubscription', () => {
const removeHandler = sinon.spy();

Observable.fromEventPattern(noop, removeHandler).subscribe(noop).unsubscribe();
fromEventPattern(noop, removeHandler).subscribe(noop).unsubscribe();

const call = removeHandler.getCall(0);
expect(removeHandler).calledOnce;
Expand All @@ -45,7 +45,7 @@ describe('Observable.fromEventPattern', () => {

it('should work without optional removeHandler', () => {
const addHandler: (h: Function) => any = sinon.spy();
Observable.fromEventPattern(addHandler).subscribe(noop);
fromEventPattern(addHandler).subscribe(noop);

expect(addHandler).calledOnce;
});
Expand All @@ -54,14 +54,14 @@ describe('Observable.fromEventPattern', () => {
const expected = { signal: true};
const addHandler = () => expected;
const removeHandler = sinon.spy();
Observable.fromEventPattern(addHandler, removeHandler).subscribe(noop).unsubscribe();
fromEventPattern(addHandler, removeHandler).subscribe(noop).unsubscribe();

const call = removeHandler.getCall(0);
expect(call).calledWith(sinon.match.any, expected);
});

it('should send errors in addHandler down the error path', (done: MochaDone) => {
Observable.fromEventPattern((h: any) => {
fromEventPattern((h: any) => {
throw 'bad';
}, noop).subscribe(
() => done(new Error('should not be called')),
Expand All @@ -70,65 +70,4 @@ describe('Observable.fromEventPattern', () => {
done();
}, () => done(new Error('should not be called')));
});

it('should accept a selector that maps outgoing values', (done: MochaDone) => {
let target;
const trigger = function (...args) {
if (target) {
target.apply(null, arguments);
}
};

const addHandler = (handler: any) => {
target = handler;
};
const removeHandler = (handler: any) => {
target = null;
};
const selector = (a: any, b: any) => {
return a + b + '!';
};

Observable.fromEventPattern(addHandler, removeHandler, selector).take(1)
.subscribe((x: any) => {
expect(x).to.equal('testme!');
}, (err: any) => {
done(new Error('should not be called'));
}, () => {
done();
});

trigger('test', 'me');
});

it('should send errors in the selector down the error path', (done: MochaDone) => {
let target;
const trigger = (value: any) => {
if (target) {
target(value);
}
};

const addHandler = (handler: any) => {
target = handler;
};
const removeHandler = (handler: any) => {
target = null;
};
const selector = (x: any) => {
throw 'bad';
};

Observable.fromEventPattern(addHandler, removeHandler, selector)
.subscribe((x: any) => {
done(new Error('should not be called'));
}, (err: any) => {
expect(err).to.equal('bad');
done();
}, () => {
done(new Error('should not be called'));
});

trigger('test');
});
});
110 changes: 0 additions & 110 deletions src/internal/observable/FromEventPatternObservable.ts

This file was deleted.

66 changes: 64 additions & 2 deletions src/internal/observable/fromEventPattern.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,65 @@
import { FromEventPatternObservable } from './FromEventPatternObservable';
import { Observable } from '../Observable';
import { isFunction } from '../util/isFunction';

export const fromEventPattern = FromEventPatternObservable.create;
/**
* Creates an Observable from an API based on addHandler/removeHandler
* functions.
*
* <span class="informal">Converts any addHandler/removeHandler API to an
* Observable.</span>
*
* <img src="./img/fromEventPattern.png" width="100%">
*
* Creates an Observable by using the `addHandler` and `removeHandler`
* functions to add and remove the handlers. The `addHandler` is
* called when the output Observable is subscribed, and `removeHandler` is
* called when the Subscription is unsubscribed.
*
* @example <caption>Emits clicks happening on the DOM document</caption>
* function addClickHandler(handler) {
* document.addEventListener('click', handler);
* }
*
* function removeClickHandler(handler) {
* document.removeEventListener('click', handler);
* }
*
* var clicks = fromEventPattern(
* addClickHandler,
* removeClickHandler
* );
* clicks.subscribe(x => console.log(x));
*
* @see {@link from}
* @see {@link fromEvent}
*
* @param {function(handler: Function): any} addHandler A function that takes
* a `handler` function as argument and attaches it somehow to the actual
* source of events.
* @param {function(handler: Function, signal?: any): void} [removeHandler] An optional function that
* takes a `handler` function as argument and removes it in case it was
* previously attached using `addHandler`. if addHandler returns signal to teardown when remove,
* removeHandler function will forward it.
* @return {Observable<T>}
* @name fromEventPattern
*/
export function fromEventPattern<T>(addHandler: (handler: Function) => any,
removeHandler?: (handler: Function, signal?: any) => void) {
return new Observable<T>(subscriber => {
const handler = (e: T) => subscriber.next(e);

let retValue: any;
try {
retValue = addHandler(handler);
} catch (err) {
subscriber.error(err);
return undefined;
}

if (!isFunction(removeHandler)) {
return undefined;
}

return () => removeHandler(handler, retValue) ;
});
}