50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.fromEvent = fromEvent;
|
||
|
|
||
|
var _Stream = require('../Stream');
|
||
|
|
||
|
var _Stream2 = _interopRequireDefault(_Stream);
|
||
|
|
||
|
var _EventTargetSource = require('./EventTargetSource');
|
||
|
|
||
|
var _EventTargetSource2 = _interopRequireDefault(_EventTargetSource);
|
||
|
|
||
|
var _EventEmitterSource = require('./EventEmitterSource');
|
||
|
|
||
|
var _EventEmitterSource2 = _interopRequireDefault(_EventEmitterSource);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
/**
|
||
|
* Create a stream from an EventTarget, such as a DOM Node, or EventEmitter.
|
||
|
* @param {String} event event type name, e.g. 'click'
|
||
|
* @param {EventTarget|EventEmitter} source EventTarget or EventEmitter
|
||
|
* @param {*?} capture for DOM events, whether to use
|
||
|
* capturing--passed as 3rd parameter to addEventListener.
|
||
|
* @returns {Stream} stream containing all events of the specified type
|
||
|
* from the source.
|
||
|
*/
|
||
|
function fromEvent(event, source, capture) {
|
||
|
// eslint-disable-line complexity
|
||
|
var s;
|
||
|
|
||
|
if (typeof source.addEventListener === 'function' && typeof source.removeEventListener === 'function') {
|
||
|
if (arguments.length < 3) {
|
||
|
capture = false;
|
||
|
}
|
||
|
|
||
|
s = new _EventTargetSource2.default(event, source, capture);
|
||
|
} else if (typeof source.addListener === 'function' && typeof source.removeListener === 'function') {
|
||
|
s = new _EventEmitterSource2.default(event, source);
|
||
|
} else {
|
||
|
throw new Error('source must support addEventListener/removeEventListener or addListener/removeListener');
|
||
|
}
|
||
|
|
||
|
return new _Stream2.default(s);
|
||
|
} /** @license MIT License (c) copyright 2010-2016 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|