1 | import { fromEventPattern } from './fromeventpattern';
|
2 | function isNodeEventEmitter(obj) {
|
3 | return !!obj && typeof obj.addListener === 'function' && typeof obj.removeListener === 'function';
|
4 | }
|
5 | function isEventTarget(obj) {
|
6 | return (!!obj &&
|
7 | typeof obj.addEventListener === 'function' &&
|
8 | typeof obj.removeEventListener === 'function');
|
9 | }
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export function fromEvent(obj, type, options) {
|
21 | if (isEventTarget(obj)) {
|
22 | const target = obj;
|
23 | return fromEventPattern((h) => target.addEventListener(type, h, options), (h) => target.removeEventListener(type, h, options));
|
24 | }
|
25 | else if (isNodeEventEmitter(obj)) {
|
26 | const target = obj;
|
27 | return fromEventPattern((h) => target.addListener(type, h), (h) => target.removeListener(type, h));
|
28 | }
|
29 | else {
|
30 | throw new TypeError('Unsupported event target');
|
31 | }
|
32 | }
|
33 |
|
34 |
|