forked from Hypercubed/mini-signals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.js
169 lines (135 loc) · 5.22 KB
/
browser.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.MiniSignal = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'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'];
},{}]},{},[1])(1)
});