source: trip-planner-front/node_modules/rxjs/_esm2015/internal/observable/fromEvent.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.4 KB
Line 
1import { Observable } from '../Observable';
2import { isArray } from '../util/isArray';
3import { isFunction } from '../util/isFunction';
4import { map } from '../operators/map';
5const toString = (() => Object.prototype.toString)();
6export function fromEvent(target, eventName, options, resultSelector) {
7 if (isFunction(options)) {
8 resultSelector = options;
9 options = undefined;
10 }
11 if (resultSelector) {
12 return fromEvent(target, eventName, options).pipe(map(args => isArray(args) ? resultSelector(...args) : resultSelector(args)));
13 }
14 return new Observable(subscriber => {
15 function handler(e) {
16 if (arguments.length > 1) {
17 subscriber.next(Array.prototype.slice.call(arguments));
18 }
19 else {
20 subscriber.next(e);
21 }
22 }
23 setupSubscription(target, eventName, handler, subscriber, options);
24 });
25}
26function setupSubscription(sourceObj, eventName, handler, subscriber, options) {
27 let unsubscribe;
28 if (isEventTarget(sourceObj)) {
29 const source = sourceObj;
30 sourceObj.addEventListener(eventName, handler, options);
31 unsubscribe = () => source.removeEventListener(eventName, handler, options);
32 }
33 else if (isJQueryStyleEventEmitter(sourceObj)) {
34 const source = sourceObj;
35 sourceObj.on(eventName, handler);
36 unsubscribe = () => source.off(eventName, handler);
37 }
38 else if (isNodeStyleEventEmitter(sourceObj)) {
39 const source = sourceObj;
40 sourceObj.addListener(eventName, handler);
41 unsubscribe = () => source.removeListener(eventName, handler);
42 }
43 else if (sourceObj && sourceObj.length) {
44 for (let i = 0, len = sourceObj.length; i < len; i++) {
45 setupSubscription(sourceObj[i], eventName, handler, subscriber, options);
46 }
47 }
48 else {
49 throw new TypeError('Invalid event target');
50 }
51 subscriber.add(unsubscribe);
52}
53function isNodeStyleEventEmitter(sourceObj) {
54 return sourceObj && typeof sourceObj.addListener === 'function' && typeof sourceObj.removeListener === 'function';
55}
56function isJQueryStyleEventEmitter(sourceObj) {
57 return sourceObj && typeof sourceObj.on === 'function' && typeof sourceObj.off === 'function';
58}
59function isEventTarget(sourceObj) {
60 return sourceObj && typeof sourceObj.addEventListener === 'function' && typeof sourceObj.removeEventListener === 'function';
61}
62//# sourceMappingURL=fromEvent.js.map
Note: See TracBrowser for help on using the repository browser.