Comparing version 2.1.0 to 2.1.1
/*! | ||
* uevent (v2.1.0) | ||
* uevent (v2.1.1) | ||
* @copyright 2015-2022 Damien "Mistic" Sorel <contact@git.strangeplanet.fr> | ||
@@ -91,9 +91,5 @@ * @licence MIT | ||
*/ | ||
_proto.on = function on(events, callback, once) { | ||
_proto.on = function on(events, callback) { | ||
var _this = this; | ||
if (once === void 0) { | ||
once = false; | ||
} | ||
this.__events = this.__events || {}; | ||
@@ -104,6 +100,2 @@ | ||
if (events.hasOwnProperty(event)) { | ||
if (once) { | ||
events[event].__once = true; | ||
} | ||
this.__events[event] = this.__events[event] || []; | ||
@@ -115,6 +107,2 @@ | ||
} else { | ||
if (once) { | ||
callback.__once = true; | ||
} | ||
events.split(' ').forEach(function (event) { | ||
@@ -192,3 +180,23 @@ _this.__events[event] = _this.__events[event] || []; | ||
_proto.once = function once(events, callback) { | ||
return this.on(events, callback, true); | ||
var _this3 = this; | ||
this.__once = this.__once || {}; | ||
if (typeof events === 'object') { | ||
for (var event in events) { | ||
if (events.hasOwnProperty(event)) { | ||
this.__once[event] = this.__once[event] || []; | ||
this.__once[event].push(events[event]); | ||
} | ||
} | ||
} else { | ||
events.split(' ').forEach(function (event) { | ||
_this3.__once[event] = _this3.__once[event] || []; | ||
_this3.__once[event].push(callback); | ||
}); | ||
} | ||
return this; | ||
} | ||
@@ -211,11 +219,5 @@ /** | ||
if (this.__events && event in this.__events) { | ||
var hasOnce = false; | ||
for (var i = 0, l = this.__events[event].length; i < l; i++) { | ||
var f = this.__events[event][i]; | ||
if (f.__once) { | ||
hasOnce = true; | ||
} | ||
if (typeof f === 'object') { | ||
@@ -231,8 +233,20 @@ f.handleEvent(e); | ||
} | ||
} | ||
if (hasOnce) { | ||
this.__events[event] = this.__events[event].filter(function (f) { | ||
return !f.__once; | ||
}); | ||
if (this.__once && event in this.__once) { | ||
for (var _i = 0, _l = this.__once[event].length; _i < _l; _i++) { | ||
var _f = this.__once[event][_i]; | ||
if (typeof _f === 'object') { | ||
_f.handleEvent(e); | ||
} else { | ||
_f.call.apply(_f, [this, e].concat(args)); | ||
} | ||
if (e.isPropagationStopped()) { | ||
break; | ||
} | ||
} | ||
delete this.__once[event]; | ||
} | ||
@@ -263,6 +277,2 @@ | ||
if (f.__once) { | ||
continue; | ||
} | ||
if (typeof f === 'object') { | ||
@@ -269,0 +279,0 @@ e.value = f.handleEvent(e); |
{ | ||
"name": "uevent", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Event emitter micro library", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -22,3 +22,3 @@ const Event = require('./Event'); | ||
*/ | ||
on(events, callback, once = false) { | ||
on(events, callback) { | ||
this.__events = this.__events || {}; | ||
@@ -29,5 +29,2 @@ | ||
if (events.hasOwnProperty(event)) { | ||
if (once) { | ||
events[event].__once = true; | ||
} | ||
this.__events[event] = this.__events[event] || []; | ||
@@ -39,5 +36,2 @@ this.__events[event].push(events[event]); | ||
else { | ||
if (once) { | ||
callback.__once = true; | ||
} | ||
events.split(' ').forEach((event) => { | ||
@@ -111,3 +105,20 @@ this.__events[event] = this.__events[event] || []; | ||
once(events, callback) { | ||
return this.on(events, callback, true); | ||
this.__once = this.__once || {}; | ||
if (typeof events === 'object') { | ||
for (const event in events) { | ||
if (events.hasOwnProperty(event)) { | ||
this.__once[event] = this.__once[event] || []; | ||
this.__once[event].push(events[event]); | ||
} | ||
} | ||
} | ||
else { | ||
events.split(' ').forEach((event) => { | ||
this.__once[event] = this.__once[event] || []; | ||
this.__once[event].push(callback); | ||
}); | ||
} | ||
return this; | ||
} | ||
@@ -127,8 +138,19 @@ | ||
if (this.__events && event in this.__events) { | ||
let hasOnce = false; | ||
for (let i = 0, l = this.__events[event].length; i < l; i++) { | ||
let f = this.__events[event][i]; | ||
if (f.__once) { | ||
hasOnce = true; | ||
const f = this.__events[event][i]; | ||
if (typeof f === 'object') { | ||
f.handleEvent(e); | ||
} | ||
else { | ||
f.call(this, e, ...args); | ||
} | ||
if (e.isPropagationStopped()) { | ||
break; | ||
} | ||
} | ||
} | ||
if (this.__once && event in this.__once) { | ||
for (let i = 0, l = this.__once[event].length; i < l; i++) { | ||
const f = this.__once[event][i]; | ||
if (typeof f === 'object') { | ||
@@ -144,5 +166,3 @@ f.handleEvent(e); | ||
} | ||
if (hasOnce) { | ||
this.__events[event] = this.__events[event].filter(f => !f.__once); | ||
} | ||
delete this.__once[event]; | ||
} | ||
@@ -169,5 +189,2 @@ | ||
const f = this.__events[event][i]; | ||
if (f.__once) { | ||
continue; | ||
} | ||
if (typeof f === 'object') { | ||
@@ -174,0 +191,0 @@ e.value = f.handleEvent(e); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38632
547