-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (59 loc) · 5.11 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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.module = factory());
}(this, (function () { 'use strict';
function once(fn) {
if (!(fn instanceof Promise || typeof fn === 'function')) {
throw new Error('arguments "fn" must be async function');
}
var queue = [];
var running = false;
var result = null;
var runQueue = function runQueue() {
running = false;
queue.forEach(function (_ref) {
var resolve = _ref.resolve,
reject = _ref.reject;
result ? resolve(result) : reject();
});
queue.length = 0;
};
return function () {
for (var _len = arguments.length, props = Array(_len), _key = 0; _key < _len; _key++) {
props[_key] = arguments[_key];
}
return new Promise(function (resolve, reject) {
if (result !== null) {
resolve(result);
return;
}
if (running) {
queue.push({ resolve: resolve, reject: reject });
return;
}
running = true;
(Promise.prototype.isPrototypeOf(fn) ? fn : fn.apply(undefined, props)).then(function (data) {
result = data;
runQueue();
resolve(result);
}).catch(function (e) {
runQueue();
reject(e);
});
});
};
}
// Map
var fnMap = {};
once.onceMap = function (fName, fn) {
if (fName in fnMap) {
return fnMap[fName];
}
var newFn = once(fn);
fnMap[fName] = newFn;
return newFn;
};
return once;
})));
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjpudWxsLCJzb3VyY2VzIjpbInNyYy9pbmRleC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJmdW5jdGlvbiBvbmNlKGZuKSB7XG4gICAgaWYgKCAhKGZuIGluc3RhbmNlb2YgUHJvbWlzZSB8fCB0eXBlb2YgZm4gPT09ICdmdW5jdGlvbicpICkge1xuICAgICAgICB0aHJvdyBuZ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