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
package/webpack/index.js
CHANGED
|
@@ -1,154 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
});
|
|
25
|
-
var i = {};
|
|
26
|
-
t.r(i), t.d(i, {
|
|
27
|
-
MiniSemaphore: () => y
|
|
28
|
-
});
|
|
29
|
-
const n = () => {
|
|
30
|
-
throw new Error("mini-semaphore: inconsistent occurred");
|
|
31
|
-
}, s = (t, e) => {
|
|
32
|
-
t.capacity > 0 ? (t.capacity--, e()) : t.q.push(e);
|
|
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!"),
|
|
37
|
-
t.capacity = t.limit);
|
|
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 {
|
|
41
|
-
constructor(t) {
|
|
42
|
-
this._c = c(t), this._l = 0, this._f = 0, this._a = [];
|
|
43
|
-
}
|
|
44
|
-
push(t) {
|
|
45
|
-
const e = this._l;
|
|
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;
|
|
49
|
-
}
|
|
50
|
-
shift() {
|
|
51
|
-
const t = this._l;
|
|
52
|
-
if (0 === t) return;
|
|
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;
|
|
55
|
-
}
|
|
56
|
-
get length() {
|
|
57
|
-
return this._l;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const l = (t, e) => {
|
|
61
|
-
const i = t._c;
|
|
62
|
-
t._c = e;
|
|
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);
|
|
69
|
-
}
|
|
70
|
-
}, h = r, u = a;
|
|
71
|
-
class y {
|
|
72
|
-
constructor(t) {
|
|
73
|
-
this.limit = this.capacity = t, this.q = new o(t);
|
|
74
|
-
}
|
|
75
|
-
acquire(t) {
|
|
76
|
-
return h(this, t);
|
|
77
|
-
}
|
|
78
|
-
release() {
|
|
79
|
-
u(this);
|
|
80
|
-
}
|
|
81
|
-
setRestriction(t) {
|
|
82
|
-
this.limit = this.capacity = t;
|
|
83
|
-
}
|
|
84
|
-
get pending() {
|
|
85
|
-
return this.q.length;
|
|
86
|
-
}
|
|
87
|
-
async flow(t, e) {
|
|
88
|
-
await h(this, e);
|
|
89
|
-
try {
|
|
90
|
-
return await t();
|
|
91
|
-
} finally {
|
|
92
|
-
u(this);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
const f = r, p = a, _ = t => ({
|
|
97
|
-
capacity: t,
|
|
98
|
-
limit: t,
|
|
99
|
-
q: new o(t),
|
|
100
|
-
acquire(t) {
|
|
101
|
-
return f(this, t);
|
|
102
|
-
},
|
|
103
|
-
release() {
|
|
104
|
-
p(this);
|
|
105
|
-
},
|
|
106
|
-
setRestriction(t) {
|
|
107
|
-
this.limit = this.capacity = t;
|
|
108
|
-
},
|
|
109
|
-
get pending() {
|
|
110
|
-
return this.q.length;
|
|
111
|
-
},
|
|
112
|
-
async flow(t, e) {
|
|
113
|
-
await f(this, e);
|
|
114
|
-
try {
|
|
115
|
-
return await t();
|
|
116
|
-
} finally {
|
|
117
|
-
p(this);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
var m;
|
|
122
|
-
!function(t) {
|
|
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;
|
|
133
|
-
}
|
|
134
|
-
t.getLockByKey = async t => {
|
|
135
|
-
await n.acquire(!1);
|
|
136
|
-
const e = s[t];
|
|
137
|
-
return n.release(), e;
|
|
138
|
-
}, t.cleanup = async (t, e) => {
|
|
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;
|
|
146
|
-
}
|
|
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);
|
|
151
|
-
};
|
|
152
|
-
}(m || (m = {})), module.exports = e;
|
|
153
|
-
})();
|
|
154
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
/*! For license information please see index.js.LICENSE.txt */
|
|
2
|
+
(()=>{"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,a=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(){a(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{a(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--,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)()),
|
|
3
|
+
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}};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)=>{
|
|
4
|
+
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 a=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=a.flow(s);return a.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)
|
|
5
|
+
;const r=n,s=Object.create(null),a=Object.keys(r);let c,o=0;!e&&(e=1),e*=1e3,t&&(c=[]);for(let i=0,n=a.length;i<n;){const n=a[i++],l=r[n];l.last&&Date.now()-l.last>=e?(o++,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]`),o},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,a=r.release;t.create=e=>({capacity:e,limit:e,q:new n.Deque(e),acquire(e){return s(this,e)},release(){a(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{a(this)}}})}},t={}
|
|
6
|
+
;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={};(()=>{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 a=i(464);Object.defineProperty(e,"restrictor",{enumerable:!0,get:function(){return a.restrictor}}),e.version="v1.3.10"})(),module.exports=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
|
+
*/
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ### Implementation restricted to FIFO
|
|
3
|
+
*
|
|
4
|
+
* this class is based on https://github.com/petkaantonov/deque/blob/master/js/deque.js
|
|
5
|
+
* Released under the MIT License: https://github.com/petkaantonov/deque/blob/master/LICENSE
|
|
6
|
+
*/
|
|
7
|
+
export declare class Deque<T extends any> {
|
|
8
|
+
/**
|
|
9
|
+
* capacity
|
|
10
|
+
* @type {number}
|
|
11
|
+
*/
|
|
12
|
+
_c: number;
|
|
13
|
+
/**
|
|
14
|
+
* current length (size
|
|
15
|
+
* @type {number}
|
|
16
|
+
*/
|
|
17
|
+
_l: number;
|
|
18
|
+
/**
|
|
19
|
+
* current front position
|
|
20
|
+
* @type {number}
|
|
21
|
+
*/
|
|
22
|
+
_f: number;
|
|
23
|
+
/**
|
|
24
|
+
* @type {T[]}
|
|
25
|
+
*/
|
|
26
|
+
_a: T[];
|
|
27
|
+
/**
|
|
28
|
+
* default capacity `16`
|
|
29
|
+
* @param ic initial capacity
|
|
30
|
+
*/
|
|
31
|
+
constructor(ic?: number);
|
|
32
|
+
/**
|
|
33
|
+
* @param s subject
|
|
34
|
+
*/
|
|
35
|
+
push(s: T): void;
|
|
36
|
+
// shift(): T | undefined;
|
|
37
|
+
clear(): void;
|
|
38
|
+
get length(): number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* basic of simplified lock interface
|
|
43
|
+
*/
|
|
44
|
+
export interface ISimplifiedLock {
|
|
45
|
+
/**
|
|
46
|
+
* acquire the process rights
|
|
47
|
+
*
|
|
48
|
+
* @param lazy Whether the privilege acquisition process is deffer. default `true`
|
|
49
|
+
*/
|
|
50
|
+
acquire(lazy?: boolean): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* release the pending of one
|
|
53
|
+
*/
|
|
54
|
+
release(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Change sharing restrictions to the value of `restriction`
|
|
57
|
+
* @param {number} restriction
|
|
58
|
+
*/
|
|
59
|
+
setRestriction(restriction: number): void;
|
|
60
|
+
/**
|
|
61
|
+
* Get the number of currently pending processes
|
|
62
|
+
* @type {number}
|
|
63
|
+
*/
|
|
64
|
+
readonly pending: number;
|
|
65
|
+
/**
|
|
66
|
+
* limitation
|
|
67
|
+
*/
|
|
68
|
+
limit: number;
|
|
69
|
+
/**
|
|
70
|
+
* capacity
|
|
71
|
+
*/
|
|
72
|
+
capacity: number;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* extention of `ISimplifiedLock` interface
|
|
76
|
+
*/
|
|
77
|
+
export interface IFlowableLock extends ISimplifiedLock {
|
|
78
|
+
/**
|
|
79
|
+
* combination of acquire/release
|
|
80
|
+
*
|
|
81
|
+
* + acquire/release is automatic
|
|
82
|
+
*
|
|
83
|
+
* @param lazy Whether the privilege acquisition process is deffer. default `true`
|
|
84
|
+
*/
|
|
85
|
+
flow<T>(f: () => Promise<T>, lazy?: boolean): Promise<T>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* internal type for `createMiniSemaphore`
|
|
89
|
+
*/
|
|
90
|
+
export declare type TFlowableLock<T = TVoidFunction> = IFlowableLock & {
|
|
91
|
+
/**
|
|
92
|
+
* pending
|
|
93
|
+
*/
|
|
94
|
+
readonly q: Deque<T>;
|
|
95
|
+
};
|
|
96
|
+
export declare type TVoidFunction = () => void;
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* #### Mini Semaphore
|
|
101
|
+
*
|
|
102
|
+
* + minimal implementation of semaphore
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* import { MiniSemaphore } from "mini-semaphore";
|
|
106
|
+
*
|
|
107
|
+
* const s = new MiniSemaphore(10);
|
|
108
|
+
* async function fetchTypeData(type_id) {
|
|
109
|
+
* await s.acquire();
|
|
110
|
+
* try {
|
|
111
|
+
* return fetch(`https://esi.evetech.net/latest/universe/types/${type_id}/`);
|
|
112
|
+
* } finally {
|
|
113
|
+
* s.release();
|
|
114
|
+
* }
|
|
115
|
+
* }
|
|
116
|
+
*
|
|
117
|
+
* //
|
|
118
|
+
* // or automatic acquire/release
|
|
119
|
+
* //
|
|
120
|
+
* async function fetchTypeData(type_id) {
|
|
121
|
+
* return s.flow(async () => fetch(`https://esi.evetech.net/latest/universe/types/${type_id}/`));
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* @date 2020/2/7
|
|
125
|
+
* @version 1.0
|
|
126
|
+
*/
|
|
127
|
+
export declare class MiniSemaphore implements TFlowableLock {
|
|
128
|
+
/**
|
|
129
|
+
* spare capacity
|
|
130
|
+
*/
|
|
131
|
+
capacity: number;
|
|
132
|
+
/**
|
|
133
|
+
* limitation
|
|
134
|
+
*/
|
|
135
|
+
limit: number;
|
|
136
|
+
/**
|
|
137
|
+
* queue of Promise's `resolve`
|
|
138
|
+
*/
|
|
139
|
+
q: Deque<TVoidFunction>;
|
|
140
|
+
/**
|
|
141
|
+
* constructs a semaphore instance limited at `capacity`
|
|
142
|
+
*
|
|
143
|
+
* @param capacity limitation of concurrent async by `capacity`
|
|
144
|
+
*/
|
|
145
|
+
constructor(capacity: number);
|
|
146
|
+
/**
|
|
147
|
+
* If there is enough capacity, execute the `resolve` immediately
|
|
148
|
+
*
|
|
149
|
+
* If not, put it in a queue and wait for the currently pending process to execute `release`
|
|
150
|
+
*/
|
|
151
|
+
acquire(lazy?: boolean): Promise<void>;
|
|
152
|
+
release(): void;
|
|
153
|
+
setRestriction(restriction: number): void;
|
|
154
|
+
get pending(): number;
|
|
155
|
+
/**
|
|
156
|
+
* automatic acquire/release
|
|
157
|
+
* @param process
|
|
158
|
+
*/
|
|
159
|
+
flow<T>(process: () => Promise<T>, lazy?: boolean): Promise<T>;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* object implementation of `IFlowableLock`
|
|
164
|
+
*
|
|
165
|
+
* + constructs a semaphore object limited at `capacity`
|
|
166
|
+
*
|
|
167
|
+
* @param capacity limitation of concurrent async by `capacity`
|
|
168
|
+
* @date 2020/2/7
|
|
169
|
+
* @version 1.0
|
|
170
|
+
*/
|
|
171
|
+
export declare const create: (capacity: number) => IFlowableLock;
|
|
172
|
+
|
|
173
|
+
declare namespace fr {
|
|
174
|
+
/**
|
|
175
|
+
* Eliminate unused instances for the `timeSpan` seconds
|
|
176
|
+
*
|
|
177
|
+
* @param timeSpan specify unit as seconds
|
|
178
|
+
* @returns {Promise<number>} eliminated count
|
|
179
|
+
* @date 2020/6/19
|
|
180
|
+
*/
|
|
181
|
+
const cleanup: (timeSpan: number, debug?: true | undefined) => Promise<number>;
|
|
182
|
+
/**
|
|
183
|
+
* get the semaphore associated with the value of `key`
|
|
184
|
+
*
|
|
185
|
+
* + ⚠️ The object to be retrieved with `key` must already be created with `multi` ore `one`
|
|
186
|
+
*
|
|
187
|
+
* @param key
|
|
188
|
+
* @returns `IFlowableLock` instance or `undefined`
|
|
189
|
+
*/
|
|
190
|
+
export const getLockByKey: (key: string | number) => Promise<IFlowableLock>;
|
|
191
|
+
/**
|
|
192
|
+
* Allocate a semaphore for each `key`, and limit the number of shares with the value of `restriction`
|
|
193
|
+
*
|
|
194
|
+
* @param key number or string as tag
|
|
195
|
+
* @param restriction number of process restriction
|
|
196
|
+
* @param pb the process body
|
|
197
|
+
*/
|
|
198
|
+
export function multi<T>(key: string | number, restriction: number, pb: () => Promise<T>): Promise<T>;
|
|
199
|
+
/**
|
|
200
|
+
* synonym of `multi(key, 1, pb)`
|
|
201
|
+
*
|
|
202
|
+
* + use case
|
|
203
|
+
* * Avoid concurrent requests to the same url
|
|
204
|
+
*
|
|
205
|
+
* @param key number or string as tag
|
|
206
|
+
* @param pb the process body
|
|
207
|
+
*/
|
|
208
|
+
export function one<T>(key: string | number, pb: () => Promise<T>): Promise<T>;
|
|
209
|
+
}
|
|
210
|
+
export declare const restrictor: typeof fr;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/*! For license information please see index.mjs.LICENSE.txt */
|
|
2
|
+
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,a=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(){a(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{a(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--,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)()),
|
|
3
|
+
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}};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)=>{
|
|
4
|
+
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 a=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=a.flow(s);return a.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)
|
|
5
|
+
;const r=n,s=Object.create(null),a=Object.keys(r);let c,o=0;!e&&(e=1),e*=1e3,t&&(c=[]);for(let i=0,n=a.length;i<n;){const n=a[i++],l=r[n];l.last&&Date.now()-l.last>=e?(o++,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]`),o},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,a=r.release;t.create=e=>({capacity:e,limit:e,q:new n.Deque(e),acquire(e){return s(this,e)},release(){a(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{a(this)}}})}},t={}
|
|
6
|
+
;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={};(()=>{var e=r;Object.defineProperty(e,"X$",{value:!0}),e.i8=e.gD=e.P7=e.Ue=e.G3=void 0;var t=i(518);Object.defineProperty(e,"G3",{enumerable:!0,get:function(){return t.MiniSemaphore}});var n=i(139);Object.defineProperty(e,"Ue",{enumerable:!0,get:function(){return n.create}});var s=i(761);Object.defineProperty(e,"P7",{enumerable:!0,get:function(){return s.Deque}});var a=i(464);Object.defineProperty(e,"gD",{enumerable:!0,get:function(){return a.restrictor}}),e.i8="v1.3.10"})();var n=r.P7,s=r.G3,a=r.X$,c=r.Ue,o=r.gD,l=r.i8;export{n as Deque,s as MiniSemaphore,a as __esModule,c as create,o as restrictor,l as version};
|
|
@@ -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/esm/class.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import * as core from "./core";
|
|
2
|
-
import { Deque } from "./deque";
|
|
3
|
-
const a = core.acquire;
|
|
4
|
-
const r = core.release;
|
|
5
|
-
export class MiniSemaphore {
|
|
6
|
-
constructor(capacity) {
|
|
7
|
-
this.limit = this.capacity = capacity;
|
|
8
|
-
this.q = new Deque(capacity);
|
|
9
|
-
}
|
|
10
|
-
acquire(lazy) {
|
|
11
|
-
return a(this, lazy);
|
|
12
|
-
}
|
|
13
|
-
release() {
|
|
14
|
-
r(this);
|
|
15
|
-
}
|
|
16
|
-
setRestriction(restriction) {
|
|
17
|
-
this.limit = this.capacity = restriction;
|
|
18
|
-
}
|
|
19
|
-
get pending() {
|
|
20
|
-
return this.q.length;
|
|
21
|
-
}
|
|
22
|
-
async flow(process, lazy) {
|
|
23
|
-
await a(this, lazy);
|
|
24
|
-
try {
|
|
25
|
-
return await process();
|
|
26
|
-
}
|
|
27
|
-
finally {
|
|
28
|
-
r(this);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
package/esm/core.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { THROW } from "./extras";
|
|
2
|
-
const box = (z, r) => {
|
|
3
|
-
if (z.capacity > 0) {
|
|
4
|
-
z.capacity--, r();
|
|
5
|
-
}
|
|
6
|
-
else {
|
|
7
|
-
z.q.push(r);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
export const acquire = (dis, lazy = true) => {
|
|
11
|
-
return new Promise(r => {
|
|
12
|
-
if (!lazy) {
|
|
13
|
-
box(dis, r);
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
setTimeout(() => box(dis, r), 4);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
};
|
|
20
|
-
export const release = (dis) => {
|
|
21
|
-
dis.capacity++;
|
|
22
|
-
if (dis.q.length) {
|
|
23
|
-
dis.capacity -= 1, (dis.q.shift() || THROW)();
|
|
24
|
-
}
|
|
25
|
-
if (dis.capacity > dis.limit) {
|
|
26
|
-
console.warn("inconsistent release!");
|
|
27
|
-
dis.capacity = dis.limit;
|
|
28
|
-
}
|
|
29
|
-
};
|
package/esm/index.js
DELETED
package/esm/object.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import * as core from "./core";
|
|
2
|
-
import { Deque } from "./deque";
|
|
3
|
-
const a = core.acquire;
|
|
4
|
-
const r = core.release;
|
|
5
|
-
export const create = (capacity) => {
|
|
6
|
-
return {
|
|
7
|
-
capacity,
|
|
8
|
-
limit: capacity,
|
|
9
|
-
q: new Deque(capacity),
|
|
10
|
-
acquire(lazy) {
|
|
11
|
-
return a(this, lazy);
|
|
12
|
-
},
|
|
13
|
-
release() {
|
|
14
|
-
r(this);
|
|
15
|
-
},
|
|
16
|
-
setRestriction(restriction) {
|
|
17
|
-
this.limit = this.capacity = restriction;
|
|
18
|
-
},
|
|
19
|
-
get pending() {
|
|
20
|
-
return this.q.length;
|
|
21
|
-
},
|
|
22
|
-
async flow(process, lazy) {
|
|
23
|
-
await a(this, lazy);
|
|
24
|
-
try {
|
|
25
|
-
return await process();
|
|
26
|
-
}
|
|
27
|
-
finally {
|
|
28
|
-
r(this);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
};
|
package/umd/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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":""}
|