Files
Zos/Skills/@be/be/node_modules/mini-signals/lib/mini-signals.js

166 lines
4.4 KiB
JavaScript

'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var MiniSignalBinding = (function () {
function MiniSignalBinding(fn, once, thisArg) {
if (once === undefined) once = false;
_classCallCheck(this, MiniSignalBinding);
this._fn = fn;
this._once = once;
this._thisArg = thisArg;
this._next = this._prev = this._owner = null;
}
_createClass(MiniSignalBinding, [{
key: 'detach',
value: function detach() {
if (this._owner === null) return false;
this._owner.detach(this);
return true;
}
}]);
return MiniSignalBinding;
})();
function _addMiniSignalBinding(self, node) {
if (!self._head) {
self._head = node;
self._tail = node;
} else {
self._tail._next = node;
node._prev = self._tail;
self._tail = node;
}
node._owner = self;
return node;
}
var MiniSignal = (function () {
function MiniSignal() {
_classCallCheck(this, MiniSignal);
this._head = this._tail = undefined;
}
_createClass(MiniSignal, [{
key: 'handlers',
value: function handlers() {
var exists = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
var node = this._head;
if (exists) return !!node;
var ee = [];
while (node) {
ee.push(node);
node = node._next;
}
return ee;
}
}, {
key: 'has',
value: function has(node) {
if (!(node instanceof MiniSignalBinding)) {
throw new Error('MiniSignal#has(): First arg must be a MiniSignalBinding object.');
}
return node._owner === this;
}
}, {
key: 'dispatch',
value: function dispatch() {
var node = this._head;
if (!node) return false;
while (node) {
if (node._once) this.detach(node);
node._fn.apply(node._thisArg, arguments);
node = node._next;
}
return true;
}
}, {
key: 'add',
value: function add(fn) {
var thisArg = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
if (typeof fn !== 'function') {
throw new Error('MiniSignal#add(): First arg must be a Function.');
}
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, false, thisArg));
}
}, {
key: 'once',
value: function once(fn) {
var thisArg = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
if (typeof fn !== 'function') {
throw new Error('MiniSignal#once(): First arg must be a Function.');
}
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, true, thisArg));
}
}, {
key: 'detach',
value: function detach(node) {
if (!(node instanceof MiniSignalBinding)) {
throw new Error('MiniSignal#detach(): First arg must be a MiniSignalBinding object.');
}
if (node._owner !== this) return this;
if (node._prev) node._prev._next = node._next;
if (node._next) node._next._prev = node._prev;
if (node === this._head) {
this._head = node._next;
if (node._next === null) {
this._tail = null;
}
} else if (node === this._tail) {
this._tail = node._prev;
this._tail._next = null;
}
node._owner = null;
return this;
}
}, {
key: 'detachAll',
value: function detachAll() {
var node = this._head;
if (!node) return this;
this._head = this._tail = null;
while (node) {
node._owner = null;
node = node._next;
}
return this;
}
}]);
return MiniSignal;
})();
MiniSignal.MiniSignalBinding = MiniSignalBinding;
exports['default'] = MiniSignal;
module.exports = exports['default'];