better-lock 3.0.0 → 3.1.0
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 +4 -3
- package/dist/better_lock.js +25 -22
- package/dist/better_lock.js.map +1 -1
- package/dist/options.d.ts +12 -0
- package/package.json +1 -1
- package/spec/better_lock.spec.ts +60 -0
- package/src/better_lock.ts +26 -23
- package/src/options.ts +15 -0
package/README.md
CHANGED
|
@@ -184,9 +184,6 @@ Most commonly used options are:
|
|
|
184
184
|
- `queue_size`
|
|
185
185
|
Max queue size for waiting jobs.
|
|
186
186
|
|
|
187
|
-
- `queue_ejection_strategy`
|
|
188
|
-
Which job to eject from the queue when it's full. Default is `newest`.
|
|
189
|
-
|
|
190
187
|
Default options are a static member `DEFAULT_OPTIONS` on the `BetterLock` class. During runtime, you can change the defaults like this:
|
|
191
188
|
|
|
192
189
|
```javascript
|
|
@@ -214,6 +211,10 @@ The library is not a good fit if:
|
|
|
214
211
|
|
|
215
212
|
### Change log
|
|
216
213
|
|
|
214
|
+
#### **3.1.0** (_2024/06/21_)
|
|
215
|
+
|
|
216
|
+
Add option `lock_condition`.
|
|
217
|
+
|
|
217
218
|
#### **3.0.0** (_2024/04/18_)
|
|
218
219
|
|
|
219
220
|
Add option `queue_ejection_strategy`.
|
package/dist/better_lock.js
CHANGED
|
@@ -76,23 +76,6 @@ var BetterLock = /** @class */ (function () {
|
|
|
76
76
|
executor = key;
|
|
77
77
|
key = null;
|
|
78
78
|
}
|
|
79
|
-
// Repackage "key" into an array of keys
|
|
80
|
-
var keys = [];
|
|
81
|
-
if (Array.isArray(key)) {
|
|
82
|
-
var arrayOfKeys = key;
|
|
83
|
-
// Make sure we don't add duplicates
|
|
84
|
-
var seenKeys = new Set();
|
|
85
|
-
for (var i = 0; i < arrayOfKeys.length; i++) {
|
|
86
|
-
var normalizedKey = this.normalizeAndValidateKey(arrayOfKeys[i]);
|
|
87
|
-
if (!seenKeys.has(normalizedKey)) {
|
|
88
|
-
keys.push(normalizedKey);
|
|
89
|
-
seenKeys.add(normalizedKey);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
keys.push(this.normalizeAndValidateKey(key));
|
|
95
|
-
}
|
|
96
79
|
// Create callback wrapper for promise interface
|
|
97
80
|
if (!tools.isFunction(callback)) {
|
|
98
81
|
if (jobOptions === undefined) {
|
|
@@ -100,6 +83,31 @@ var BetterLock = /** @class */ (function () {
|
|
|
100
83
|
}
|
|
101
84
|
callback = tools.callbackWithPromise();
|
|
102
85
|
}
|
|
86
|
+
// Prepare job options
|
|
87
|
+
var effectiveJobOptions = jobOptions
|
|
88
|
+
? tools.assign({}, [this.options, jobOptions])
|
|
89
|
+
: // No need to create new object since these will be read-only and are subset of global options
|
|
90
|
+
this.options;
|
|
91
|
+
// Repackage "key" into an array of keys, but only if lock_condition allows it.
|
|
92
|
+
// Otherwise, it stays an array and gets executed immediately.
|
|
93
|
+
var keys = [];
|
|
94
|
+
if (!effectiveJobOptions.lock_condition || effectiveJobOptions.lock_condition(key)) {
|
|
95
|
+
if (Array.isArray(key)) {
|
|
96
|
+
var arrayOfKeys = key;
|
|
97
|
+
// Make sure we don't add duplicates
|
|
98
|
+
var seenKeys = new Set();
|
|
99
|
+
for (var i = 0; i < arrayOfKeys.length; i++) {
|
|
100
|
+
var normalizedKey = this.normalizeAndValidateKey(arrayOfKeys[i]);
|
|
101
|
+
if (!seenKeys.has(normalizedKey)) {
|
|
102
|
+
keys.push(normalizedKey);
|
|
103
|
+
seenKeys.add(normalizedKey);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
keys.push(this.normalizeAndValidateKey(key));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
103
111
|
// Validate other options
|
|
104
112
|
if (!tools.isFunction(executor)) {
|
|
105
113
|
throw new errors_1.BetterLockInvalidArgumentError(this.options.name, 'executor', 'a function', executor);
|
|
@@ -107,11 +115,6 @@ var BetterLock = /** @class */ (function () {
|
|
|
107
115
|
if (!tools.isFunction(callback)) {
|
|
108
116
|
throw new errors_1.BetterLockInvalidArgumentError(this.options.name, 'callback', 'a function', callback);
|
|
109
117
|
}
|
|
110
|
-
// Prepare job options
|
|
111
|
-
var effectiveJobOptions = jobOptions
|
|
112
|
-
? tools.assign({}, [this.options, jobOptions])
|
|
113
|
-
: // No need to create new object since these will be read-only and are subset of global options
|
|
114
|
-
this.options;
|
|
115
118
|
var job = new internals_1.LockJob(keys, executor, callback, effectiveJobOptions);
|
|
116
119
|
// Set incoming stack
|
|
117
120
|
if (effectiveJobOptions.extend_stack_traces) {
|
package/dist/better_lock.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"better_lock.js","sourceRoot":"","sources":["../src/better_lock.ts"],"names":[],"mappings":";;;AAAA,mCAOkB;AAClB,yCAAgD;AAEhD,+BAAiC;AAGjC;;GAEG;AACH;IAaE;;OAEG;IACH,oBAAY,OAAkB;QAC5B,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC,CAAC;QACvE,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC9D,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;IACnB,CAAC;IAED;;OAEG;IACK,6BAAQ,GAAhB,UAAiB,GAAa;QAC5B,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,IAAI,EAAE;YACrC,GAAG,GAAG,oBAAQ,CAAC,iBAAiB,CAAC;SAClC;QACD,IAAI,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,CAAC,KAAK,EAAE;YACV,KAAK,GAAG,IAAI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AAiC;KACvD,CAAC;IA8ZJ,iBAAC;CAAA,AAraD,IAqaC;AAraY,gCAAU"}
|
package/dist/options.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ILockKey } from './types';
|
|
1
2
|
/**
|
|
2
3
|
* Options that apply on LockJob
|
|
3
4
|
*/
|
|
@@ -14,6 +15,12 @@ export interface ILockJobOptions {
|
|
|
14
15
|
* In any error produced by BetterLock, extend the stack trace to include incoming trace. Overrides BetterLockOptions value.
|
|
15
16
|
*/
|
|
16
17
|
extend_stack_traces?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Before acquiring lock, call this function with the given key. If condition is rejected, the executor is immediately called, without locking.
|
|
20
|
+
* This can be used to more conveniently opt out of locking, without duplicating or extracting the executor.
|
|
21
|
+
* Overrides BetterLockOptions value.
|
|
22
|
+
*/
|
|
23
|
+
lock_condition?: (key: ILockKey | Array<ILockKey>) => boolean;
|
|
17
24
|
}
|
|
18
25
|
export declare type IQueueEjectionStrategy = 'oldest' | 'newest';
|
|
19
26
|
/**
|
|
@@ -37,6 +44,11 @@ export interface IOptions extends ILockJobOptions {
|
|
|
37
44
|
* If queue is full, which job to eject. Defaults to 'oldest'.
|
|
38
45
|
*/
|
|
39
46
|
queue_ejection_strategy?: IQueueEjectionStrategy;
|
|
47
|
+
/**
|
|
48
|
+
* Before acquiring lock, call this function with the given key. If condition is rejected, the executor is immediately called, without locking.
|
|
49
|
+
* This can be used to more conveniently opt out of locking, without duplicating or extracting the executor.
|
|
50
|
+
*/
|
|
51
|
+
lock_condition?: (key: ILockKey | Array<ILockKey>) => boolean;
|
|
40
52
|
/**
|
|
41
53
|
* Lock name. This will be written in all logs and error messages, to help you distinguish between different locks
|
|
42
54
|
*/
|
package/package.json
CHANGED
package/spec/better_lock.spec.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import BetterLock from '../src/index';
|
|
2
2
|
import { LockJob } from '../src/internals';
|
|
3
|
+
import { ILockJobOptions } from '../src/options';
|
|
3
4
|
|
|
4
5
|
describe('BetterLock', () => {
|
|
5
6
|
let baseDefaultOptions;
|
|
@@ -626,6 +627,65 @@ describe('BetterLock', () => {
|
|
|
626
627
|
});
|
|
627
628
|
});
|
|
628
629
|
});
|
|
630
|
+
|
|
631
|
+
describe('will respect lock_condition', () => {
|
|
632
|
+
function runJob(
|
|
633
|
+
lock: InstanceType<typeof BetterLock>,
|
|
634
|
+
key: string,
|
|
635
|
+
expectedMs: number,
|
|
636
|
+
options?: ILockJobOptions
|
|
637
|
+
) {
|
|
638
|
+
const startedAt = new Date();
|
|
639
|
+
let executed = false;
|
|
640
|
+
return lock
|
|
641
|
+
.acquire(
|
|
642
|
+
key,
|
|
643
|
+
() => {
|
|
644
|
+
executed = true;
|
|
645
|
+
expectMs(new Date().valueOf() - startedAt.valueOf(), expectedMs);
|
|
646
|
+
return waitPromise(50);
|
|
647
|
+
},
|
|
648
|
+
options
|
|
649
|
+
)
|
|
650
|
+
.finally(() => {
|
|
651
|
+
expect(executed).toBeTruthy();
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
it('for global condition', async () => {
|
|
656
|
+
const lock = new BetterLock({
|
|
657
|
+
lock_condition: key => !String(key).startsWith('a'),
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
await Promise.all([
|
|
661
|
+
runJob(lock, 'a', 0),
|
|
662
|
+
runJob(lock, 'a', 0),
|
|
663
|
+
runJob(lock, 'a', 0),
|
|
664
|
+
runJob(lock, 'b', 0),
|
|
665
|
+
runJob(lock, 'b', 50),
|
|
666
|
+
runJob(lock, 'b', 100),
|
|
667
|
+
]);
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
it('for local condition', async () => {
|
|
671
|
+
const lock = new BetterLock({
|
|
672
|
+
lock_condition: key => !String(key).startsWith('a'),
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
const overrideToB = {
|
|
676
|
+
lock_condition: key => !String(key).startsWith('b'),
|
|
677
|
+
};
|
|
678
|
+
|
|
679
|
+
await Promise.all([
|
|
680
|
+
runJob(lock, 'b', 0, overrideToB),
|
|
681
|
+
runJob(lock, 'b', 0, overrideToB),
|
|
682
|
+
runJob(lock, 'b', 0, overrideToB),
|
|
683
|
+
runJob(lock, 'a', 0, overrideToB),
|
|
684
|
+
runJob(lock, 'a', 50, overrideToB),
|
|
685
|
+
runJob(lock, 'a', 100, overrideToB),
|
|
686
|
+
]);
|
|
687
|
+
});
|
|
688
|
+
});
|
|
629
689
|
});
|
|
630
690
|
|
|
631
691
|
function waitCallback(ms: number, ...args): (done: (...args: any) => void) => void {
|
package/src/better_lock.ts
CHANGED
|
@@ -133,23 +133,6 @@ export class BetterLock {
|
|
|
133
133
|
key = null;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
// Repackage "key" into an array of keys
|
|
137
|
-
const keys = [];
|
|
138
|
-
if (Array.isArray(key)) {
|
|
139
|
-
const arrayOfKeys = key;
|
|
140
|
-
// Make sure we don't add duplicates
|
|
141
|
-
const seenKeys = new Set();
|
|
142
|
-
for (let i = 0; i < arrayOfKeys.length; i++) {
|
|
143
|
-
const normalizedKey = this.normalizeAndValidateKey(arrayOfKeys[i]);
|
|
144
|
-
if (!seenKeys.has(normalizedKey)) {
|
|
145
|
-
keys.push(normalizedKey);
|
|
146
|
-
seenKeys.add(normalizedKey);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
} else {
|
|
150
|
-
keys.push(this.normalizeAndValidateKey(key));
|
|
151
|
-
}
|
|
152
|
-
|
|
153
136
|
// Create callback wrapper for promise interface
|
|
154
137
|
if (!tools.isFunction(callback)) {
|
|
155
138
|
if (jobOptions === undefined) {
|
|
@@ -159,6 +142,32 @@ export class BetterLock {
|
|
|
159
142
|
callback = tools.callbackWithPromise();
|
|
160
143
|
}
|
|
161
144
|
|
|
145
|
+
// Prepare job options
|
|
146
|
+
const effectiveJobOptions = jobOptions
|
|
147
|
+
? tools.assign({} as IOptions, [this.options, jobOptions])
|
|
148
|
+
: // No need to create new object since these will be read-only and are subset of global options
|
|
149
|
+
this.options;
|
|
150
|
+
|
|
151
|
+
// Repackage "key" into an array of keys, but only if lock_condition allows it.
|
|
152
|
+
// Otherwise, it stays an array and gets executed immediately.
|
|
153
|
+
const keys = [];
|
|
154
|
+
if (!effectiveJobOptions.lock_condition || effectiveJobOptions.lock_condition(key)) {
|
|
155
|
+
if (Array.isArray(key)) {
|
|
156
|
+
const arrayOfKeys = key;
|
|
157
|
+
// Make sure we don't add duplicates
|
|
158
|
+
const seenKeys = new Set();
|
|
159
|
+
for (let i = 0; i < arrayOfKeys.length; i++) {
|
|
160
|
+
const normalizedKey = this.normalizeAndValidateKey(arrayOfKeys[i]);
|
|
161
|
+
if (!seenKeys.has(normalizedKey)) {
|
|
162
|
+
keys.push(normalizedKey);
|
|
163
|
+
seenKeys.add(normalizedKey);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
keys.push(this.normalizeAndValidateKey(key));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
162
171
|
// Validate other options
|
|
163
172
|
if (!tools.isFunction(executor)) {
|
|
164
173
|
throw new BetterLockInvalidArgumentError(
|
|
@@ -177,12 +186,6 @@ export class BetterLock {
|
|
|
177
186
|
);
|
|
178
187
|
}
|
|
179
188
|
|
|
180
|
-
// Prepare job options
|
|
181
|
-
const effectiveJobOptions = jobOptions
|
|
182
|
-
? tools.assign({}, [this.options, jobOptions])
|
|
183
|
-
: // No need to create new object since these will be read-only and are subset of global options
|
|
184
|
-
this.options;
|
|
185
|
-
|
|
186
189
|
const job = new LockJob(keys, executor, callback, effectiveJobOptions);
|
|
187
190
|
|
|
188
191
|
// Set incoming stack
|
package/src/options.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ILockKey } from './types';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Options that apply on LockJob
|
|
3
5
|
*/
|
|
@@ -16,6 +18,13 @@ export interface ILockJobOptions {
|
|
|
16
18
|
* In any error produced by BetterLock, extend the stack trace to include incoming trace. Overrides BetterLockOptions value.
|
|
17
19
|
*/
|
|
18
20
|
extend_stack_traces?: boolean;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Before acquiring lock, call this function with the given key. If condition is rejected, the executor is immediately called, without locking.
|
|
24
|
+
* This can be used to more conveniently opt out of locking, without duplicating or extracting the executor.
|
|
25
|
+
* Overrides BetterLockOptions value.
|
|
26
|
+
*/
|
|
27
|
+
lock_condition?: (key: ILockKey | Array<ILockKey>) => boolean;
|
|
19
28
|
}
|
|
20
29
|
|
|
21
30
|
export type IQueueEjectionStrategy = 'oldest' | 'newest';
|
|
@@ -45,6 +54,12 @@ export interface IOptions extends ILockJobOptions {
|
|
|
45
54
|
*/
|
|
46
55
|
queue_ejection_strategy?: IQueueEjectionStrategy;
|
|
47
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Before acquiring lock, call this function with the given key. If condition is rejected, the executor is immediately called, without locking.
|
|
59
|
+
* This can be used to more conveniently opt out of locking, without duplicating or extracting the executor.
|
|
60
|
+
*/
|
|
61
|
+
lock_condition?: (key: ILockKey | Array<ILockKey>) => boolean;
|
|
62
|
+
|
|
48
63
|
/**
|
|
49
64
|
* Lock name. This will be written in all logs and error messages, to help you distinguish between different locks
|
|
50
65
|
*/
|