59 lines
No EOL
2.5 KiB
JavaScript
Executable file
59 lines
No EOL
2.5 KiB
JavaScript
Executable file
import { __read } from "tslib";
|
|
import { innerFrom } from '../observable/innerFrom';
|
|
import { Observable } from '../Observable';
|
|
import { mergeMap } from '../operators/mergeMap';
|
|
import { isArrayLike } from '../util/isArrayLike';
|
|
import { isFunction } from '../util/isFunction';
|
|
import { mapOneOrManyArgs } from '../util/mapOneOrManyArgs';
|
|
var nodeEventEmitterMethods = ['addListener', 'removeListener'];
|
|
var eventTargetMethods = ['addEventListener', 'removeEventListener'];
|
|
var jqueryMethods = ['on', 'off'];
|
|
export function fromEvent(target, eventName, options, resultSelector) {
|
|
if (isFunction(options)) {
|
|
resultSelector = options;
|
|
options = undefined;
|
|
}
|
|
if (resultSelector) {
|
|
return fromEvent(target, eventName, options).pipe(mapOneOrManyArgs(resultSelector));
|
|
}
|
|
var _a = __read(isEventTarget(target)
|
|
? eventTargetMethods.map(function (methodName) { return function (handler) { return target[methodName](eventName, handler, options); }; })
|
|
:
|
|
isNodeStyleEventEmitter(target)
|
|
? nodeEventEmitterMethods.map(toCommonHandlerRegistry(target, eventName))
|
|
: isJQueryStyleEventEmitter(target)
|
|
? jqueryMethods.map(toCommonHandlerRegistry(target, eventName))
|
|
: [], 2), add = _a[0], remove = _a[1];
|
|
if (!add) {
|
|
if (isArrayLike(target)) {
|
|
return mergeMap(function (subTarget) { return fromEvent(subTarget, eventName, options); })(innerFrom(target));
|
|
}
|
|
}
|
|
if (!add) {
|
|
throw new TypeError('Invalid event target');
|
|
}
|
|
return new Observable(function (subscriber) {
|
|
var handler = function () {
|
|
var args = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
args[_i] = arguments[_i];
|
|
}
|
|
return subscriber.next(1 < args.length ? args : args[0]);
|
|
};
|
|
add(handler);
|
|
return function () { return remove(handler); };
|
|
});
|
|
}
|
|
function toCommonHandlerRegistry(target, eventName) {
|
|
return function (methodName) { return function (handler) { return target[methodName](eventName, handler); }; };
|
|
}
|
|
function isNodeStyleEventEmitter(target) {
|
|
return isFunction(target.addListener) && isFunction(target.removeListener);
|
|
}
|
|
function isJQueryStyleEventEmitter(target) {
|
|
return isFunction(target.on) && isFunction(target.off);
|
|
}
|
|
function isEventTarget(target) {
|
|
return isFunction(target.addEventListener) && isFunction(target.removeEventListener);
|
|
}
|
|
//# sourceMappingURL=fromEvent.js.map
|