import { Observable } from '../Observable'; import { isArray } from '../util/isArray'; import { isFunction } from '../util/isFunction'; import { map } from '../operators/map'; const toString = (() => Object.prototype.toString)(); export function fromEvent(target, eventName, options, resultSelector) { if (isFunction(options)) { resultSelector = options; options = undefined; } if (resultSelector) { return fromEvent(target, eventName, options).pipe(map(args => isArray(args) ? resultSelector(...args) : resultSelector(args))); } return new Observable(subscriber => { function handler(e) { if (arguments.length > 1) { subscriber.next(Array.prototype.slice.call(arguments)); } else { subscriber.next(e); } } setupSubscription(target, eventName, handler, subscriber, options); }); } function setupSubscription(sourceObj, eventName, handler, subscriber, options) { let unsubscribe; if (isEventTarget(sourceObj)) { const source = sourceObj; sourceObj.addEventListener(eventName, handler, options); unsubscribe = () => source.removeEventListener(eventName, handler, options); } else if (isJQueryStyleEventEmitter(sourceObj)) { const source = sourceObj; sourceObj.on(eventName, handler); unsubscribe = () => source.off(eventName, handler); } else if (isNodeStyleEventEmitter(sourceObj)) { const source = sourceObj; sourceObj.addListener(eventName, handler); unsubscribe = () => source.removeListener(eventName, handler); } else if (sourceObj && sourceObj.length) { for (let i = 0, len = sourceObj.length; i < len; i++) { setupSubscription(sourceObj[i], eventName, handler, subscriber, options); } } else { throw new TypeError('Invalid event target'); } subscriber.add(unsubscribe); } function isNodeStyleEventEmitter(sourceObj) { return sourceObj && typeof sourceObj.addListener === 'function' && typeof sourceObj.removeListener === 'function'; } function isJQueryStyleEventEmitter(sourceObj) { return sourceObj && typeof sourceObj.on === 'function' && typeof sourceObj.off === 'function'; } function isEventTarget(sourceObj) { return sourceObj && typeof sourceObj.addEventListener === 'function' && typeof sourceObj.removeEventListener === 'function'; } //# sourceMappingURL=fromEvent.js.map
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
dom | Folder | 0755 |
|
|
ConnectableObservable.js | File | 4.14 KB | 0644 |
|
ConnectableObservable.js.map | File | 3.82 KB | 0644 |
|
SubscribeOnObservable.js | File | 1.14 KB | 0644 |
|
SubscribeOnObservable.js.map | File | 1.2 KB | 0644 |
|
bindCallback.js | File | 2.82 KB | 0644 |
|
bindCallback.js.map | File | 2.83 KB | 0644 |
|
bindNodeCallback.js | File | 3.25 KB | 0644 |
|
bindNodeCallback.js.map | File | 3.23 KB | 0644 |
|
combineLatest.js | File | 2.82 KB | 0644 |
|
combineLatest.js.map | File | 2.86 KB | 0644 |
|
concat.js | File | 200 B | 0644 |
|
concat.js.map | File | 308 B | 0644 |
|
defer.js | File | 516 B | 0644 |
|
defer.js.map | File | 636 B | 0644 |
|
empty.js | File | 375 B | 0644 |
|
empty.js.map | File | 521 B | 0644 |
|
forkJoin.js | File | 2.2 KB | 0644 |
|
forkJoin.js.map | File | 2.43 KB | 0644 |
|
from.js | File | 441 B | 0644 |
|
from.js.map | File | 489 B | 0644 |
|
fromArray.js | File | 399 B | 0644 |
|
fromArray.js.map | File | 446 B | 0644 |
|
fromEvent.js | File | 2.45 KB | 0644 |
|
fromEvent.js.map | File | 2.53 KB | 0644 |
|
fromEventPattern.js | File | 926 B | 0644 |
|
fromEventPattern.js.map | File | 1.09 KB | 0644 |
|
fromIterable.js | File | 499 B | 0644 |
|
fromIterable.js.map | File | 529 B | 0644 |
|
fromPromise.js | File | 415 B | 0644 |
|
fromPromise.js.map | File | 450 B | 0644 |
|
generate.js | File | 3.39 KB | 0644 |
|
generate.js.map | File | 2.88 KB | 0644 |
|
iif.js | File | 237 B | 0644 |
|
iif.js.map | File | 364 B | 0644 |
|
interval.js | File | 758 B | 0644 |
|
interval.js.map | File | 945 B | 0644 |
|
merge.js | File | 901 B | 0644 |
|
merge.js.map | File | 995 B | 0644 |
|
never.js | File | 203 B | 0644 |
|
never.js.map | File | 307 B | 0644 |
|
of.js | File | 413 B | 0644 |
|
of.js.map | File | 511 B | 0644 |
|
onErrorResumeNext.js | File | 763 B | 0644 |
|
onErrorResumeNext.js.map | File | 941 B | 0644 |
|
pairs.js | File | 1.29 KB | 0644 |
|
pairs.js.map | File | 1.52 KB | 0644 |
|
partition.js | File | 441 B | 0644 |
|
partition.js.map | File | 562 B | 0644 |
|
race.js | File | 2.04 KB | 0644 |
|
race.js.map | File | 2.01 KB | 0644 |
|
range.js | File | 1.14 KB | 0644 |
|
range.js.map | File | 1.19 KB | 0644 |
|
throwError.js | File | 421 B | 0644 |
|
throwError.js.map | File | 565 B | 0644 |
|
timer.js | File | 1.12 KB | 0644 |
|
timer.js.map | File | 1.29 KB | 0644 |
|
using.js | File | 898 B | 0644 |
|
using.js.map | File | 939 B | 0644 |
|
zip.js | File | 5.7 KB | 0644 |
|
zip.js.map | File | 5.67 KB | 0644 |
|