mini-semaphore 1.3.3 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/core.js +4 -2
- package/cjs/deque.js +0 -12
- package/cjs/extras.js +2 -1
- package/cjs/flow-restrictor.js +18 -18
- package/cjs/index.d.ts +4 -4
- package/cjs/index.js +1 -0
- package/cjs/object.js +2 -1
- package/esm/deque.js +0 -12
- package/esm/flow-restrictor.js +18 -18
- package/esm/index.d.ts +4 -4
- package/index.d.ts +4 -4
- package/package.json +1 -1
- package/umd/index.d.ts +4 -4
- package/umd/index.js +89 -123
- package/umd/index.js.map +1 -1
- package/webpack/index.d.ts +4 -4
- package/webpack/index.js +87 -121
- package/webpack/index.js.map +1 -1
package/cjs/core.js
CHANGED
|
@@ -10,7 +10,7 @@ const box = (z, r) => {
|
|
|
10
10
|
z.q.push(r);
|
|
11
11
|
}
|
|
12
12
|
};
|
|
13
|
-
|
|
13
|
+
const acquire = (dis, lazy = true) => {
|
|
14
14
|
return new Promise(r => {
|
|
15
15
|
if (!lazy) {
|
|
16
16
|
box(dis, r);
|
|
@@ -20,7 +20,8 @@ exports.acquire = (dis, lazy = true) => {
|
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
22
|
};
|
|
23
|
-
exports.
|
|
23
|
+
exports.acquire = acquire;
|
|
24
|
+
const release = (dis) => {
|
|
24
25
|
dis.capacity++;
|
|
25
26
|
if (dis.q.length) {
|
|
26
27
|
dis.capacity -= 1, (dis.q.shift() || extras_1.THROW)();
|
|
@@ -30,3 +31,4 @@ exports.release = (dis) => {
|
|
|
30
31
|
dis.capacity = dis.limit;
|
|
31
32
|
}
|
|
32
33
|
};
|
|
34
|
+
exports.release = release;
|
package/cjs/deque.js
CHANGED
|
@@ -35,7 +35,6 @@ class Deque {
|
|
|
35
35
|
const i = (this._f + l) & (this._c - 1);
|
|
36
36
|
this._a[i] = s;
|
|
37
37
|
this._l = l + 1;
|
|
38
|
-
return l + 1;
|
|
39
38
|
}
|
|
40
39
|
shift() {
|
|
41
40
|
const l = this._l;
|
|
@@ -49,17 +48,6 @@ class Deque {
|
|
|
49
48
|
this._l = l - 1;
|
|
50
49
|
return r;
|
|
51
50
|
}
|
|
52
|
-
clear() {
|
|
53
|
-
const l = this._l;
|
|
54
|
-
const f = this._f;
|
|
55
|
-
const c = this._c;
|
|
56
|
-
const a = this._a;
|
|
57
|
-
for (let j = 0; j < l; ++j) {
|
|
58
|
-
a[(f + j) & (c - 1)] = void 0;
|
|
59
|
-
}
|
|
60
|
-
this._l = 0;
|
|
61
|
-
this._f = 0;
|
|
62
|
-
}
|
|
63
51
|
get length() {
|
|
64
52
|
return this._l;
|
|
65
53
|
}
|
package/cjs/extras.js
CHANGED
package/cjs/flow-restrictor.js
CHANGED
|
@@ -2,24 +2,24 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.restrictor = void 0;
|
|
4
4
|
const c = require("./class");
|
|
5
|
-
const { MiniSemaphore: MS } = c;
|
|
6
|
-
const internalLock = new MS(1);
|
|
7
|
-
let locks = Object.create(null);
|
|
8
|
-
const get = async (key, restriction) => {
|
|
9
|
-
await internalLock.acquire(false);
|
|
10
|
-
let lock = locks[key];
|
|
11
|
-
if (!lock) {
|
|
12
|
-
locks[key] = lock = new MS(restriction);
|
|
13
|
-
}
|
|
14
|
-
if (lock.limit !== restriction) {
|
|
15
|
-
internalLock.release();
|
|
16
|
-
throw new ReferenceError(`Cannot get object with different restriction: key: '${key}', lock.limit: ${lock.limit} <-> restriction: ${restriction},`);
|
|
17
|
-
}
|
|
18
|
-
internalLock.release();
|
|
19
|
-
return lock;
|
|
20
|
-
};
|
|
21
5
|
var restrictor;
|
|
22
6
|
(function (restrictor) {
|
|
7
|
+
const { MiniSemaphore: MS } = c;
|
|
8
|
+
const internalLock = new MS(1);
|
|
9
|
+
let locks = Object.create(null);
|
|
10
|
+
const get = async (key, restriction) => {
|
|
11
|
+
await internalLock.acquire(false);
|
|
12
|
+
let lock = locks[key];
|
|
13
|
+
if (!lock) {
|
|
14
|
+
locks[key] = lock = new MS(restriction);
|
|
15
|
+
}
|
|
16
|
+
if (lock.limit !== restriction) {
|
|
17
|
+
internalLock.release();
|
|
18
|
+
throw new ReferenceError(`Cannot get object with different restriction: key: '${key}', lock.limit: ${lock.limit} <-> restriction: ${restriction},`);
|
|
19
|
+
}
|
|
20
|
+
internalLock.release();
|
|
21
|
+
return lock;
|
|
22
|
+
};
|
|
23
23
|
restrictor.getLockByKey = async (key) => {
|
|
24
24
|
await internalLock.acquire(false);
|
|
25
25
|
const l = locks[key];
|
|
@@ -53,9 +53,9 @@ var restrictor;
|
|
|
53
53
|
locks = newLocks;
|
|
54
54
|
internalLock.release();
|
|
55
55
|
if (debug) {
|
|
56
|
-
console.log(`
|
|
56
|
+
console.log(`eliminated: [\n${eliminatedKeys.join(",\n")}\n]` +
|
|
57
57
|
"\n" +
|
|
58
|
-
`lived:
|
|
58
|
+
`lived: [\n${Object.keys(newLocks).join(",\n")}\n]`);
|
|
59
59
|
}
|
|
60
60
|
return eliminatedCount;
|
|
61
61
|
};
|
package/cjs/index.d.ts
CHANGED
|
@@ -32,8 +32,8 @@ export declare class Deque<T extends any> {
|
|
|
32
32
|
/**
|
|
33
33
|
* @param s subject
|
|
34
34
|
*/
|
|
35
|
-
push(s: T):
|
|
36
|
-
shift(): T | undefined;
|
|
35
|
+
push(s: T): void;
|
|
36
|
+
// shift(): T | undefined;
|
|
37
37
|
clear(): void;
|
|
38
38
|
get length(): number;
|
|
39
39
|
}
|
|
@@ -94,8 +94,8 @@ export declare type TFlowableLock<T = TVoidFunction> = IFlowableLock & {
|
|
|
94
94
|
readonly q: Deque<T>;
|
|
95
95
|
};
|
|
96
96
|
export declare type TVoidFunction = () => void;
|
|
97
|
-
export declare const acquire: (dis: TFlowableLock<TVoidFunction>, lazy?: boolean) => Promise<void>;
|
|
98
|
-
export declare const release: (dis: TFlowableLock<TVoidFunction>) => void;
|
|
97
|
+
// export declare const acquire: (dis: TFlowableLock<TVoidFunction>, lazy?: boolean) => Promise<void>;
|
|
98
|
+
// export declare const release: (dis: TFlowableLock<TVoidFunction>) => void;
|
|
99
99
|
|
|
100
100
|
|
|
101
101
|
/**
|
package/cjs/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.restrictor = exports.Deque = exports.create = exports.MiniSemaphore = void 0;
|
|
3
4
|
var class_1 = require("./class");
|
|
4
5
|
Object.defineProperty(exports, "MiniSemaphore", { enumerable: true, get: function () { return class_1.MiniSemaphore; } });
|
|
5
6
|
var object_1 = require("./object");
|
package/cjs/object.js
CHANGED
|
@@ -5,7 +5,7 @@ const core = require("./core");
|
|
|
5
5
|
const deque_1 = require("./deque");
|
|
6
6
|
const a = core.acquire;
|
|
7
7
|
const r = core.release;
|
|
8
|
-
|
|
8
|
+
const create = (capacity) => {
|
|
9
9
|
return {
|
|
10
10
|
capacity,
|
|
11
11
|
limit: capacity,
|
|
@@ -33,3 +33,4 @@ exports.create = (capacity) => {
|
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
};
|
|
36
|
+
exports.create = create;
|
package/esm/deque.js
CHANGED
|
@@ -32,7 +32,6 @@ export class Deque {
|
|
|
32
32
|
const i = (this._f + l) & (this._c - 1);
|
|
33
33
|
this._a[i] = s;
|
|
34
34
|
this._l = l + 1;
|
|
35
|
-
return l + 1;
|
|
36
35
|
}
|
|
37
36
|
shift() {
|
|
38
37
|
const l = this._l;
|
|
@@ -46,17 +45,6 @@ export class Deque {
|
|
|
46
45
|
this._l = l - 1;
|
|
47
46
|
return r;
|
|
48
47
|
}
|
|
49
|
-
clear() {
|
|
50
|
-
const l = this._l;
|
|
51
|
-
const f = this._f;
|
|
52
|
-
const c = this._c;
|
|
53
|
-
const a = this._a;
|
|
54
|
-
for (let j = 0; j < l; ++j) {
|
|
55
|
-
a[(f + j) & (c - 1)] = void 0;
|
|
56
|
-
}
|
|
57
|
-
this._l = 0;
|
|
58
|
-
this._f = 0;
|
|
59
|
-
}
|
|
60
48
|
get length() {
|
|
61
49
|
return this._l;
|
|
62
50
|
}
|
package/esm/flow-restrictor.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import * as c from "./class";
|
|
2
|
-
const { MiniSemaphore: MS } = c;
|
|
3
|
-
const internalLock = new MS(1);
|
|
4
|
-
let locks = Object.create(null);
|
|
5
|
-
const get = async (key, restriction) => {
|
|
6
|
-
await internalLock.acquire(false);
|
|
7
|
-
let lock = locks[key];
|
|
8
|
-
if (!lock) {
|
|
9
|
-
locks[key] = lock = new MS(restriction);
|
|
10
|
-
}
|
|
11
|
-
if (lock.limit !== restriction) {
|
|
12
|
-
internalLock.release();
|
|
13
|
-
throw new ReferenceError(`Cannot get object with different restriction: key: '${key}', lock.limit: ${lock.limit} <-> restriction: ${restriction},`);
|
|
14
|
-
}
|
|
15
|
-
internalLock.release();
|
|
16
|
-
return lock;
|
|
17
|
-
};
|
|
18
2
|
export var restrictor;
|
|
19
3
|
(function (restrictor) {
|
|
4
|
+
const { MiniSemaphore: MS } = c;
|
|
5
|
+
const internalLock = new MS(1);
|
|
6
|
+
let locks = Object.create(null);
|
|
7
|
+
const get = async (key, restriction) => {
|
|
8
|
+
await internalLock.acquire(false);
|
|
9
|
+
let lock = locks[key];
|
|
10
|
+
if (!lock) {
|
|
11
|
+
locks[key] = lock = new MS(restriction);
|
|
12
|
+
}
|
|
13
|
+
if (lock.limit !== restriction) {
|
|
14
|
+
internalLock.release();
|
|
15
|
+
throw new ReferenceError(`Cannot get object with different restriction: key: '${key}', lock.limit: ${lock.limit} <-> restriction: ${restriction},`);
|
|
16
|
+
}
|
|
17
|
+
internalLock.release();
|
|
18
|
+
return lock;
|
|
19
|
+
};
|
|
20
20
|
restrictor.getLockByKey = async (key) => {
|
|
21
21
|
await internalLock.acquire(false);
|
|
22
22
|
const l = locks[key];
|
|
@@ -50,9 +50,9 @@ export var restrictor;
|
|
|
50
50
|
locks = newLocks;
|
|
51
51
|
internalLock.release();
|
|
52
52
|
if (debug) {
|
|
53
|
-
console.log(`
|
|
53
|
+
console.log(`eliminated: [\n${eliminatedKeys.join(",\n")}\n]` +
|
|
54
54
|
"\n" +
|
|
55
|
-
`lived:
|
|
55
|
+
`lived: [\n${Object.keys(newLocks).join(",\n")}\n]`);
|
|
56
56
|
}
|
|
57
57
|
return eliminatedCount;
|
|
58
58
|
};
|
package/esm/index.d.ts
CHANGED
|
@@ -32,8 +32,8 @@ export declare class Deque<T extends any> {
|
|
|
32
32
|
/**
|
|
33
33
|
* @param s subject
|
|
34
34
|
*/
|
|
35
|
-
push(s: T):
|
|
36
|
-
shift(): T | undefined;
|
|
35
|
+
push(s: T): void;
|
|
36
|
+
// shift(): T | undefined;
|
|
37
37
|
clear(): void;
|
|
38
38
|
get length(): number;
|
|
39
39
|
}
|
|
@@ -94,8 +94,8 @@ export declare type TFlowableLock<T = TVoidFunction> = IFlowableLock & {
|
|
|
94
94
|
readonly q: Deque<T>;
|
|
95
95
|
};
|
|
96
96
|
export declare type TVoidFunction = () => void;
|
|
97
|
-
export declare const acquire: (dis: TFlowableLock<TVoidFunction>, lazy?: boolean) => Promise<void>;
|
|
98
|
-
export declare const release: (dis: TFlowableLock<TVoidFunction>) => void;
|
|
97
|
+
// export declare const acquire: (dis: TFlowableLock<TVoidFunction>, lazy?: boolean) => Promise<void>;
|
|
98
|
+
// export declare const release: (dis: TFlowableLock<TVoidFunction>) => void;
|
|
99
99
|
|
|
100
100
|
|
|
101
101
|
/**
|
package/index.d.ts
CHANGED
|
@@ -32,8 +32,8 @@ export declare class Deque<T extends any> {
|
|
|
32
32
|
/**
|
|
33
33
|
* @param s subject
|
|
34
34
|
*/
|
|
35
|
-
push(s: T):
|
|
36
|
-
shift(): T | undefined;
|
|
35
|
+
push(s: T): void;
|
|
36
|
+
// shift(): T | undefined;
|
|
37
37
|
clear(): void;
|
|
38
38
|
get length(): number;
|
|
39
39
|
}
|
|
@@ -94,8 +94,8 @@ export declare type TFlowableLock<T = TVoidFunction> = IFlowableLock & {
|
|
|
94
94
|
readonly q: Deque<T>;
|
|
95
95
|
};
|
|
96
96
|
export declare type TVoidFunction = () => void;
|
|
97
|
-
export declare const acquire: (dis: TFlowableLock<TVoidFunction>, lazy?: boolean) => Promise<void>;
|
|
98
|
-
export declare const release: (dis: TFlowableLock<TVoidFunction>) => void;
|
|
97
|
+
// export declare const acquire: (dis: TFlowableLock<TVoidFunction>, lazy?: boolean) => Promise<void>;
|
|
98
|
+
// export declare const release: (dis: TFlowableLock<TVoidFunction>) => void;
|
|
99
99
|
|
|
100
100
|
|
|
101
101
|
/**
|
package/package.json
CHANGED
package/umd/index.d.ts
CHANGED
|
@@ -32,8 +32,8 @@ export declare class Deque<T extends any> {
|
|
|
32
32
|
/**
|
|
33
33
|
* @param s subject
|
|
34
34
|
*/
|
|
35
|
-
push(s: T):
|
|
36
|
-
shift(): T | undefined;
|
|
35
|
+
push(s: T): void;
|
|
36
|
+
// shift(): T | undefined;
|
|
37
37
|
clear(): void;
|
|
38
38
|
get length(): number;
|
|
39
39
|
}
|
|
@@ -94,8 +94,8 @@ export declare type TFlowableLock<T = TVoidFunction> = IFlowableLock & {
|
|
|
94
94
|
readonly q: Deque<T>;
|
|
95
95
|
};
|
|
96
96
|
export declare type TVoidFunction = () => void;
|
|
97
|
-
export declare const acquire: (dis: TFlowableLock<TVoidFunction>, lazy?: boolean) => Promise<void>;
|
|
98
|
-
export declare const release: (dis: TFlowableLock<TVoidFunction>) => void;
|
|
97
|
+
// export declare const acquire: (dis: TFlowableLock<TVoidFunction>, lazy?: boolean) => Promise<void>;
|
|
98
|
+
// export declare const release: (dis: TFlowableLock<TVoidFunction>) => void;
|
|
99
99
|
|
|
100
100
|
|
|
101
101
|
/**
|
package/umd/index.js
CHANGED
|
@@ -1,120 +1,85 @@
|
|
|
1
1
|
!function(t, e) {
|
|
2
2
|
'object' == typeof exports && 'object' == typeof module ? module.exports = e() : 'function' == typeof define && define.amd ? define([], e) : 'object' == typeof exports ? exports.MiniSema = e() : t.MiniSema = e();
|
|
3
|
-
}(
|
|
4
|
-
return
|
|
5
|
-
var e = {};
|
|
6
|
-
function n(i) {
|
|
7
|
-
if (e[i]) return e[i].exports;
|
|
8
|
-
var r = e[i] = {
|
|
9
|
-
i: i,
|
|
10
|
-
l: !1,
|
|
11
|
-
exports: {}
|
|
12
|
-
};
|
|
13
|
-
return t[i].call(r.exports, r, r.exports, n), r.l = !0, r.exports;
|
|
14
|
-
}
|
|
15
|
-
return n.m = t, n.c = e, n.d = function(t, e, i) {
|
|
16
|
-
n.o(t, e) || Object.defineProperty(t, e, {
|
|
17
|
-
enumerable: !0,
|
|
18
|
-
get: i
|
|
19
|
-
});
|
|
20
|
-
}, n.r = function(t) {
|
|
21
|
-
'undefined' != typeof Symbol && Symbol.toStringTag && Object.defineProperty(t, Symbol.toStringTag, {
|
|
22
|
-
value: 'Module'
|
|
23
|
-
}), Object.defineProperty(t, '__esModule', {
|
|
24
|
-
value: !0
|
|
25
|
-
});
|
|
26
|
-
}, n.t = function(t, e) {
|
|
27
|
-
if (1 & e && (t = n(t)), 8 & e) return t;
|
|
28
|
-
if (4 & e && 'object' == typeof t && t && t.__esModule) return t;
|
|
29
|
-
var i = Object.create(null);
|
|
30
|
-
if (n.r(i), Object.defineProperty(i, 'default', {
|
|
31
|
-
enumerable: !0,
|
|
32
|
-
value: t
|
|
33
|
-
}), 2 & e && 'string' != typeof t) for (var r in t) n.d(i, r, function(e) {
|
|
34
|
-
return t[e];
|
|
35
|
-
}.bind(null, r));
|
|
36
|
-
return i;
|
|
37
|
-
}, n.n = function(t) {
|
|
38
|
-
var e = t && t.__esModule ? function() {
|
|
39
|
-
return t.default;
|
|
40
|
-
} : function() {
|
|
41
|
-
return t;
|
|
42
|
-
};
|
|
43
|
-
return n.d(e, 'a', e), e;
|
|
44
|
-
}, n.o = function(t, e) {
|
|
45
|
-
return Object.prototype.hasOwnProperty.call(t, e);
|
|
46
|
-
}, n.p = "", n(n.s = 0);
|
|
47
|
-
}([ function(t, e, n) {
|
|
3
|
+
}(self, (function() {
|
|
4
|
+
return (() => {
|
|
48
5
|
"use strict";
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
6
|
+
var t = {
|
|
7
|
+
d: (e, i) => {
|
|
8
|
+
for (var n in i) t.o(i, n) && !t.o(e, n) && Object.defineProperty(e, n, {
|
|
9
|
+
enumerable: !0,
|
|
10
|
+
get: i[n]
|
|
11
|
+
});
|
|
12
|
+
},
|
|
13
|
+
o: (t, e) => Object.prototype.hasOwnProperty.call(t, e),
|
|
14
|
+
r: t => {
|
|
15
|
+
'undefined' != typeof Symbol && Symbol.toStringTag && Object.defineProperty(t, Symbol.toStringTag, {
|
|
16
|
+
value: 'Module'
|
|
17
|
+
}), Object.defineProperty(t, '__esModule', {
|
|
18
|
+
value: !0
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}, e = {};
|
|
22
|
+
t.r(e), t.d(e, {
|
|
23
|
+
Deque: () => o,
|
|
24
|
+
MiniSemaphore: () => f,
|
|
25
|
+
create: () => m,
|
|
26
|
+
restrictor: () => _
|
|
27
|
+
});
|
|
58
28
|
var i = {};
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
})
|
|
62
|
-
const
|
|
29
|
+
t.r(i), t.d(i, {
|
|
30
|
+
MiniSemaphore: () => f
|
|
31
|
+
});
|
|
32
|
+
const n = () => {
|
|
63
33
|
throw new Error("mini-semaphore: inconsistent occurred");
|
|
64
|
-
},
|
|
34
|
+
}, r = (t, e) => {
|
|
65
35
|
t.capacity > 0 ? (t.capacity--, e()) : t.q.push(e);
|
|
66
|
-
},
|
|
67
|
-
e ? setTimeout(() =>
|
|
68
|
-
}),
|
|
69
|
-
t.capacity++, t.q.length && (t.capacity -= 1, (t.q.shift() ||
|
|
36
|
+
}, s = (t, e = !0) => new Promise((i => {
|
|
37
|
+
e ? setTimeout((() => r(t, i)), 4) : r(t, i);
|
|
38
|
+
})), a = t => {
|
|
39
|
+
t.capacity++, t.q.length && (t.capacity -= 1, (t.q.shift() || n)()), t.capacity > t.limit && (console.warn("inconsistent release!"),
|
|
70
40
|
t.capacity = t.limit);
|
|
71
|
-
},
|
|
72
|
-
(t |= t >> 16)
|
|
73
|
-
class
|
|
41
|
+
}, c = t => (t => (t >>>= 0, t -= 1, t |= t >> 1, t |= t >> 2, t |= t >> 4, t |= t >> 8,
|
|
42
|
+
1 + (t |= t >> 16)))(Math.min(Math.max(16, 0 | t), 1073741824));
|
|
43
|
+
class o {
|
|
74
44
|
constructor(t) {
|
|
75
|
-
this._c =
|
|
45
|
+
this._c = c(t), this._l = 0, this._f = 0, this._a = [];
|
|
76
46
|
}
|
|
77
47
|
push(t) {
|
|
78
48
|
const e = this._l;
|
|
79
|
-
this._c < e + 1 && l(this,
|
|
80
|
-
const
|
|
81
|
-
|
|
49
|
+
this._c < e + 1 && l(this, c(1.5 * this._c + 16));
|
|
50
|
+
const i = this._f + e & this._c - 1;
|
|
51
|
+
this._a[i] = t, this._l = e + 1;
|
|
82
52
|
}
|
|
83
53
|
shift() {
|
|
84
54
|
const t = this._l;
|
|
85
55
|
if (0 === t) return;
|
|
86
|
-
const e = this._f,
|
|
87
|
-
return this._a[e] = void 0, this._f = e + 1 & this._c - 1, this._l = t - 1,
|
|
88
|
-
}
|
|
89
|
-
clear() {
|
|
90
|
-
const t = this._l, e = this._f, n = this._c, i = this._a;
|
|
91
|
-
for (let r = 0; r < t; ++r) i[e + r & n - 1] = void 0;
|
|
92
|
-
this._l = 0, this._f = 0;
|
|
56
|
+
const e = this._f, i = this._a[e];
|
|
57
|
+
return this._a[e] = void 0, this._f = e + 1 & this._c - 1, this._l = t - 1, i;
|
|
93
58
|
}
|
|
94
59
|
get length() {
|
|
95
60
|
return this._l;
|
|
96
61
|
}
|
|
97
62
|
}
|
|
98
63
|
const l = (t, e) => {
|
|
99
|
-
const
|
|
64
|
+
const i = t._c;
|
|
100
65
|
t._c = e;
|
|
101
|
-
const
|
|
102
|
-
if (
|
|
103
|
-
const e =
|
|
104
|
-
((t, e,
|
|
105
|
-
for (let
|
|
106
|
-
})(t._a, 0, t._a,
|
|
66
|
+
const n = t._f, r = t._l;
|
|
67
|
+
if (n + r > i) {
|
|
68
|
+
const e = n + r & i - 1;
|
|
69
|
+
((t, e, i, n, r) => {
|
|
70
|
+
for (let s = 0; s < r; ++s) i[s + n] = t[s + e], t[s + e] = void 0;
|
|
71
|
+
})(t._a, 0, t._a, i, e);
|
|
107
72
|
}
|
|
108
|
-
},
|
|
109
|
-
class
|
|
73
|
+
}, h = s, u = a;
|
|
74
|
+
class f {
|
|
110
75
|
constructor(t) {
|
|
111
|
-
this.limit = this.capacity = t, this.q = new
|
|
76
|
+
this.limit = this.capacity = t, this.q = new o(t);
|
|
112
77
|
}
|
|
113
78
|
acquire(t) {
|
|
114
|
-
return
|
|
79
|
+
return h(this, t);
|
|
115
80
|
}
|
|
116
81
|
release() {
|
|
117
|
-
|
|
82
|
+
u(this);
|
|
118
83
|
}
|
|
119
84
|
setRestriction(t) {
|
|
120
85
|
this.limit = this.capacity = t;
|
|
@@ -123,23 +88,23 @@
|
|
|
123
88
|
return this.q.length;
|
|
124
89
|
}
|
|
125
90
|
async flow(t, e) {
|
|
126
|
-
await
|
|
91
|
+
await h(this, e);
|
|
127
92
|
try {
|
|
128
93
|
return await t();
|
|
129
94
|
} finally {
|
|
130
|
-
|
|
95
|
+
u(this);
|
|
131
96
|
}
|
|
132
97
|
}
|
|
133
98
|
}
|
|
134
|
-
const y =
|
|
99
|
+
const y = s, p = a, m = t => ({
|
|
135
100
|
capacity: t,
|
|
136
101
|
limit: t,
|
|
137
|
-
q: new
|
|
102
|
+
q: new o(t),
|
|
138
103
|
acquire(t) {
|
|
139
104
|
return y(this, t);
|
|
140
105
|
},
|
|
141
106
|
release() {
|
|
142
|
-
|
|
107
|
+
p(this);
|
|
143
108
|
},
|
|
144
109
|
setRestriction(t) {
|
|
145
110
|
this.limit = this.capacity = t;
|
|
@@ -152,41 +117,42 @@
|
|
|
152
117
|
try {
|
|
153
118
|
return await t();
|
|
154
119
|
} finally {
|
|
155
|
-
|
|
120
|
+
p(this);
|
|
156
121
|
}
|
|
157
122
|
}
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
123
|
+
});
|
|
124
|
+
var _;
|
|
125
|
+
return function(t) {
|
|
126
|
+
const {MiniSemaphore: e} = i, n = new e(1);
|
|
127
|
+
let r = Object.create(null);
|
|
128
|
+
async function s(t, i, s) {
|
|
129
|
+
const a = await (async (t, i) => {
|
|
130
|
+
await n.acquire(!1);
|
|
131
|
+
let s = r[t];
|
|
132
|
+
if (s || (r[t] = s = new e(i)), s.limit !== i) throw n.release(), new ReferenceError(`Cannot get object with different restriction: key: '${t}', lock.limit: ${s.limit} <-> restriction: ${i},`);
|
|
133
|
+
return n.release(), s;
|
|
134
|
+
})(t, i), c = a.flow(s);
|
|
135
|
+
return a.last = Date.now(), c;
|
|
170
136
|
}
|
|
171
137
|
t.getLockByKey = async t => {
|
|
172
|
-
await
|
|
173
|
-
const e =
|
|
174
|
-
return
|
|
138
|
+
await n.acquire(!1);
|
|
139
|
+
const e = r[t];
|
|
140
|
+
return n.release(), e;
|
|
175
141
|
}, t.cleanup = async (t, e) => {
|
|
176
|
-
await
|
|
177
|
-
const
|
|
178
|
-
let
|
|
179
|
-
!t && (t = 1), t *= 1e3, e && (
|
|
180
|
-
for (let
|
|
181
|
-
const
|
|
182
|
-
|
|
142
|
+
await n.acquire(!1);
|
|
143
|
+
const i = r, s = Object.create(null), a = Object.keys(i);
|
|
144
|
+
let c, o = 0;
|
|
145
|
+
!t && (t = 1), t *= 1e3, e && (c = []);
|
|
146
|
+
for (let n = 0, r = a.length; n < r; ) {
|
|
147
|
+
const r = a[n++], l = i[r];
|
|
148
|
+
l.last && Date.now() - l.last >= t ? (o++, e && c.push(r)) : s[r] = l;
|
|
183
149
|
}
|
|
184
|
-
return
|
|
185
|
-
|
|
186
|
-
}, t.multi =
|
|
187
|
-
return
|
|
150
|
+
return r = s, n.release(), e && console.log(`eliminated: [\n${c.join(",\n")}\n]\nlived: [\n${Object.keys(s).join(",\n")}\n]`),
|
|
151
|
+
o;
|
|
152
|
+
}, t.multi = s, t.one = async function(t, e) {
|
|
153
|
+
return s(t, 1, e);
|
|
188
154
|
};
|
|
189
|
-
}(
|
|
190
|
-
}
|
|
155
|
+
}(_ || (_ = {})), e;
|
|
156
|
+
})();
|
|
191
157
|
}));
|
|
192
158
|
//# sourceMappingURL=index.js.map
|
package/umd/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["webpack://MiniSema/
|
|
1
|
+
{"version":3,"file":"index.js","mappings":"AAAA;AACA;AADA;AAUA;;ACTA;ACAA;AACA;AAEA;AAAA;;;ACJA;ACCA;AACA;AACA;AAEA;AAAA;;;;;;;;;;;;;;ACLA;AACA;ACAA;AACA;AAOA;AAEA;AAQA;AACA;AAMA;ACVA;AAFA;AAKA;AACA;AACA;;AAKA;AACA;AACA;AAGA;AACA;;AAGA;AACA;AACA;AAGA;AAKA;;AAEA;AACA;;;AAGA;AACA;AACA;AACA;AAEA;AACA;AAzDA;AACA;AAyDA;;ACxDA;AAEA;AACA;AACA;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AAEA;AACA;;;;ACzBA;AAIA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AAEA;AACA;;;;AC1BA;;AAEA;AAEA;AAqDA;AACA;AApDA;AACA;AAIA;AAKA;AA0CA;AAGA;;AA3CA;AACA;AACA;AAEA;AAEA;AACA;AACA;AAGA;AAEA;AAKA;AACA;AAEA;;AAgBA;AAAA;AAQA;AAEA;;AAhEA;AVQA","sources":["webpack://MiniSema/webpack/universalModuleDefinition","webpack://MiniSema/webpack/bootstrap","webpack://MiniSema/webpack/runtime/define property getters","webpack://MiniSema/webpack/runtime/hasOwnProperty shorthand","webpack://MiniSema/webpack/runtime/make namespace object","webpack://MiniSema/./src/extras.ts","webpack://MiniSema/./src/core.ts","webpack://MiniSema/./src/deque.ts","webpack://MiniSema/./src/class.ts","webpack://MiniSema/./src/object.ts","webpack://MiniSema/./src/flow-restrictor.ts"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"MiniSema\"] = factory();\n\telse\n\t\troot[\"MiniSema\"] = factory();\n})(self, function() {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","export const THROW = () => {\r\n throw new Error(\"mini-semaphore: inconsistent occurred\");\r\n};\r\n","import { THROW } from \"./extras\";\r\nconst box = (z, r) => {\r\n if (z.capacity > 0) {\r\n z.capacity--, r();\r\n }\r\n else {\r\n z.q.push(r);\r\n }\r\n};\r\nexport const acquire = (dis, lazy = true) => {\r\n return new Promise(r => {\r\n if (!lazy) {\r\n box(dis, r);\r\n }\r\n else {\r\n setTimeout(() => box(dis, r), 4);\r\n }\r\n });\r\n};\r\nexport const release = (dis) => {\r\n dis.capacity++;\r\n if (dis.q.length) {\r\n dis.capacity -= 1, (dis.q.shift() || THROW)();\r\n }\r\n if (dis.capacity > dis.limit) {\r\n console.warn(\"inconsistent release!\");\r\n dis.capacity = dis.limit;\r\n }\r\n};\r\n","const am = (src, si, dst, di, len) => {\r\n for (let j = 0; j < len; ++j) {\r\n dst[j + di] = src[j + si];\r\n src[j + si] = void 0;\r\n }\r\n};\r\nconst p2l = (n) => {\r\n n = n >>> 0;\r\n n = n - 1;\r\n n = n | (n >> 1);\r\n n = n | (n >> 2);\r\n n = n | (n >> 4);\r\n n = n | (n >> 8);\r\n n = n | (n >> 16);\r\n return n + 1;\r\n};\r\nconst gc = (n) => {\r\n return p2l(Math.min(Math.max(16, n | 0), 1073741824));\r\n};\r\nexport class Deque {\r\n constructor(ic) {\r\n this._c = gc(ic);\r\n this._l = 0;\r\n this._f = 0;\r\n this._a = [];\r\n }\r\n push(s) {\r\n const l = this._l;\r\n if (this._c < l + 1) {\r\n rt(this, gc(this._c * 1.5 + 16));\r\n }\r\n const i = (this._f + l) & (this._c - 1);\r\n this._a[i] = s;\r\n this._l = l + 1;\r\n }\r\n shift() {\r\n const l = this._l;\r\n if (l === 0) {\r\n return void 0;\r\n }\r\n const f = this._f;\r\n const r = this._a[f];\r\n this._a[f] = void 0;\r\n this._f = (f + 1) & (this._c - 1);\r\n this._l = l - 1;\r\n return r;\r\n }\r\n get length() {\r\n return this._l;\r\n }\r\n}\r\nconst rt = (dis, n) => {\r\n const oc = dis._c;\r\n dis._c = n;\r\n const f = dis._f;\r\n const l = dis._l;\r\n if (f + l > oc) {\r\n const mc = (f + l) & (oc - 1);\r\n am(dis._a, 0, dis._a, oc, mc);\r\n }\r\n};\r\n","import * as core from \"./core\";\r\nimport { Deque } from \"./deque\";\r\nconst a = core.acquire;\r\nconst r = core.release;\r\nexport class MiniSemaphore {\r\n constructor(capacity) {\r\n this.limit = this.capacity = capacity;\r\n this.q = new Deque(capacity);\r\n }\r\n acquire(lazy) {\r\n return a(this, lazy);\r\n }\r\n release() {\r\n r(this);\r\n }\r\n setRestriction(restriction) {\r\n this.limit = this.capacity = restriction;\r\n }\r\n get pending() {\r\n return this.q.length;\r\n }\r\n async flow(process, lazy) {\r\n await a(this, lazy);\r\n try {\r\n return await process();\r\n }\r\n finally {\r\n r(this);\r\n }\r\n }\r\n}\r\n","import * as core from \"./core\";\r\nimport { Deque } from \"./deque\";\r\nconst a = core.acquire;\r\nconst r = core.release;\r\nexport const create = (capacity) => {\r\n return {\r\n capacity,\r\n limit: capacity,\r\n q: new Deque(capacity),\r\n acquire(lazy) {\r\n return a(this, lazy);\r\n },\r\n release() {\r\n r(this);\r\n },\r\n setRestriction(restriction) {\r\n this.limit = this.capacity = restriction;\r\n },\r\n get pending() {\r\n return this.q.length;\r\n },\r\n async flow(process, lazy) {\r\n await a(this, lazy);\r\n try {\r\n return await process();\r\n }\r\n finally {\r\n r(this);\r\n }\r\n }\r\n };\r\n};\r\n","import * as c from \"./class\";\r\nexport var restrictor;\r\n(function (restrictor) {\r\n const { MiniSemaphore: MS } = c;\r\n const internalLock = new MS(1);\r\n let locks = Object.create(null);\r\n const get = async (key, restriction) => {\r\n await internalLock.acquire(false);\r\n let lock = locks[key];\r\n if (!lock) {\r\n locks[key] = lock = new MS(restriction);\r\n }\r\n if (lock.limit !== restriction) {\r\n internalLock.release();\r\n throw new ReferenceError(`Cannot get object with different restriction: key: '${key}', lock.limit: ${lock.limit} <-> restriction: ${restriction},`);\r\n }\r\n internalLock.release();\r\n return lock;\r\n };\r\n restrictor.getLockByKey = async (key) => {\r\n await internalLock.acquire(false);\r\n const l = locks[key];\r\n internalLock.release();\r\n return l;\r\n };\r\n restrictor.cleanup = async (timeSpan, debug) => {\r\n await internalLock.acquire(false);\r\n const currentLocks = locks;\r\n const newLocks = Object.create(null);\r\n const keys = Object.keys(currentLocks);\r\n let eliminatedCount = 0;\r\n let eliminatedKeys;\r\n !timeSpan && (timeSpan = 1);\r\n timeSpan *= 1000;\r\n if (debug) {\r\n eliminatedKeys = [];\r\n }\r\n for (let i = 0, end = keys.length; i < end;) {\r\n const key = keys[i++];\r\n const s = currentLocks[key];\r\n if (s.last && Date.now() - s.last >= timeSpan) {\r\n eliminatedCount++;\r\n if (debug) {\r\n eliminatedKeys.push(key);\r\n }\r\n continue;\r\n }\r\n newLocks[key] = s;\r\n }\r\n locks = newLocks;\r\n internalLock.release();\r\n if (debug) {\r\n console.log(`eliminated: [\\n${eliminatedKeys.join(\",\\n\")}\\n]` +\r\n \"\\n\" +\r\n `lived: [\\n${Object.keys(newLocks).join(\",\\n\")}\\n]`);\r\n }\r\n return eliminatedCount;\r\n };\r\n async function multi(key, restriction, pb) {\r\n const s = await get(key, restriction);\r\n const result = s.flow(pb);\r\n s.last = Date.now();\r\n return result;\r\n }\r\n restrictor.multi = multi;\r\n async function one(key, pb) {\r\n return multi(key, 1, pb);\r\n }\r\n restrictor.one = one;\r\n})(restrictor || (restrictor = {}));\r\n"],"names":[],"sourceRoot":""}
|
package/webpack/index.d.ts
CHANGED
|
@@ -32,8 +32,8 @@ export declare class Deque<T extends any> {
|
|
|
32
32
|
/**
|
|
33
33
|
* @param s subject
|
|
34
34
|
*/
|
|
35
|
-
push(s: T):
|
|
36
|
-
shift(): T | undefined;
|
|
35
|
+
push(s: T): void;
|
|
36
|
+
// shift(): T | undefined;
|
|
37
37
|
clear(): void;
|
|
38
38
|
get length(): number;
|
|
39
39
|
}
|
|
@@ -94,8 +94,8 @@ export declare type TFlowableLock<T = TVoidFunction> = IFlowableLock & {
|
|
|
94
94
|
readonly q: Deque<T>;
|
|
95
95
|
};
|
|
96
96
|
export declare type TVoidFunction = () => void;
|
|
97
|
-
export declare const acquire: (dis: TFlowableLock<TVoidFunction>, lazy?: boolean) => Promise<void>;
|
|
98
|
-
export declare const release: (dis: TFlowableLock<TVoidFunction>) => void;
|
|
97
|
+
// export declare const acquire: (dis: TFlowableLock<TVoidFunction>, lazy?: boolean) => Promise<void>;
|
|
98
|
+
// export declare const release: (dis: TFlowableLock<TVoidFunction>) => void;
|
|
99
99
|
|
|
100
100
|
|
|
101
101
|
/**
|
package/webpack/index.js
CHANGED
|
@@ -1,117 +1,82 @@
|
|
|
1
|
-
|
|
2
|
-
var e = {};
|
|
3
|
-
function n(i) {
|
|
4
|
-
if (e[i]) return e[i].exports;
|
|
5
|
-
var r = e[i] = {
|
|
6
|
-
i: i,
|
|
7
|
-
l: !1,
|
|
8
|
-
exports: {}
|
|
9
|
-
};
|
|
10
|
-
return t[i].call(r.exports, r, r.exports, n), r.l = !0, r.exports;
|
|
11
|
-
}
|
|
12
|
-
return n.m = t, n.c = e, n.d = function(t, e, i) {
|
|
13
|
-
n.o(t, e) || Object.defineProperty(t, e, {
|
|
14
|
-
enumerable: !0,
|
|
15
|
-
get: i
|
|
16
|
-
});
|
|
17
|
-
}, n.r = function(t) {
|
|
18
|
-
'undefined' != typeof Symbol && Symbol.toStringTag && Object.defineProperty(t, Symbol.toStringTag, {
|
|
19
|
-
value: 'Module'
|
|
20
|
-
}), Object.defineProperty(t, '__esModule', {
|
|
21
|
-
value: !0
|
|
22
|
-
});
|
|
23
|
-
}, n.t = function(t, e) {
|
|
24
|
-
if (1 & e && (t = n(t)), 8 & e) return t;
|
|
25
|
-
if (4 & e && 'object' == typeof t && t && t.__esModule) return t;
|
|
26
|
-
var i = Object.create(null);
|
|
27
|
-
if (n.r(i), Object.defineProperty(i, 'default', {
|
|
28
|
-
enumerable: !0,
|
|
29
|
-
value: t
|
|
30
|
-
}), 2 & e && 'string' != typeof t) for (var r in t) n.d(i, r, function(e) {
|
|
31
|
-
return t[e];
|
|
32
|
-
}.bind(null, r));
|
|
33
|
-
return i;
|
|
34
|
-
}, n.n = function(t) {
|
|
35
|
-
var e = t && t.__esModule ? function() {
|
|
36
|
-
return t.default;
|
|
37
|
-
} : function() {
|
|
38
|
-
return t;
|
|
39
|
-
};
|
|
40
|
-
return n.d(e, 'a', e), e;
|
|
41
|
-
}, n.o = function(t, e) {
|
|
42
|
-
return Object.prototype.hasOwnProperty.call(t, e);
|
|
43
|
-
}, n.p = "", n(n.s = 0);
|
|
44
|
-
}([ function(t, e, n) {
|
|
1
|
+
(() => {
|
|
45
2
|
"use strict";
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
3
|
+
var t = {
|
|
4
|
+
d: (e, i) => {
|
|
5
|
+
for (var n in i) t.o(i, n) && !t.o(e, n) && Object.defineProperty(e, n, {
|
|
6
|
+
enumerable: !0,
|
|
7
|
+
get: i[n]
|
|
8
|
+
});
|
|
9
|
+
},
|
|
10
|
+
o: (t, e) => Object.prototype.hasOwnProperty.call(t, e),
|
|
11
|
+
r: t => {
|
|
12
|
+
'undefined' != typeof Symbol && Symbol.toStringTag && Object.defineProperty(t, Symbol.toStringTag, {
|
|
13
|
+
value: 'Module'
|
|
14
|
+
}), Object.defineProperty(t, '__esModule', {
|
|
15
|
+
value: !0
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}, e = {};
|
|
19
|
+
t.r(e), t.d(e, {
|
|
20
|
+
Deque: () => o,
|
|
21
|
+
MiniSemaphore: () => y,
|
|
22
|
+
create: () => _,
|
|
23
|
+
restrictor: () => m
|
|
24
|
+
});
|
|
55
25
|
var i = {};
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
})
|
|
59
|
-
const
|
|
26
|
+
t.r(i), t.d(i, {
|
|
27
|
+
MiniSemaphore: () => y
|
|
28
|
+
});
|
|
29
|
+
const n = () => {
|
|
60
30
|
throw new Error("mini-semaphore: inconsistent occurred");
|
|
61
31
|
}, s = (t, e) => {
|
|
62
32
|
t.capacity > 0 ? (t.capacity--, e()) : t.q.push(e);
|
|
63
|
-
},
|
|
64
|
-
e ? setTimeout(() => s(t,
|
|
65
|
-
}),
|
|
66
|
-
t.capacity++, t.q.length && (t.capacity -= 1, (t.q.shift() ||
|
|
33
|
+
}, r = (t, e = !0) => new Promise((i => {
|
|
34
|
+
e ? setTimeout((() => s(t, i)), 4) : s(t, i);
|
|
35
|
+
})), a = t => {
|
|
36
|
+
t.capacity++, t.q.length && (t.capacity -= 1, (t.q.shift() || n)()), t.capacity > t.limit && (console.warn("inconsistent release!"),
|
|
67
37
|
t.capacity = t.limit);
|
|
68
|
-
},
|
|
69
|
-
(t |= t >> 16)
|
|
70
|
-
class
|
|
38
|
+
}, c = t => (t => (t >>>= 0, t -= 1, t |= t >> 1, t |= t >> 2, t |= t >> 4, t |= t >> 8,
|
|
39
|
+
1 + (t |= t >> 16)))(Math.min(Math.max(16, 0 | t), 1073741824));
|
|
40
|
+
class o {
|
|
71
41
|
constructor(t) {
|
|
72
|
-
this._c =
|
|
42
|
+
this._c = c(t), this._l = 0, this._f = 0, this._a = [];
|
|
73
43
|
}
|
|
74
44
|
push(t) {
|
|
75
45
|
const e = this._l;
|
|
76
|
-
this._c < e + 1 &&
|
|
77
|
-
const
|
|
78
|
-
|
|
46
|
+
this._c < e + 1 && l(this, c(1.5 * this._c + 16));
|
|
47
|
+
const i = this._f + e & this._c - 1;
|
|
48
|
+
this._a[i] = t, this._l = e + 1;
|
|
79
49
|
}
|
|
80
50
|
shift() {
|
|
81
51
|
const t = this._l;
|
|
82
52
|
if (0 === t) return;
|
|
83
|
-
const e = this._f,
|
|
84
|
-
return this._a[e] = void 0, this._f = e + 1 & this._c - 1, this._l = t - 1,
|
|
85
|
-
}
|
|
86
|
-
clear() {
|
|
87
|
-
const t = this._l, e = this._f, n = this._c, i = this._a;
|
|
88
|
-
for (let r = 0; r < t; ++r) i[e + r & n - 1] = void 0;
|
|
89
|
-
this._l = 0, this._f = 0;
|
|
53
|
+
const e = this._f, i = this._a[e];
|
|
54
|
+
return this._a[e] = void 0, this._f = e + 1 & this._c - 1, this._l = t - 1, i;
|
|
90
55
|
}
|
|
91
56
|
get length() {
|
|
92
57
|
return this._l;
|
|
93
58
|
}
|
|
94
59
|
}
|
|
95
|
-
const
|
|
96
|
-
const
|
|
60
|
+
const l = (t, e) => {
|
|
61
|
+
const i = t._c;
|
|
97
62
|
t._c = e;
|
|
98
|
-
const
|
|
99
|
-
if (
|
|
100
|
-
const e =
|
|
101
|
-
((t, e,
|
|
102
|
-
for (let
|
|
103
|
-
})(t._a, 0, t._a,
|
|
63
|
+
const n = t._f, s = t._l;
|
|
64
|
+
if (n + s > i) {
|
|
65
|
+
const e = n + s & i - 1;
|
|
66
|
+
((t, e, i, n, s) => {
|
|
67
|
+
for (let r = 0; r < s; ++r) i[r + n] = t[r + e], t[r + e] = void 0;
|
|
68
|
+
})(t._a, 0, t._a, i, e);
|
|
104
69
|
}
|
|
105
|
-
}, h =
|
|
106
|
-
class
|
|
70
|
+
}, h = r, u = a;
|
|
71
|
+
class y {
|
|
107
72
|
constructor(t) {
|
|
108
|
-
this.limit = this.capacity = t, this.q = new
|
|
73
|
+
this.limit = this.capacity = t, this.q = new o(t);
|
|
109
74
|
}
|
|
110
75
|
acquire(t) {
|
|
111
76
|
return h(this, t);
|
|
112
77
|
}
|
|
113
78
|
release() {
|
|
114
|
-
|
|
79
|
+
u(this);
|
|
115
80
|
}
|
|
116
81
|
setRestriction(t) {
|
|
117
82
|
this.limit = this.capacity = t;
|
|
@@ -124,19 +89,19 @@ module.exports = function(t) {
|
|
|
124
89
|
try {
|
|
125
90
|
return await t();
|
|
126
91
|
} finally {
|
|
127
|
-
|
|
92
|
+
u(this);
|
|
128
93
|
}
|
|
129
94
|
}
|
|
130
95
|
}
|
|
131
|
-
const
|
|
96
|
+
const f = r, p = a, _ = t => ({
|
|
132
97
|
capacity: t,
|
|
133
98
|
limit: t,
|
|
134
|
-
q: new
|
|
99
|
+
q: new o(t),
|
|
135
100
|
acquire(t) {
|
|
136
|
-
return
|
|
101
|
+
return f(this, t);
|
|
137
102
|
},
|
|
138
103
|
release() {
|
|
139
|
-
|
|
104
|
+
p(this);
|
|
140
105
|
},
|
|
141
106
|
setRestriction(t) {
|
|
142
107
|
this.limit = this.capacity = t;
|
|
@@ -145,44 +110,45 @@ module.exports = function(t) {
|
|
|
145
110
|
return this.q.length;
|
|
146
111
|
},
|
|
147
112
|
async flow(t, e) {
|
|
148
|
-
await
|
|
113
|
+
await f(this, e);
|
|
149
114
|
try {
|
|
150
115
|
return await t();
|
|
151
116
|
} finally {
|
|
152
|
-
|
|
117
|
+
p(this);
|
|
153
118
|
}
|
|
154
119
|
}
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
var b;
|
|
120
|
+
});
|
|
121
|
+
var m;
|
|
158
122
|
!function(t) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
123
|
+
const {MiniSemaphore: e} = i, n = new e(1);
|
|
124
|
+
let s = Object.create(null);
|
|
125
|
+
async function r(t, i, r) {
|
|
126
|
+
const a = await (async (t, i) => {
|
|
127
|
+
await n.acquire(!1);
|
|
128
|
+
let r = s[t];
|
|
129
|
+
if (r || (s[t] = r = new e(i)), r.limit !== i) throw n.release(), new ReferenceError(`Cannot get object with different restriction: key: '${t}', lock.limit: ${r.limit} <-> restriction: ${i},`);
|
|
130
|
+
return n.release(), r;
|
|
131
|
+
})(t, i), c = a.flow(r);
|
|
132
|
+
return a.last = Date.now(), c;
|
|
167
133
|
}
|
|
168
134
|
t.getLockByKey = async t => {
|
|
169
|
-
await
|
|
170
|
-
const e =
|
|
171
|
-
return
|
|
135
|
+
await n.acquire(!1);
|
|
136
|
+
const e = s[t];
|
|
137
|
+
return n.release(), e;
|
|
172
138
|
}, t.cleanup = async (t, e) => {
|
|
173
|
-
await
|
|
174
|
-
const
|
|
175
|
-
let
|
|
176
|
-
!t && (t = 1), t *= 1e3, e && (
|
|
177
|
-
for (let
|
|
178
|
-
const
|
|
179
|
-
l.last && Date.now() - l.last >= t ? (
|
|
139
|
+
await n.acquire(!1);
|
|
140
|
+
const i = s, r = Object.create(null), a = Object.keys(i);
|
|
141
|
+
let c, o = 0;
|
|
142
|
+
!t && (t = 1), t *= 1e3, e && (c = []);
|
|
143
|
+
for (let n = 0, s = a.length; n < s; ) {
|
|
144
|
+
const s = a[n++], l = i[s];
|
|
145
|
+
l.last && Date.now() - l.last >= t ? (o++, e && c.push(s)) : r[s] = l;
|
|
180
146
|
}
|
|
181
|
-
return
|
|
182
|
-
|
|
183
|
-
}, t.multi =
|
|
184
|
-
return
|
|
147
|
+
return s = r, n.release(), e && console.log(`eliminated: [\n${c.join(",\n")}\n]\nlived: [\n${Object.keys(r).join(",\n")}\n]`),
|
|
148
|
+
o;
|
|
149
|
+
}, t.multi = r, t.one = async function(t, e) {
|
|
150
|
+
return r(t, 1, e);
|
|
185
151
|
};
|
|
186
|
-
}(
|
|
187
|
-
}
|
|
152
|
+
}(m || (m = {})), module.exports = e;
|
|
153
|
+
})();
|
|
188
154
|
//# sourceMappingURL=index.js.map
|
package/webpack/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["webpack
|
|
1
|
+
{"version":3,"file":"index.js","mappings":";;AACA;ACAA;AACA;AAEA;AAAA;;;ACJA;ACCA;AACA;AACA;AAEA;AAAA;;;;;;;;;;;;;;ACLA;AACA;ACAA;AACA;AAOA;AAEA;AAQA;AACA;AAMA;ACVA;AAFA;AAKA;AACA;AACA;;AAKA;AACA;AACA;AAGA;AACA;;AAGA;AACA;AACA;AAGA;AAKA;;AAEA;AACA;;;AAGA;AACA;AACA;AACA;AAEA;AACA;AAzDA;AACA;AAyDA;;ACxDA;AAEA;AACA;AACA;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AAEA;AACA;;;;ACzBA;AAIA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AAEA;AACA;;;;AC1BA;AACA;AACA;AAEA;AAqDA;AACA;AApDA;AACA;AAIA;AAKA;AA0CA;AAGA;;AA3CA;AACA;AACA;AAEA;AAEA;AACA;AACA;AAGA;AAEA;AAKA;AACA;AAEA;;AAgBA;AAAA;AAQA;AAEA;;AAhEA","sources":["webpack://mini-semaphore/webpack/bootstrap","webpack://mini-semaphore/webpack/runtime/define property getters","webpack://mini-semaphore/webpack/runtime/hasOwnProperty shorthand","webpack://mini-semaphore/webpack/runtime/make namespace object","webpack://mini-semaphore/./src/extras.ts","webpack://mini-semaphore/./src/core.ts","webpack://mini-semaphore/./src/deque.ts","webpack://mini-semaphore/./src/class.ts","webpack://mini-semaphore/./src/object.ts","webpack://mini-semaphore/./src/flow-restrictor.ts"],"sourcesContent":["// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","export const THROW = () => {\r\n throw new Error(\"mini-semaphore: inconsistent occurred\");\r\n};\r\n","import { THROW } from \"./extras\";\r\nconst box = (z, r) => {\r\n if (z.capacity > 0) {\r\n z.capacity--, r();\r\n }\r\n else {\r\n z.q.push(r);\r\n }\r\n};\r\nexport const acquire = (dis, lazy = true) => {\r\n return new Promise(r => {\r\n if (!lazy) {\r\n box(dis, r);\r\n }\r\n else {\r\n setTimeout(() => box(dis, r), 4);\r\n }\r\n });\r\n};\r\nexport const release = (dis) => {\r\n dis.capacity++;\r\n if (dis.q.length) {\r\n dis.capacity -= 1, (dis.q.shift() || THROW)();\r\n }\r\n if (dis.capacity > dis.limit) {\r\n console.warn(\"inconsistent release!\");\r\n dis.capacity = dis.limit;\r\n }\r\n};\r\n","const am = (src, si, dst, di, len) => {\r\n for (let j = 0; j < len; ++j) {\r\n dst[j + di] = src[j + si];\r\n src[j + si] = void 0;\r\n }\r\n};\r\nconst p2l = (n) => {\r\n n = n >>> 0;\r\n n = n - 1;\r\n n = n | (n >> 1);\r\n n = n | (n >> 2);\r\n n = n | (n >> 4);\r\n n = n | (n >> 8);\r\n n = n | (n >> 16);\r\n return n + 1;\r\n};\r\nconst gc = (n) => {\r\n return p2l(Math.min(Math.max(16, n | 0), 1073741824));\r\n};\r\nexport class Deque {\r\n constructor(ic) {\r\n this._c = gc(ic);\r\n this._l = 0;\r\n this._f = 0;\r\n this._a = [];\r\n }\r\n push(s) {\r\n const l = this._l;\r\n if (this._c < l + 1) {\r\n rt(this, gc(this._c * 1.5 + 16));\r\n }\r\n const i = (this._f + l) & (this._c - 1);\r\n this._a[i] = s;\r\n this._l = l + 1;\r\n }\r\n shift() {\r\n const l = this._l;\r\n if (l === 0) {\r\n return void 0;\r\n }\r\n const f = this._f;\r\n const r = this._a[f];\r\n this._a[f] = void 0;\r\n this._f = (f + 1) & (this._c - 1);\r\n this._l = l - 1;\r\n return r;\r\n }\r\n get length() {\r\n return this._l;\r\n }\r\n}\r\nconst rt = (dis, n) => {\r\n const oc = dis._c;\r\n dis._c = n;\r\n const f = dis._f;\r\n const l = dis._l;\r\n if (f + l > oc) {\r\n const mc = (f + l) & (oc - 1);\r\n am(dis._a, 0, dis._a, oc, mc);\r\n }\r\n};\r\n","import * as core from \"./core\";\r\nimport { Deque } from \"./deque\";\r\nconst a = core.acquire;\r\nconst r = core.release;\r\nexport class MiniSemaphore {\r\n constructor(capacity) {\r\n this.limit = this.capacity = capacity;\r\n this.q = new Deque(capacity);\r\n }\r\n acquire(lazy) {\r\n return a(this, lazy);\r\n }\r\n release() {\r\n r(this);\r\n }\r\n setRestriction(restriction) {\r\n this.limit = this.capacity = restriction;\r\n }\r\n get pending() {\r\n return this.q.length;\r\n }\r\n async flow(process, lazy) {\r\n await a(this, lazy);\r\n try {\r\n return await process();\r\n }\r\n finally {\r\n r(this);\r\n }\r\n }\r\n}\r\n","import * as core from \"./core\";\r\nimport { Deque } from \"./deque\";\r\nconst a = core.acquire;\r\nconst r = core.release;\r\nexport const create = (capacity) => {\r\n return {\r\n capacity,\r\n limit: capacity,\r\n q: new Deque(capacity),\r\n acquire(lazy) {\r\n return a(this, lazy);\r\n },\r\n release() {\r\n r(this);\r\n },\r\n setRestriction(restriction) {\r\n this.limit = this.capacity = restriction;\r\n },\r\n get pending() {\r\n return this.q.length;\r\n },\r\n async flow(process, lazy) {\r\n await a(this, lazy);\r\n try {\r\n return await process();\r\n }\r\n finally {\r\n r(this);\r\n }\r\n }\r\n };\r\n};\r\n","import * as c from \"./class\";\r\nexport var restrictor;\r\n(function (restrictor) {\r\n const { MiniSemaphore: MS } = c;\r\n const internalLock = new MS(1);\r\n let locks = Object.create(null);\r\n const get = async (key, restriction) => {\r\n await internalLock.acquire(false);\r\n let lock = locks[key];\r\n if (!lock) {\r\n locks[key] = lock = new MS(restriction);\r\n }\r\n if (lock.limit !== restriction) {\r\n internalLock.release();\r\n throw new ReferenceError(`Cannot get object with different restriction: key: '${key}', lock.limit: ${lock.limit} <-> restriction: ${restriction},`);\r\n }\r\n internalLock.release();\r\n return lock;\r\n };\r\n restrictor.getLockByKey = async (key) => {\r\n await internalLock.acquire(false);\r\n const l = locks[key];\r\n internalLock.release();\r\n return l;\r\n };\r\n restrictor.cleanup = async (timeSpan, debug) => {\r\n await internalLock.acquire(false);\r\n const currentLocks = locks;\r\n const newLocks = Object.create(null);\r\n const keys = Object.keys(currentLocks);\r\n let eliminatedCount = 0;\r\n let eliminatedKeys;\r\n !timeSpan && (timeSpan = 1);\r\n timeSpan *= 1000;\r\n if (debug) {\r\n eliminatedKeys = [];\r\n }\r\n for (let i = 0, end = keys.length; i < end;) {\r\n const key = keys[i++];\r\n const s = currentLocks[key];\r\n if (s.last && Date.now() - s.last >= timeSpan) {\r\n eliminatedCount++;\r\n if (debug) {\r\n eliminatedKeys.push(key);\r\n }\r\n continue;\r\n }\r\n newLocks[key] = s;\r\n }\r\n locks = newLocks;\r\n internalLock.release();\r\n if (debug) {\r\n console.log(`eliminated: [\\n${eliminatedKeys.join(\",\\n\")}\\n]` +\r\n \"\\n\" +\r\n `lived: [\\n${Object.keys(newLocks).join(\",\\n\")}\\n]`);\r\n }\r\n return eliminatedCount;\r\n };\r\n async function multi(key, restriction, pb) {\r\n const s = await get(key, restriction);\r\n const result = s.flow(pb);\r\n s.last = Date.now();\r\n return result;\r\n }\r\n restrictor.multi = multi;\r\n async function one(key, pb) {\r\n return multi(key, 1, pb);\r\n }\r\n restrictor.one = one;\r\n})(restrictor || (restrictor = {}));\r\n"],"names":[],"sourceRoot":""}
|