1 /** PURE_IMPORTS_START _Observable,_util_isArray,_util_isFunction,_operators_map PURE_IMPORTS_END */
2 import { Observable } from '../Observable';
3 import { isArray } from '../util/isArray';
4 import { isFunction } from '../util/isFunction';
5 import { map } from '../operators/map';
6 var toString = /*@__PURE__*/ (function () { return Object.prototype.toString; })();
7 export function fromEvent(target, eventName, options, resultSelector) {
8 if (isFunction(options)) {
9 resultSelector = options;
13 return fromEvent(target, eventName, options).pipe(map(function (args) { return isArray(args) ? resultSelector.apply(void 0, args) : resultSelector(args); }));
15 return new Observable(function (subscriber) {
17 if (arguments.length > 1) {
18 subscriber.next(Array.prototype.slice.call(arguments));
24 setupSubscription(target, eventName, handler, subscriber, options);
27 function setupSubscription(sourceObj, eventName, handler, subscriber, options) {
29 if (isEventTarget(sourceObj)) {
30 var source_1 = sourceObj;
31 sourceObj.addEventListener(eventName, handler, options);
32 unsubscribe = function () { return source_1.removeEventListener(eventName, handler, options); };
34 else if (isJQueryStyleEventEmitter(sourceObj)) {
35 var source_2 = sourceObj;
36 sourceObj.on(eventName, handler);
37 unsubscribe = function () { return source_2.off(eventName, handler); };
39 else if (isNodeStyleEventEmitter(sourceObj)) {
40 var source_3 = sourceObj;
41 sourceObj.addListener(eventName, handler);
42 unsubscribe = function () { return source_3.removeListener(eventName, handler); };
44 else if (sourceObj && sourceObj.length) {
45 for (var i = 0, len = sourceObj.length; i < len; i++) {
46 setupSubscription(sourceObj[i], eventName, handler, subscriber, options);
50 throw new TypeError('Invalid event target');
52 subscriber.add(unsubscribe);
54 function isNodeStyleEventEmitter(sourceObj) {
55 return sourceObj && typeof sourceObj.addListener === 'function' && typeof sourceObj.removeListener === 'function';
57 function isJQueryStyleEventEmitter(sourceObj) {
58 return sourceObj && typeof sourceObj.on === 'function' && typeof sourceObj.off === 'function';
60 function isEventTarget(sourceObj) {
61 return sourceObj && typeof sourceObj.addEventListener === 'function' && typeof sourceObj.removeEventListener === 'function';
63 //# sourceMappingURL=fromEvent.js.map