Last change
on this file since 84d0fbb was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | "use strict";
|
---|
| 2 | function noop() { }
|
---|
| 3 | function once(emitter, name) {
|
---|
| 4 | const o = once.spread(emitter, name);
|
---|
| 5 | const r = o.then((args) => args[0]);
|
---|
| 6 | r.cancel = o.cancel;
|
---|
| 7 | return r;
|
---|
| 8 | }
|
---|
| 9 | (function (once) {
|
---|
| 10 | function spread(emitter, name) {
|
---|
| 11 | let c = null;
|
---|
| 12 | const p = new Promise((resolve, reject) => {
|
---|
| 13 | function cancel() {
|
---|
| 14 | emitter.removeListener(name, onEvent);
|
---|
| 15 | emitter.removeListener('error', onError);
|
---|
| 16 | p.cancel = noop;
|
---|
| 17 | }
|
---|
| 18 | function onEvent(...args) {
|
---|
| 19 | cancel();
|
---|
| 20 | resolve(args);
|
---|
| 21 | }
|
---|
| 22 | function onError(err) {
|
---|
| 23 | cancel();
|
---|
| 24 | reject(err);
|
---|
| 25 | }
|
---|
| 26 | c = cancel;
|
---|
| 27 | emitter.on(name, onEvent);
|
---|
| 28 | emitter.on('error', onError);
|
---|
| 29 | });
|
---|
| 30 | if (!c) {
|
---|
| 31 | throw new TypeError('Could not get `cancel()` function');
|
---|
| 32 | }
|
---|
| 33 | p.cancel = c;
|
---|
| 34 | return p;
|
---|
| 35 | }
|
---|
| 36 | once.spread = spread;
|
---|
| 37 | })(once || (once = {}));
|
---|
| 38 | module.exports = once;
|
---|
| 39 | //# sourceMappingURL=index.js.map |
---|
Note:
See
TracBrowser
for help on using the repository browser.