mini-semaphore 1.3.4 → 1.3.10
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/README.md +11 -1
- package/cjs/class.js +55 -1
- package/cjs/core.js +33 -2
- package/cjs/deque.js +40 -2
- package/cjs/extras.js +1 -1
- package/cjs/flow-restrictor.js +66 -2
- package/cjs/index.d.ts +0 -2
- package/cjs/index.js +9 -1
- package/cjs/object.js +36 -1
- package/esm/class.mjs +85 -0
- package/esm/core.mjs +60 -0
- package/esm/{deque.js → deque.mjs} +40 -2
- package/esm/{extras.js → extras.mjs} +2 -2
- package/esm/{flow-restrictor.js → flow-restrictor.mjs} +67 -3
- package/esm/index.d.ts +0 -2
- package/esm/index.mjs +12 -0
- package/esm/object.mjs +67 -0
- package/index.d.ts +0 -2
- package/package.json +5 -4
- package/umd/index.d.ts +0 -2
- package/umd/index.js +7 -158
- package/umd/index.js.LICENSE.txt +7 -0
- package/webpack/index.d.ts +0 -2
- package/webpack/index.js +6 -154
- package/webpack/index.js.LICENSE.txt +7 -0
- package/webpack-esm/index.d.ts +210 -0
- package/webpack-esm/index.mjs +6 -0
- package/webpack-esm/index.mjs.LICENSE.txt +7 -0
- package/esm/class.js +0 -31
- package/esm/core.js +0 -29
- package/esm/index.js +0 -4
- package/esm/object.js +0 -32
- package/umd/index.js.map +0 -1
- package/webpack/index.js.map +0 -1
|
@@ -1,9 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* arrayMove
|
|
3
|
+
*
|
|
4
|
+
* @param src
|
|
5
|
+
* @param si
|
|
6
|
+
* @param dst
|
|
7
|
+
* @param di
|
|
8
|
+
* @param len
|
|
9
|
+
*/
|
|
10
|
+
const am = /* istanbul ignore next */ (src, si, dst, di, len) => {
|
|
2
11
|
for (let j = 0; j < len; ++j) {
|
|
3
12
|
dst[j + di] = src[j + si];
|
|
4
13
|
src[j + si] = void 0;
|
|
5
14
|
}
|
|
6
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* pow2AtLeast
|
|
18
|
+
* @param n
|
|
19
|
+
*/
|
|
7
20
|
const p2l = (n) => {
|
|
8
21
|
n = n >>> 0;
|
|
9
22
|
n = n - 1;
|
|
@@ -14,16 +27,34 @@ const p2l = (n) => {
|
|
|
14
27
|
n = n | (n >> 16);
|
|
15
28
|
return n + 1;
|
|
16
29
|
};
|
|
30
|
+
/**
|
|
31
|
+
* getCapacity
|
|
32
|
+
* @param n
|
|
33
|
+
*/
|
|
17
34
|
const gc = (n) => {
|
|
35
|
+
// @ts-ignore typescript cannot allow (undefined | 0) expression
|
|
18
36
|
return p2l(Math.min(Math.max(16, n | 0), 1073741824));
|
|
19
37
|
};
|
|
38
|
+
/**
|
|
39
|
+
* ### Implementation restricted to FIFO
|
|
40
|
+
*
|
|
41
|
+
* this class is based on https://github.com/petkaantonov/deque/blob/master/js/deque.js
|
|
42
|
+
* Released under the MIT License: https://github.com/petkaantonov/deque/blob/master/LICENSE
|
|
43
|
+
*/
|
|
20
44
|
export class Deque {
|
|
45
|
+
/**
|
|
46
|
+
* default capacity `16`
|
|
47
|
+
* @param ic initial capacity
|
|
48
|
+
*/
|
|
21
49
|
constructor(ic) {
|
|
22
50
|
this._c = gc(ic);
|
|
23
51
|
this._l = 0;
|
|
24
52
|
this._f = 0;
|
|
25
53
|
this._a = [];
|
|
26
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* @param s subject
|
|
57
|
+
*/
|
|
27
58
|
push(s) {
|
|
28
59
|
const l = this._l;
|
|
29
60
|
if (this._c < l + 1) {
|
|
@@ -35,6 +66,7 @@ export class Deque {
|
|
|
35
66
|
}
|
|
36
67
|
shift() {
|
|
37
68
|
const l = this._l;
|
|
69
|
+
/* istanbul ignore if */
|
|
38
70
|
if (l === 0) {
|
|
39
71
|
return void 0;
|
|
40
72
|
}
|
|
@@ -49,13 +81,19 @@ export class Deque {
|
|
|
49
81
|
return this._l;
|
|
50
82
|
}
|
|
51
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* resize to
|
|
86
|
+
*
|
|
87
|
+
* @param n expected capacity
|
|
88
|
+
*/
|
|
52
89
|
const rt = (dis, n) => {
|
|
53
90
|
const oc = dis._c;
|
|
54
91
|
dis._c = n;
|
|
55
92
|
const f = dis._f;
|
|
56
93
|
const l = dis._l;
|
|
94
|
+
/* istanbul ignore next */
|
|
57
95
|
if (f + l > oc) {
|
|
58
96
|
const mc = (f + l) & (oc - 1);
|
|
59
97
|
am(dis._a, 0, dis._a, oc, mc);
|
|
60
98
|
}
|
|
61
|
-
};
|
|
99
|
+
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export const THROW = () => {
|
|
1
|
+
export const THROW = /* istanbul ignore next */ () => {
|
|
2
2
|
throw new Error("mini-semaphore: inconsistent occurred");
|
|
3
|
-
};
|
|
3
|
+
};
|
|
@@ -1,9 +1,39 @@
|
|
|
1
|
-
|
|
1
|
+
/*!
|
|
2
|
+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
3
|
+
Copyright (C) 2020 jeffy-g <hirotom1107@gmail.com>
|
|
4
|
+
Released under the MIT license
|
|
5
|
+
https://opensource.org/licenses/mit-license.php
|
|
6
|
+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* @file Utility module using `MiniSemaphore`
|
|
10
|
+
* @author jeffy-g <hirotom1107@gmail.com>
|
|
11
|
+
* @version 1.0
|
|
12
|
+
*/
|
|
13
|
+
import * as c from "./class.mjs";
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {string | number} TLockRecordKey
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Flow Restriction
|
|
19
|
+
*/
|
|
2
20
|
export var restrictor;
|
|
3
21
|
(function (restrictor) {
|
|
4
22
|
const { MiniSemaphore: MS } = c;
|
|
23
|
+
/**
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
5
26
|
const internalLock = new MS(1);
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
6
30
|
let locks = Object.create(null);
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* @param {TLockRecordKey} key
|
|
34
|
+
* @param {number} restriction
|
|
35
|
+
* @throws when different restriction
|
|
36
|
+
*/
|
|
7
37
|
const get = async (key, restriction) => {
|
|
8
38
|
await internalLock.acquire(false);
|
|
9
39
|
let lock = locks[key];
|
|
@@ -17,12 +47,28 @@ export var restrictor;
|
|
|
17
47
|
internalLock.release();
|
|
18
48
|
return lock;
|
|
19
49
|
};
|
|
50
|
+
/**
|
|
51
|
+
* get the semaphore associated with the value of `key`
|
|
52
|
+
*
|
|
53
|
+
* + ⚠️ The object to be retrieved with `key` must already be created with `multi` ore `one`
|
|
54
|
+
*
|
|
55
|
+
* @param {TLockRecordKey} key
|
|
56
|
+
* @returns `IFlowableLock` instance or `undefined`
|
|
57
|
+
*/
|
|
20
58
|
restrictor.getLockByKey = async (key) => {
|
|
21
59
|
await internalLock.acquire(false);
|
|
22
60
|
const l = locks[key];
|
|
23
61
|
internalLock.release();
|
|
24
62
|
return l;
|
|
25
63
|
};
|
|
64
|
+
/**
|
|
65
|
+
* Eliminate unused instances for the `timeSpan` seconds
|
|
66
|
+
*
|
|
67
|
+
* @param {number} timeSpan specify unit as seconds
|
|
68
|
+
* @param {true} [debug] enable debug
|
|
69
|
+
* @returns {Promise<number>} eliminated count
|
|
70
|
+
* @date 2020/6/19
|
|
71
|
+
*/
|
|
26
72
|
restrictor.cleanup = async (timeSpan, debug) => {
|
|
27
73
|
await internalLock.acquire(false);
|
|
28
74
|
const currentLocks = locks;
|
|
@@ -30,7 +76,7 @@ export var restrictor;
|
|
|
30
76
|
const keys = Object.keys(currentLocks);
|
|
31
77
|
let eliminatedCount = 0;
|
|
32
78
|
let eliminatedKeys;
|
|
33
|
-
!timeSpan && (timeSpan = 1);
|
|
79
|
+
!timeSpan && /* istanbul ignore next */ (timeSpan = 1);
|
|
34
80
|
timeSpan *= 1000;
|
|
35
81
|
if (debug) {
|
|
36
82
|
eliminatedKeys = [];
|
|
@@ -56,6 +102,14 @@ export var restrictor;
|
|
|
56
102
|
}
|
|
57
103
|
return eliminatedCount;
|
|
58
104
|
};
|
|
105
|
+
/**
|
|
106
|
+
* Allocate a semaphore for each `key`, and limit the number of shares with the value of `restriction`
|
|
107
|
+
*
|
|
108
|
+
* @template {any} T
|
|
109
|
+
* @param {TLockRecordKey} key number or string as tag
|
|
110
|
+
* @param {number} restriction number of process restriction
|
|
111
|
+
* @param {() => Promise<T>} pb the process body
|
|
112
|
+
*/
|
|
59
113
|
async function multi(key, restriction, pb) {
|
|
60
114
|
const s = await get(key, restriction);
|
|
61
115
|
const result = s.flow(pb);
|
|
@@ -63,8 +117,18 @@ export var restrictor;
|
|
|
63
117
|
return result;
|
|
64
118
|
}
|
|
65
119
|
restrictor.multi = multi;
|
|
120
|
+
/**
|
|
121
|
+
* synonym of `multi(key, 1, pb)`
|
|
122
|
+
*
|
|
123
|
+
* + use case
|
|
124
|
+
* * Avoid concurrent requests to the same url
|
|
125
|
+
*
|
|
126
|
+
* @template {any} T
|
|
127
|
+
* @param {TLockRecordKey} key number or string as tag
|
|
128
|
+
* @param {() => Promise<T>} pb the process body
|
|
129
|
+
*/
|
|
66
130
|
async function one(key, pb) {
|
|
67
131
|
return multi(key, 1, pb);
|
|
68
132
|
}
|
|
69
133
|
restrictor.one = one;
|
|
70
|
-
})(restrictor || (restrictor = {}));
|
|
134
|
+
})(restrictor || (restrictor = {}));
|
package/esm/index.d.ts
CHANGED
|
@@ -94,8 +94,6 @@ 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;
|
|
99
97
|
|
|
100
98
|
|
|
101
99
|
/**
|
package/esm/index.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
3
|
+
Copyright (C) 2020 jeffy-g <hirotom1107@gmail.com>
|
|
4
|
+
Released under the MIT license
|
|
5
|
+
https://opensource.org/licenses/mit-license.php
|
|
6
|
+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
7
|
+
*/
|
|
8
|
+
export { MiniSemaphore } from "./class.mjs";
|
|
9
|
+
export { create } from "./object.mjs";
|
|
10
|
+
export { Deque } from "./deque.mjs";
|
|
11
|
+
export { restrictor } from "./flow-restrictor.mjs";
|
|
12
|
+
export const version = "v1.3.10";
|
package/esm/object.mjs
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
3
|
+
Copyright (C) 2020 jeffy-g <hirotom1107@gmail.com>
|
|
4
|
+
Released under the MIT license
|
|
5
|
+
https://opensource.org/licenses/mit-license.php
|
|
6
|
+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* @file minimal implementation of semaphore (object implementation
|
|
10
|
+
* @author jeffy-g <hirotom1107@gmail.com>
|
|
11
|
+
* @version 1.0
|
|
12
|
+
*/
|
|
13
|
+
import * as core from "./core.mjs";
|
|
14
|
+
import { Deque } from "./deque.mjs";
|
|
15
|
+
const a = core.acquire;
|
|
16
|
+
const r = core.release;
|
|
17
|
+
/**
|
|
18
|
+
* object implementation of `IFlowableLock`
|
|
19
|
+
*
|
|
20
|
+
* + constructs a semaphore object limited at `capacity`
|
|
21
|
+
*
|
|
22
|
+
* @param {number} capacity limitation of concurrent async by `capacity`
|
|
23
|
+
* @date 2020/2/7
|
|
24
|
+
* @version 1.0
|
|
25
|
+
*/
|
|
26
|
+
export const create = (capacity) => {
|
|
27
|
+
return {
|
|
28
|
+
capacity,
|
|
29
|
+
limit: capacity,
|
|
30
|
+
q: new Deque(capacity),
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* @param {boolean} [lazy]
|
|
34
|
+
* @returns {Promise<void>}
|
|
35
|
+
*/
|
|
36
|
+
acquire(lazy) {
|
|
37
|
+
return a(this, lazy);
|
|
38
|
+
},
|
|
39
|
+
release() {
|
|
40
|
+
r(this);
|
|
41
|
+
},
|
|
42
|
+
/**
|
|
43
|
+
* @param {number} restriction
|
|
44
|
+
*/
|
|
45
|
+
setRestriction(restriction) {
|
|
46
|
+
this.limit = this.capacity = restriction;
|
|
47
|
+
},
|
|
48
|
+
get pending() {
|
|
49
|
+
return this.q.length;
|
|
50
|
+
},
|
|
51
|
+
/**
|
|
52
|
+
* @template {any} T
|
|
53
|
+
* @param {() => Promise<T>} process
|
|
54
|
+
* @param {boolean} [lazy]
|
|
55
|
+
* @returns {Promise<T>}
|
|
56
|
+
*/
|
|
57
|
+
async flow(process, lazy) {
|
|
58
|
+
await a(this, lazy);
|
|
59
|
+
try {
|
|
60
|
+
return await process();
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
r(this);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -94,8 +94,6 @@ 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;
|
|
99
97
|
|
|
100
98
|
|
|
101
99
|
/**
|
package/package.json
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mini-semaphore",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.10",
|
|
4
4
|
"description": "A lightweight version of Semaphore",
|
|
5
5
|
"private": false,
|
|
6
6
|
"main": "./cjs/index.js",
|
|
7
|
-
"module": "./esm/index.
|
|
7
|
+
"module": "./esm/index.mjs",
|
|
8
8
|
"unpkg": "./umd/index.js",
|
|
9
9
|
"sideEffects": false,
|
|
10
10
|
"types": "./index.d.ts",
|
|
11
|
-
"typings": "./index.d.ts",
|
|
12
11
|
"author": "jeffy-g",
|
|
13
12
|
"license": "MIT",
|
|
14
13
|
"bugs": {
|
|
@@ -20,7 +19,7 @@
|
|
|
20
19
|
"url": "git+https://github.com/jeffy-g/mini-semaphore.git"
|
|
21
20
|
},
|
|
22
21
|
"engines": {
|
|
23
|
-
"node": ">=10",
|
|
22
|
+
"node": ">=v12.22.10",
|
|
24
23
|
"yarn": "^1.22.4"
|
|
25
24
|
},
|
|
26
25
|
"files": [
|
|
@@ -31,9 +30,11 @@
|
|
|
31
30
|
"esm",
|
|
32
31
|
"umd",
|
|
33
32
|
"webpack",
|
|
33
|
+
"webpack-esm",
|
|
34
34
|
"*.d.ts"
|
|
35
35
|
],
|
|
36
36
|
"keywords": [
|
|
37
|
+
"async",
|
|
37
38
|
"lock",
|
|
38
39
|
"mutex",
|
|
39
40
|
"promise",
|
package/umd/index.d.ts
CHANGED
|
@@ -94,8 +94,6 @@ 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;
|
|
99
97
|
|
|
100
98
|
|
|
101
99
|
/**
|
package/umd/index.js
CHANGED
|
@@ -1,158 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
});
|
|
28
|
-
var i = {};
|
|
29
|
-
t.r(i), t.d(i, {
|
|
30
|
-
MiniSemaphore: () => f
|
|
31
|
-
});
|
|
32
|
-
const n = () => {
|
|
33
|
-
throw new Error("mini-semaphore: inconsistent occurred");
|
|
34
|
-
}, r = (t, e) => {
|
|
35
|
-
t.capacity > 0 ? (t.capacity--, e()) : t.q.push(e);
|
|
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!"),
|
|
40
|
-
t.capacity = t.limit);
|
|
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 {
|
|
44
|
-
constructor(t) {
|
|
45
|
-
this._c = c(t), this._l = 0, this._f = 0, this._a = [];
|
|
46
|
-
}
|
|
47
|
-
push(t) {
|
|
48
|
-
const e = this._l;
|
|
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;
|
|
52
|
-
}
|
|
53
|
-
shift() {
|
|
54
|
-
const t = this._l;
|
|
55
|
-
if (0 === t) return;
|
|
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;
|
|
58
|
-
}
|
|
59
|
-
get length() {
|
|
60
|
-
return this._l;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
const l = (t, e) => {
|
|
64
|
-
const i = t._c;
|
|
65
|
-
t._c = e;
|
|
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);
|
|
72
|
-
}
|
|
73
|
-
}, h = s, u = a;
|
|
74
|
-
class f {
|
|
75
|
-
constructor(t) {
|
|
76
|
-
this.limit = this.capacity = t, this.q = new o(t);
|
|
77
|
-
}
|
|
78
|
-
acquire(t) {
|
|
79
|
-
return h(this, t);
|
|
80
|
-
}
|
|
81
|
-
release() {
|
|
82
|
-
u(this);
|
|
83
|
-
}
|
|
84
|
-
setRestriction(t) {
|
|
85
|
-
this.limit = this.capacity = t;
|
|
86
|
-
}
|
|
87
|
-
get pending() {
|
|
88
|
-
return this.q.length;
|
|
89
|
-
}
|
|
90
|
-
async flow(t, e) {
|
|
91
|
-
await h(this, e);
|
|
92
|
-
try {
|
|
93
|
-
return await t();
|
|
94
|
-
} finally {
|
|
95
|
-
u(this);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const y = s, p = a, m = t => ({
|
|
100
|
-
capacity: t,
|
|
101
|
-
limit: t,
|
|
102
|
-
q: new o(t),
|
|
103
|
-
acquire(t) {
|
|
104
|
-
return y(this, t);
|
|
105
|
-
},
|
|
106
|
-
release() {
|
|
107
|
-
p(this);
|
|
108
|
-
},
|
|
109
|
-
setRestriction(t) {
|
|
110
|
-
this.limit = this.capacity = t;
|
|
111
|
-
},
|
|
112
|
-
get pending() {
|
|
113
|
-
return this.q.length;
|
|
114
|
-
},
|
|
115
|
-
async flow(t, e) {
|
|
116
|
-
await y(this, e);
|
|
117
|
-
try {
|
|
118
|
-
return await t();
|
|
119
|
-
} finally {
|
|
120
|
-
p(this);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
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;
|
|
136
|
-
}
|
|
137
|
-
t.getLockByKey = async t => {
|
|
138
|
-
await n.acquire(!1);
|
|
139
|
-
const e = r[t];
|
|
140
|
-
return n.release(), e;
|
|
141
|
-
}, t.cleanup = async (t, e) => {
|
|
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;
|
|
149
|
-
}
|
|
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);
|
|
154
|
-
};
|
|
155
|
-
}(_ || (_ = {})), e;
|
|
156
|
-
})();
|
|
157
|
-
}));
|
|
158
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
/*! For license information please see index.js.LICENSE.txt */
|
|
2
|
+
!function(e,t){'object'==typeof exports&&'object'==typeof module?module.exports=t():'function'==typeof define&&define.amd?define([],t):'object'==typeof exports?exports.MiniSema=t():e.MiniSema=t()}(globalThis,(()=>(()=>{"use strict";var e={518:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.MiniSemaphore=void 0;const r=i(461),n=i(761),s=r.acquire,o=r.release;t.MiniSemaphore=class{constructor(e){this.limit=this.capacity=e,this.q=new n.Deque(e)}acquire(e){return s(this,e)}release(){o(this)}setRestriction(e){this.limit=this.capacity=e}get pending(){return this.q.length}async flow(e,t){await s(this,t);try{return await e()}finally{o(this)}}}},461:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.release=t.acquire=void 0;const r=i(669),n=(e,t)=>{e.capacity>0?(e.capacity--,
|
|
3
|
+
t()):e.q.push(t)};t.acquire=(e,t=!0)=>new Promise((i=>{t?setTimeout((()=>n(e,i)),4):n(e,i)}));t.release=e=>{e.capacity++,e.q.length&&(e.capacity-=1,(e.q.shift()||r.THROW)()),e.capacity>e.limit&&(console.warn("inconsistent release!"),e.capacity=e.limit)}},761:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Deque=void 0;const i=e=>(e=>(e>>>=0,e-=1,e|=e>>1,e|=e>>2,e|=e>>4,e|=e>>8,1+(e|=e>>16)))(Math.min(Math.max(16,0|e),1073741824));t.Deque=class{constructor(e){this._c=i(e),this._l=0,this._f=0,this._a=[]}push(e){const t=this._l;this._c<t+1&&r(this,i(1.5*this._c+16));const n=this._f+t&this._c-1;this._a[n]=e,this._l=t+1}shift(){const e=this._l;if(0===e)return;const t=this._f,i=this._a[t];return this._a[t]=void 0,this._f=t+1&this._c-1,this._l=e-1,i}get length(){return this._l}}
|
|
4
|
+
;const r=(e,t)=>{const i=e._c;e._c=t;const r=e._f,n=e._l;if(r+n>i){const t=r+n&i-1;((e,t,i,r,n)=>{for(let s=0;s<n;++s)i[s+r]=e[s+t],e[s+t]=void 0})(e._a,0,e._a,i,t)}}},669:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.THROW=void 0;t.THROW=()=>{throw new Error("mini-semaphore: inconsistent occurred")}},464:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.restrictor=void 0;const r=i(518);(e=>{const{MiniSemaphore:t}=r,i=new t(1);let n=Object.create(null);async function s(e,r,s){const o=await(async(e,r)=>{await i.acquire(!1);let s=n[e];if(s||(n[e]=s=new t(r)),s.limit!==r)throw i.release(),new ReferenceError(`Cannot get object with different restriction: key: '${e}', lock.limit: ${s.limit} <-> restriction: ${r},`);return i.release(),s})(e,r),c=o.flow(s)
|
|
5
|
+
;return o.last=Date.now(),c}e.getLockByKey=async e=>{await i.acquire(!1);const t=n[e];return i.release(),t},e.cleanup=async(e,t)=>{await i.acquire(!1);const r=n,s=Object.create(null),o=Object.keys(r);let c,a=0;!e&&(e=1),e*=1e3,t&&(c=[]);for(let i=0,n=o.length;i<n;){const n=o[i++],l=r[n];l.last&&Date.now()-l.last>=e?(a++,t&&c.push(n)):s[n]=l}return n=s,i.release(),t&&console.log(`eliminated: [\n${c.join(",\n")}\n]\nlived: [\n${Object.keys(s).join(",\n")}\n]`),a},e.multi=s,e.one=async function(e,t){return s(e,1,t)}})(t.restrictor||(t.restrictor={}))},139:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.create=void 0;const r=i(461),n=i(761),s=r.acquire,o=r.release;t.create=e=>({capacity:e,limit:e,q:new n.Deque(e),acquire(e){return s(this,e)},release(){o(this)},setRestriction(e){
|
|
6
|
+
this.limit=this.capacity=e},get pending(){return this.q.length},async flow(e,t){await s(this,t);try{return await e()}finally{o(this)}}})}},t={};function i(r){var n=t[r];if(void 0!==n)return n.exports;var s=t[r]={exports:{}};return e[r](s,s.exports,i),s.exports}var r={};return(()=>{var e=r;Object.defineProperty(e,"__esModule",{value:!0}),e.version=e.restrictor=e.Deque=e.create=e.MiniSemaphore=void 0;var t=i(518);Object.defineProperty(e,"MiniSemaphore",{enumerable:!0,get:function(){return t.MiniSemaphore}});var n=i(139);Object.defineProperty(e,"create",{enumerable:!0,get:function(){return n.create}});var s=i(761);Object.defineProperty(e,"Deque",{enumerable:!0,get:function(){return s.Deque}});var o=i(464);Object.defineProperty(e,"restrictor",{enumerable:!0,get:function(){return o.restrictor}
|
|
7
|
+
}),e.version="v1.3.10"})(),r})()));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
3
|
+
Copyright (C) 2020 jeffy-g <hirotom1107@gmail.com>
|
|
4
|
+
Released under the MIT license
|
|
5
|
+
https://opensource.org/licenses/mit-license.php
|
|
6
|
+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
7
|
+
*/
|
package/webpack/index.d.ts
CHANGED
|
@@ -94,8 +94,6 @@ 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;
|
|
99
97
|
|
|
100
98
|
|
|
101
99
|
/**
|