-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
43 lines (35 loc) · 991 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
var EventEmitter = require('eventemitter3');
/**
* Default methods that will help with the assignment.
*
* @type {Array}
* @public
*/
var defaults = 'message,progress,readystatechange,error,abort,close,load,timeout'.split(',');
/**
* Generate an EventEmitter with helper methods so you can assign events in
* a "DOM" way.
*
* @param {Array} methods Methods that we need to add to the instance.
* @api public
*/
module.exports = function generate(methods) {
if ('string' === typeof methods) {
methods = methods.split(/[, ]+/);
}
return (methods = methods || defaults).reduce(function each(emitter, method) {
Object.defineProperty(emitter, 'on'+ method, {
enumerable: true,
get: function get() {
return this.listeners(method).pop();
},
set: function set(fn) {
this.removeAllListeners(method);
this.on(method, fn);
return fn;
}
});
return emitter;
}, new EventEmitter());
};