|
1 | 1 | import { fromEvent } from 'rxjs';
|
2 |
| -import { JQueryStyleEventEmitter } from '../../src/internal/observable/fromEvent'; |
3 |
| -import { A, B } from '../helpers'; |
| 2 | +import { |
| 3 | + HasEventTargetAddRemove, |
| 4 | + NodeStyleEventEmitter, |
| 5 | + NodeCompatibleEventEmitter, |
| 6 | + JQueryStyleEventEmitter |
| 7 | +} from '../../src/internal/observable/fromEvent'; |
| 8 | +import { B } from '../helpers'; |
4 | 9 |
|
5 | 10 | declare const eventTargetSource: EventTarget;
|
6 | 11 |
|
7 | 12 | it('should support an event target source', () => {
|
| 13 | + const source: HasEventTargetAddRemove<Event> = eventTargetSource; |
8 | 14 | const a = fromEvent(eventTargetSource, "click"); // $ExpectType Observable<Event>
|
9 | 15 | });
|
10 | 16 |
|
| 17 | +it('should support an event target source result selector', () => { |
| 18 | + const a = fromEvent(eventTargetSource, "click", () => "clunk"); // $ExpectType Observable<string> |
| 19 | +}); |
| 20 | + |
11 | 21 | declare const documentSource: HTMLDocument;
|
12 | 22 |
|
13 | 23 | it('should support a document source', () => {
|
| 24 | + const source: HasEventTargetAddRemove<Event> = documentSource; |
14 | 25 | const a = fromEvent(documentSource, "click"); // $ExpectType Observable<Event>
|
15 | 26 | });
|
16 | 27 |
|
17 |
| -interface NodeStyleSource { |
18 |
| - addListener: (eventName: string | symbol, handler: (...args: any[]) => void) => this; |
19 |
| - removeListener: (eventName: string | symbol, handler: (...args: any[]) => void) => this; |
20 |
| -}; |
| 28 | +it('should support a document source result selector', () => { |
| 29 | + const a = fromEvent(documentSource, "click", () => "clunk"); // $ExpectType Observable<string> |
| 30 | +}); |
21 | 31 |
|
22 |
| -declare const nodeStyleSource : NodeStyleSource; |
| 32 | +// Pick the parts that will match NodeStyleEventEmitter. If this isn't done, it |
| 33 | +// will match JQueryStyleEventEmitter - because of the `on` and `off` methods - |
| 34 | +// despite the latter being declared last in the EventTargetLike union. |
| 35 | +declare const nodeStyleSource: Pick<typeof process, 'addListener' | 'removeListener'>; |
23 | 36 |
|
24 | 37 | it('should support a node-style source', () => {
|
25 |
| - const a = fromEvent(nodeStyleSource, "something"); // $ExpectType Observable<unknown> |
26 |
| - const b = fromEvent<B>(nodeStyleSource, "something"); // $ExpectType Observable<B> |
| 38 | + const source: NodeStyleEventEmitter = nodeStyleSource; |
| 39 | + const a = fromEvent(nodeStyleSource, "exit"); // $ExpectType Observable<unknown> |
| 40 | + const b = fromEvent<B>(nodeStyleSource, "exit"); // $ExpectType Observable<B> |
| 41 | +}); |
| 42 | + |
| 43 | +it('should support a node-style source result selector', () => { |
| 44 | + const a = fromEvent(nodeStyleSource, "exit", () => "bye"); // $ExpectType Observable<string> |
27 | 45 | });
|
28 | 46 |
|
29 |
| -declare const nodeCompatibleSource: { |
30 |
| - addListener: (eventName: string, handler: (...args: any[]) => void) => void; |
31 |
| - removeListener: (eventName: string, handler: (...args: any[]) => void) => void; |
| 47 | +const nodeCompatibleSource = { |
| 48 | + addListener(eventName: "something", handler: () => void) {}, |
| 49 | + removeListener(eventName: "something", handler: () => void) {} |
32 | 50 | };
|
33 | 51 |
|
34 | 52 | it('should support a node-compatible source', () => {
|
| 53 | + const source: NodeCompatibleEventEmitter = nodeCompatibleSource; |
35 | 54 | const a = fromEvent(nodeCompatibleSource, "something"); // $ExpectType Observable<unknown>
|
36 | 55 | const b = fromEvent<B>(nodeCompatibleSource, "something"); // $ExpectType Observable<B>
|
37 | 56 | });
|
38 | 57 |
|
39 |
| -declare const jQueryStyleSource: JQueryStyleEventEmitter<A, B>; |
| 58 | +it('should support a node-compatible source result selector', () => { |
| 59 | + const a = fromEvent(nodeCompatibleSource, "something", () => "something else"); // $ExpectType Observable<string> |
| 60 | +}); |
| 61 | + |
| 62 | +const jQueryStyleSource = { |
| 63 | + on(eventName: "something", handler: (this: any, b: B) => any) {}, |
| 64 | + off(eventName: "something", handler: (this: any, b: B) => any) {} |
| 65 | +}; |
40 | 66 |
|
41 | 67 | it('should support a jQuery-style source', () => {
|
| 68 | + const source: JQueryStyleEventEmitter<any, any> = jQueryStyleSource; |
42 | 69 | const a = fromEvent(jQueryStyleSource, "something"); // $ExpectType Observable<B>
|
43 | 70 | const b = fromEvent<B>(jQueryStyleSource, "something"); // $ExpectType Observable<B>
|
44 | 71 | });
|
| 72 | + |
| 73 | +it('should support a jQuery-style source result selector', () => { |
| 74 | + const a = fromEvent(jQueryStyleSource, "something", () => "something else"); // $ExpectType Observable<string> |
| 75 | +}); |
0 commit comments