Skip to content

Commit 197f449

Browse files
committed
feat(fromEvent): remove resultSelector
- removes resultSelector from function signature to simplify the API BREAKING CHANGE: result selector removed, use `map` instead: `fromEvent(target, 'click', fn)` becomes `fromEvent(target, 'click').pipe(map(fn))`
1 parent 31d7a3f commit 197f449

File tree

2 files changed

+7
-134
lines changed

2 files changed

+7
-134
lines changed

spec/observables/fromEvent-spec.ts

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -178,114 +178,6 @@ describe('fromEvent', () => {
178178
send('test');
179179
});
180180

181-
it('should pass through events that occur and use the selector if provided', (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-
function selector(x) {
193-
return x + '!';
194-
}
195-
196-
fromEvent(obj, 'click', selector).take(1)
197-
.subscribe((e: any) => {
198-
expect(e).to.equal('test!');
199-
}, (err: any) => {
200-
done(new Error('should not be called'));
201-
}, () => {
202-
done();
203-
});
204-
205-
send('test');
206-
});
207-
208-
it('should not fail if no event arguments are passed and the selector does not return', (done: MochaDone) => {
209-
let send;
210-
const obj = {
211-
on: (name: string, handler: Function) => {
212-
send = handler;
213-
},
214-
off: () => {
215-
//noop
216-
}
217-
};
218-
219-
function selector() {
220-
//noop
221-
}
222-
223-
fromEvent(obj, 'click', selector).take(1)
224-
.subscribe((e: any) => {
225-
expect(e).not.exist;
226-
}, (err: any) => {
227-
done(new Error('should not be called'));
228-
}, () => {
229-
done();
230-
});
231-
232-
send();
233-
});
234-
235-
it('should return a value from the selector if no event arguments are passed', (done: MochaDone) => {
236-
let send;
237-
const obj = {
238-
on: (name: string, handler: Function) => {
239-
send = handler;
240-
},
241-
off: () => {
242-
//noop
243-
}
244-
};
245-
246-
function selector() {
247-
return 'no arguments';
248-
}
249-
250-
fromEvent(obj, 'click', selector).take(1)
251-
.subscribe((e: any) => {
252-
expect(e).to.equal('no arguments');
253-
}, (err: any) => {
254-
done(new Error('should not be called'));
255-
}, () => {
256-
done();
257-
});
258-
259-
send();
260-
});
261-
262-
it('should pass multiple arguments to selector from event emitter', (done: MochaDone) => {
263-
let send;
264-
const obj = {
265-
on: (name: string, handler: Function) => {
266-
send = handler;
267-
},
268-
off: () => {
269-
//noop
270-
}
271-
};
272-
273-
function selector(x, y, z) {
274-
return [].slice.call(arguments);
275-
}
276-
277-
fromEvent(obj, 'click', selector).take(1)
278-
.subscribe((e: any) => {
279-
expect(e).to.deep.equal([1, 2, 3]);
280-
}, (err: any) => {
281-
done(new Error('should not be called'));
282-
}, () => {
283-
done();
284-
});
285-
286-
send(1, 2, 3);
287-
});
288-
289181
it('should not throw an exception calling toString on obj with a null prototype', (done: MochaDone) => {
290182
// NOTE: Can not test with Object.create(null) or `class Foo extends null`
291183
// due to TypeScript bug. https://github.com/Microsoft/TypeScript/issues/1108

src/internal/observable/fromEvent.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ export type EventListenerOptions = {
2222

2323
/* tslint:disable:max-line-length */
2424
export function fromEvent<T>(target: EventTargetLike, eventName: string): Observable<T>;
25-
export function fromEvent<T>(target: EventTargetLike, eventName: string, selector: (...args: any[]) => T): Observable<T>;
25+
export function fromEvent<T>(target: EventTargetLike, eventName: string): Observable<T>;
2626
export function fromEvent<T>(target: EventTargetLike, eventName: string, options: EventListenerOptions): Observable<T>;
27-
export function fromEvent<T>(target: EventTargetLike, eventName: string, options: EventListenerOptions, selector: (...args: any[]) => T): Observable<T>;
2827
/* tslint:enable:max-line-length */
2928

3029
/**
@@ -135,35 +134,17 @@ export function fromEvent<T>(target: EventTargetLike, eventName: string, options
135134
* @param {string} eventName The event name of interest, being emitted by the
136135
* `target`.
137136
* @param {EventListenerOptions} [options] Options to pass through to addEventListener
138-
* @param {(...args: any[]) => T} [selector] An optional function to
139-
* post-process results. It takes the arguments from the event handler and
140-
* should return a single value.
141137
* @return {Observable<T>}
142-
* @static true
143138
* @name fromEvent
144-
* @owner Observable
145139
*/
146-
export function fromEvent<T>(target: EventTargetLike,
147-
eventName: string,
148-
options?: EventListenerOptions | ((...args: any[]) => T),
149-
selector?: (...args: any[]) => T): Observable<T> {
150-
if (isFunction(options)) {
151-
selector = options as ((...args: any[]) => T);
152-
options = undefined;
153-
}
140+
export function fromEvent<T>(
141+
target: EventTargetLike,
142+
eventName: string,
143+
options?: EventListenerOptions
144+
): Observable<T> {
154145

155146
return new Observable<T>(subscriber => {
156-
const handler = selector ? (...args: any[]) => {
157-
let result: any;
158-
try {
159-
result = selector(...args);
160-
} catch (err) {
161-
subscriber.error(err);
162-
return;
163-
}
164-
subscriber.next(result);
165-
} : (e: any) => subscriber.next(e);
166-
147+
const handler = (e: T) => subscriber.next(e);
167148
setupSubscription(target, eventName, handler, subscriber, options as EventListenerOptions);
168149
});
169150
}

0 commit comments

Comments
 (0)