This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
404 lines (360 loc) · 8.63 KB
/
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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
/* eslint-disable class-methods-use-this */
/**
* Abstract class for the Cancellable classes
* @abstract
*/
class Cancellable {
/**
* @ignore
*/
constructor() {
/**
* @ignore
*/
this.events = {
cancel: [],
};
}
/**
* Returns true if the instance is cancelled.
* @abstract
* @returns {boolean}
*/
get cancelled() {
return false;
}
/**
* Cancels the instance.
* @abstract
* @returns {boolean}
*/
cancel() {
return false;
}
/**
* Registers a listener function to a target event dispatcher.
* @param {!string} ev
* Defines the event target
* - cancel: fires when the Cancellable is cancelled.
* @param {!function} listener
* a function to be called when the event is dispatched.
*/
addEventListener(ev, listener) {
if (typeof listener !== 'function') {
return;
}
const { events } = this;
if (this.cancelled) {
return;
}
if (ev in events) {
events[ev].push(listener);
}
}
/**
* Removes a listener function from a target event dispatcher.
* @param {!string} ev
* Defines the event target
* - cancel: fires when the Cancellable is cancelled.
* @param {!function} listener
* a function from be removed from the target event dispatcher.
*/
removeEventListener(ev, listener) {
if (typeof listener !== 'function') {
return;
}
const { events } = this;
if (this.cancelled) {
return;
}
if (ev in events) {
const index = events[ev].indexOf(listener);
if (index !== -1) {
events[ev].splice(index, 1);
}
}
}
}
/* eslint-disable class-methods-use-this */
/**
* A Cancellable class that is always cancelled and cannot be cancelled.
*/
class CancelledCancellable extends Cancellable {
/**
* Always returns true since this is a CancelledCancellable.
*/
get cancelled() {
return true;
}
/**
* Cancels the CancelledCancellable (empty)
*/
cancel() {
return false;
}
}
const CANCELLED = new CancelledCancellable();
/* eslint-disable class-methods-use-this */
/**
* A Cancellable class that is always uncancelled and cannot be cancelled.
*/
class UncancelledCancellable extends Cancellable {
/**
* Always returns true since this is a CancelledCancellable.
*/
get cancelled() {
return false;
}
/**
* Cancels the CancelledCancellable (empty)
*/
cancel() {
return false;
}
}
const UNCANCELLED = new UncancelledCancellable();
/**
* @ignore
*/
const dispatch = (s, e) => s.events[e].forEach(f => f.apply(s));
/**
* A simple Cancellable class that represents a boolean state.
*/
class BooleanCancellable extends Cancellable {
/**
* Creates a BooleanCancellable
*/
constructor() {
super();
/**
* @ignore
*/
this.state = UNCANCELLED;
}
/**
* Returns true if the instance is cancelled.
* @returns {boolean}
*/
get cancelled() {
return this.state.cancelled;
}
/**
* Cancels the instance
* @returns {boolean}
* Returns true if the cancel was successful.
*/
cancel() {
if (!this.cancelled) {
this.state = CANCELLED;
dispatch(this, 'cancel');
return true;
}
return false;
}
}
/* eslint-disable no-restricted-syntax */
/**
* A Cancellable class that allows composition of Cancellable instances.
*/
class CompositeCancellable extends Cancellable {
/**
* Creates a CompositeCancellable
*/
constructor() {
super();
/**
* @ignore
*/
this.buffer = [];
/**
* @ignore
*/
this.state = UNCANCELLED;
}
/**
* Returns true if the instance is cancelled.
* @returns {boolean}
*/
get cancelled() {
return this.state.cancelled;
}
/**
* Cancels the instances contained.
* @returns {boolean}
* Returns true if the cancel was successful.
*/
cancel() {
if (!this.cancelled) {
const { buffer } = this;
this.buffer = [];
for (const i of buffer) {
i.cancel();
}
this.state = CANCELLED;
dispatch(this, 'cancel');
return true;
}
return false;
}
/**
* Adds the given Cancellable into the composite.
* @param {Cancellable} cancellable
* The cancellable to be added to the composite.
* @returns {boolean}
*/
add(cancellable) {
if (cancellable instanceof Cancellable && cancellable !== this) {
if (this.cancelled) {
cancellable.cancel();
} else {
this.buffer.push(cancellable);
return true;
}
}
return false;
}
/**
* Removes the given Cancellable from the composite.
* @param {Cancellable} cancellable
* The cancellable to be removed from the composite.
* @returns {boolean}
*/
remove(cancellable) {
if (cancellable instanceof Cancellable && cancellable !== this) {
const { buffer } = this;
const index = buffer.indexOf(cancellable);
if (index !== -1) {
buffer.splice(index, 1);
return true;
}
}
return false;
}
}
/**
* A Cancellable class that allows linking on Cancellable instances.
*
* A LinkedCancellable will be disposed when the linked Cancellable
* instance is disposed and vice-versa
*/
class LinkedCancellable extends Cancellable {
/**
* Creates a LinkedCancellable
*/
constructor() {
super();
const bool = new BooleanCancellable();
/**
* @ignore
*/
this.origin = bool;
/**
* @ignore
*/
this.linked = bool;
}
/**
* Returns true if the instance is cancelled.
* @returns {boolean}
*/
get cancelled() {
return this.origin.cancelled;
}
/**
* Cancels the instances contained.
* @returns {boolean}
* Returns true if the cancel was successful.
*/
cancel() {
if (!this.cancelled) {
const { linked } = this;
const { origin } = this;
if (origin !== linked) {
this.unlink();
linked.cancel();
this.linked = origin;
}
origin.cancel();
dispatch(this, 'cancel');
return true;
}
return false;
}
/**
* Links to a Cancellable instance.
* @param {Cancellable} cancellable
* @returns {boolean}
* Returns true if the link was successful
*/
link(cancellable) {
if (cancellable instanceof Cancellable && cancellable !== this) {
if (this.cancelled) {
cancellable.cancel();
} else if (cancellable.cancelled) {
this.cancel();
} else {
this.unlink();
this.linked = cancellable;
const listener = () => this.cancel();
cancellable.addEventListener('cancel', listener);
this.listener = listener;
return true;
}
}
return false;
}
/**
* Unlinks this cancellable
* @returns {boolean}
* Returns true if the unlink is successful
*/
unlink() {
if (!this.cancelled) {
const { linked } = this;
const { origin } = this;
if (origin !== linked) {
const { listener } = this;
linked.removeEventListener('cancel', listener);
this.listener = null;
this.linked = origin;
return true;
}
}
return false;
}
}
/**
* @license
* MIT License
*
* Copyright (c) 2019 Alexis Munsayac
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
* @author Alexis Munsayac <[email protected]>
* @copyright Alexis Munsayac 2019
*/
exports.BooleanCancellable = BooleanCancellable;
exports.CANCELLED = CANCELLED;
exports.Cancellable = Cancellable;
exports.CompositeCancellable = CompositeCancellable;
exports.LinkedCancellable = LinkedCancellable;
exports.UNCANCELLED = UNCANCELLED;