@daiso-tech/core 0.24.0 → 0.26.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 +3 -2
- package/dist/async/utilities/lazy-promise/lazy-promise.d.ts +12 -3
- package/dist/async/utilities/lazy-promise/lazy-promise.js +37 -4
- package/dist/async/utilities/lazy-promise/lazy-promise.js.map +1 -1
- package/dist/cache/contracts/cache.contract.d.ts +4 -4
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/kysely-sqlite-cache-adapter.js +2 -2
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/kysely-sqlite-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/memory-cache-adapter/memory-cache-adapter.js +4 -4
- package/dist/cache/implementations/adapters/memory-cache-adapter/memory-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js +2 -2
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.js +5 -5
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/derivables/cache/cache.d.ts +70 -184
- package/dist/cache/implementations/derivables/cache/cache.js +68 -185
- package/dist/cache/implementations/derivables/cache/cache.js.map +1 -1
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.d.ts +11 -15
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.js +0 -11
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.js.map +1 -1
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js +5 -5
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
- package/dist/collection/implementations/iterable-collection/iterable-collection.js +5 -5
- package/dist/collection/implementations/iterable-collection/iterable-collection.js.map +1 -1
- package/dist/collection/implementations/list-collection/list-collection.js +5 -5
- package/dist/collection/implementations/list-collection/list-collection.js.map +1 -1
- package/dist/event-bus/contracts/_shared.d.ts +0 -7
- package/dist/event-bus/contracts/_shared.js.map +1 -1
- package/dist/event-bus/contracts/event-bus-adapter.contract.d.ts +6 -5
- package/dist/event-bus/contracts/event-bus.contract.d.ts +21 -16
- package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.d.ts +4 -3
- package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.js +3 -3
- package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.js.map +1 -1
- package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.d.ts +4 -3
- package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.js.map +1 -1
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.d.ts +4 -3
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js +3 -3
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.d.ts +64 -33
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +92 -33
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js +957 -201
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js.map +1 -1
- package/dist/lock/contracts/lock-provider.contract.d.ts +2 -2
- package/dist/lock/contracts/lock.contract.d.ts +24 -6
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js +2 -2
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js +4 -4
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js +2 -2
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js +4 -4
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.d.ts +90 -48
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js +67 -43
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.d.ts +4 -23
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js +8 -2
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock.d.ts +744 -11
- package/dist/lock/implementations/derivables/lock-provider/lock.js +770 -3
- package/dist/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.d.ts +36 -5
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js +10 -4
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.d.ts +1 -0
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js +417 -0
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js.map +1 -1
- package/dist/serde/implementations/derivables/serde.js +4 -4
- package/dist/serde/implementations/derivables/serde.js.map +1 -1
- package/dist/utilities/{constants.js → _constants.js} +1 -1
- package/dist/utilities/_constants.js.map +1 -0
- package/dist/utilities/_module-exports.d.ts +1 -1
- package/dist/utilities/_module-exports.js +1 -1
- package/dist/utilities/_module-exports.js.map +1 -1
- package/dist/utilities/contracts/_module.d.ts +0 -1
- package/dist/utilities/contracts/_module.js +0 -1
- package/dist/utilities/contracts/_module.js.map +1 -1
- package/dist/utilities/functions.d.ts +3 -7
- package/dist/utilities/functions.js +3 -10
- package/dist/utilities/functions.js.map +1 -1
- package/dist/utilities/pipeline/_module.d.ts +1 -0
- package/dist/utilities/pipeline/_module.js +2 -0
- package/dist/utilities/pipeline/_module.js.map +1 -0
- package/dist/utilities/pipeline/pipeline.d.ts +45 -0
- package/dist/utilities/pipeline/pipeline.js +75 -0
- package/dist/utilities/pipeline/pipeline.js.map +1 -0
- package/dist/utilities/time-span/time-span.d.ts +3 -12
- package/dist/utilities/time-span/time-span.js +3 -6
- package/dist/utilities/time-span/time-span.js.map +1 -1
- package/dist/utilities/types.d.ts +19 -2
- package/package.json +1 -1
- package/dist/utilities/constants.js.map +0 -1
- package/dist/utilities/contracts/buildable.contract.d.ts +0 -11
- package/dist/utilities/contracts/buildable.contract.js +0 -5
- package/dist/utilities/contracts/buildable.contract.js.map +0 -1
- /package/dist/utilities/{constants.d.ts → _constants.d.ts} +0 -0
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
|
-
import type { LazyPromiseable, Result } from "../../../../utilities/_module-exports.js";
|
|
4
|
+
import type { Invokable, LazyPromiseable, Result } from "../../../../utilities/_module-exports.js";
|
|
5
5
|
import type { TimeSpan } from "../../../../utilities/_module-exports.js";
|
|
6
6
|
import type { BackoffPolicy, RetryPolicy } from "../../../../async/_module-exports.js";
|
|
7
7
|
import { LazyPromise } from "../../../../async/_module-exports.js";
|
|
8
|
-
import type { ILockAdapter, LockEvents } from "../../../../lock/contracts/_module-exports.js";
|
|
8
|
+
import type { AquireBlockingSettings, ILockAdapter, LockEvents } from "../../../../lock/contracts/_module-exports.js";
|
|
9
9
|
import { KeyAlreadyAcquiredLockError, type ILock } from "../../../../lock/contracts/_module-exports.js";
|
|
10
|
-
import type { EventClass,
|
|
10
|
+
import type { EventClass, EventInstance, Unsubscribe, IGroupableEventBus, IEventDispatcher } from "../../../../event-bus/contracts/_module-exports.js";
|
|
11
11
|
import { type ILockStateRecord } from "../../../../lock/implementations/derivables/lock-provider/lock-state.js";
|
|
12
12
|
/**
|
|
13
13
|
* @internal
|
|
@@ -28,6 +28,8 @@ export type LockSettings = {
|
|
|
28
28
|
adapter: ILockAdapter;
|
|
29
29
|
key: string;
|
|
30
30
|
owner: string;
|
|
31
|
+
defaultBlockingInterval: TimeSpan;
|
|
32
|
+
defaultBlockingTime: TimeSpan;
|
|
31
33
|
defaultRefreshTime: TimeSpan;
|
|
32
34
|
ttl: TimeSpan | null;
|
|
33
35
|
expirationInMs: number | null;
|
|
@@ -40,9 +42,13 @@ export type LockSettings = {
|
|
|
40
42
|
stateRecord: ILockStateRecord;
|
|
41
43
|
};
|
|
42
44
|
/**
|
|
43
|
-
*
|
|
45
|
+
* IMPORTANT: This class is not intended to be instantiated directly, instead it should be created by the <i>LockProvider</i> class instance.
|
|
46
|
+
* @group Derivables
|
|
44
47
|
*/
|
|
45
48
|
export declare class Lock implements ILock {
|
|
49
|
+
/**
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
46
52
|
static serialize(deserializedValue: Lock): ISerializedLock;
|
|
47
53
|
private readonly lockProviderEventDispatcher;
|
|
48
54
|
private readonly lockEventBus;
|
|
@@ -50,15 +56,478 @@ export declare class Lock implements ILock {
|
|
|
50
56
|
private readonly key;
|
|
51
57
|
private readonly owner;
|
|
52
58
|
private readonly ttl;
|
|
59
|
+
private readonly defaultBlockingInterval;
|
|
60
|
+
private readonly defaultBlockingTime;
|
|
53
61
|
private readonly defaultRefreshTime;
|
|
54
62
|
private readonly lazyPromiseSettings;
|
|
55
63
|
private readonly state;
|
|
64
|
+
/**
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
56
67
|
constructor(settings: LockSettings);
|
|
57
68
|
private createLayPromise;
|
|
69
|
+
/**
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
73
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
74
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
75
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
76
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
77
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
78
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
79
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
80
|
+
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
81
|
+
* import { delay } from "@daiso-tech/core/async";
|
|
82
|
+
*
|
|
83
|
+
* const eventBus = new EventBus({
|
|
84
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
85
|
+
* });
|
|
86
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
87
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
88
|
+
* serde,
|
|
89
|
+
* adapter: new MemoryLockAdapter({
|
|
90
|
+
* rootGroup: "@global"
|
|
91
|
+
* }),
|
|
92
|
+
* eventBus,
|
|
93
|
+
* });
|
|
94
|
+
*
|
|
95
|
+
* const lock = lockProvider.create("a");
|
|
96
|
+
*
|
|
97
|
+
* async function fn(): Promise<void> {
|
|
98
|
+
* await lock.run(async () => {
|
|
99
|
+
* console.log("START: ", 1);
|
|
100
|
+
* // Let’s pretend we’re doing async database work.
|
|
101
|
+
* await delay(TimeSpan.fromSeconds(2));
|
|
102
|
+
* console.log("END: ", 2);
|
|
103
|
+
* });
|
|
104
|
+
* }
|
|
105
|
+
*
|
|
106
|
+
* await Promise.allSettled([
|
|
107
|
+
* fn(),
|
|
108
|
+
* fn(),
|
|
109
|
+
* fn(),
|
|
110
|
+
* fn(),
|
|
111
|
+
* ]);
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* You can also pass an <i>{@link LazyPromise}</i> instead of async function.
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
118
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
119
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
120
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
121
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
122
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
123
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
124
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
125
|
+
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
126
|
+
* import { delay } from "@daiso-tech/core/async";
|
|
127
|
+
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
128
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
129
|
+
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
130
|
+
*
|
|
131
|
+
* const eventBus = new EventBus({
|
|
132
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
133
|
+
* });
|
|
134
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
135
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
136
|
+
* serde,
|
|
137
|
+
* adapter: new MemoryLockAdapter({
|
|
138
|
+
* rootGroup: "@global"
|
|
139
|
+
* }),
|
|
140
|
+
* eventBus,
|
|
141
|
+
* });
|
|
142
|
+
*
|
|
143
|
+
* const cache: IGroupableCache = new Cache({
|
|
144
|
+
* // Let's pretend when the cache adapter increments a key it will occur in 2 async, get the value, increment the value in memory and update the key.
|
|
145
|
+
* adapter: new MemoryCacheAdapter({
|
|
146
|
+
* rootGroup: "@global"
|
|
147
|
+
* }),
|
|
148
|
+
* eventBus,
|
|
149
|
+
* });
|
|
150
|
+
* await cache.put("a", 0);
|
|
151
|
+
*
|
|
152
|
+
* const lock = lockProvider.create("a");
|
|
153
|
+
*
|
|
154
|
+
* async function fn(): Promise<void> {
|
|
155
|
+
* // The lock will ensure the key will be incremented correctly
|
|
156
|
+
* await lock.run(cache.increment("a", 1));
|
|
157
|
+
* }
|
|
158
|
+
*
|
|
159
|
+
* await Promise.allSettled([
|
|
160
|
+
* fn(),
|
|
161
|
+
* fn(),
|
|
162
|
+
* fn(),
|
|
163
|
+
* fn(),
|
|
164
|
+
* ]);
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
58
167
|
run<TValue = void>(asyncFn: LazyPromiseable<TValue>): LazyPromise<Result<TValue, KeyAlreadyAcquiredLockError>>;
|
|
168
|
+
/**
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
172
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
173
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
174
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
175
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
176
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
177
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
178
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
179
|
+
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
180
|
+
* import { delay } from "@daiso-tech/core/async";
|
|
181
|
+
*
|
|
182
|
+
* const eventBus = new EventBus({
|
|
183
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
184
|
+
* });
|
|
185
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
186
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
187
|
+
* serde,
|
|
188
|
+
* adapter: new MemoryLockAdapter({
|
|
189
|
+
* rootGroup: "@global"
|
|
190
|
+
* }),
|
|
191
|
+
* eventBus,
|
|
192
|
+
* });
|
|
193
|
+
*
|
|
194
|
+
* const lock = lockProvider.create("a");
|
|
195
|
+
*
|
|
196
|
+
* async function fn(): Promise<void> {
|
|
197
|
+
* await lock.runOrFail(async () => {
|
|
198
|
+
* console.log("START: ", 1);
|
|
199
|
+
* // Let’s pretend we’re doing async database work.
|
|
200
|
+
* await delay(TimeSpan.fromSeconds(2));
|
|
201
|
+
* console.log("END: ", 2);
|
|
202
|
+
* })
|
|
203
|
+
* // Retries 4 times to acquire the lock, then throws an error.
|
|
204
|
+
* .setRetryAttempts(4);
|
|
205
|
+
* }
|
|
206
|
+
*
|
|
207
|
+
* await Promise.allSettled([
|
|
208
|
+
* fn(),
|
|
209
|
+
* fn(),
|
|
210
|
+
* fn(),
|
|
211
|
+
* fn(),
|
|
212
|
+
* ]);
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* You can also pass an <i>{@link LazyPromise}</i> instead of async function.
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
219
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
220
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
221
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
222
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
223
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
224
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
225
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
226
|
+
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
227
|
+
* import { delay } from "@daiso-tech/core/async";
|
|
228
|
+
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
229
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
230
|
+
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
231
|
+
*
|
|
232
|
+
* const eventBus = new EventBus({
|
|
233
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
234
|
+
* });
|
|
235
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
236
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
237
|
+
* serde,
|
|
238
|
+
* adapter: new MemoryLockAdapter({
|
|
239
|
+
* rootGroup: "@global"
|
|
240
|
+
* }),
|
|
241
|
+
* eventBus,
|
|
242
|
+
* });
|
|
243
|
+
*
|
|
244
|
+
* const cache: IGroupableCache = new Cache({
|
|
245
|
+
* // Let's pretend when the cache adapter increments a key it will occur in 2 async, get the value, increment the value in memory and update the key.
|
|
246
|
+
* adapter: new MemoryCacheAdapter({
|
|
247
|
+
* rootGroup: "@global"
|
|
248
|
+
* }),
|
|
249
|
+
* eventBus,
|
|
250
|
+
* });
|
|
251
|
+
* await cache.put("a", 0);
|
|
252
|
+
*
|
|
253
|
+
* const lock = lockProvider.create("a");
|
|
254
|
+
*
|
|
255
|
+
* async function fn(): Promise<void> {
|
|
256
|
+
* await lock
|
|
257
|
+
* // The lock will ensure the key will be incremented correctly
|
|
258
|
+
* .runOrFail(cache.increment("a", 1));
|
|
259
|
+
* // Retries 4 times to acquire the lock, then throws an error.
|
|
260
|
+
* .setRetryAttempts(4);
|
|
261
|
+
* }
|
|
262
|
+
*
|
|
263
|
+
* await Promise.allSettled([
|
|
264
|
+
* fn(),
|
|
265
|
+
* fn(),
|
|
266
|
+
* fn(),
|
|
267
|
+
* fn(),
|
|
268
|
+
* ]);
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
59
271
|
runOrFail<TValue = void>(asyncFn: LazyPromiseable<TValue>): LazyPromise<TValue>;
|
|
272
|
+
/**
|
|
273
|
+
* @example
|
|
274
|
+
* ```ts
|
|
275
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
276
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
277
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
278
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
279
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
280
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
281
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
282
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
283
|
+
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
284
|
+
* import { delay } from "@daiso-tech/core/async";
|
|
285
|
+
*
|
|
286
|
+
* const eventBus = new EventBus({
|
|
287
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
288
|
+
* });
|
|
289
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
290
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
291
|
+
* serde,
|
|
292
|
+
* adapter: new MemoryLockAdapter({
|
|
293
|
+
* rootGroup: "@global"
|
|
294
|
+
* }),
|
|
295
|
+
* eventBus,
|
|
296
|
+
* });
|
|
297
|
+
*
|
|
298
|
+
* const lock = lockProvider.create("a");
|
|
299
|
+
*
|
|
300
|
+
* async function fn(): Promise<void> {
|
|
301
|
+
* await lock.runBlocking(async () => {
|
|
302
|
+
* console.log("START: ", 1);
|
|
303
|
+
* // Let’s pretend we’re doing async database work.
|
|
304
|
+
* await delay(TimeSpan.fromSeconds(2));
|
|
305
|
+
* console.log("END: ", 2);
|
|
306
|
+
* });
|
|
307
|
+
* }
|
|
308
|
+
*
|
|
309
|
+
* await Promise.allSettled([
|
|
310
|
+
* fn(),
|
|
311
|
+
* fn(),
|
|
312
|
+
* fn(),
|
|
313
|
+
* fn(),
|
|
314
|
+
* ]);
|
|
315
|
+
* ```
|
|
316
|
+
*
|
|
317
|
+
* You can also pass an <i>{@link LazyPromise}</i> instead of async function.
|
|
318
|
+
* @example
|
|
319
|
+
* ```ts
|
|
320
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
321
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
322
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
323
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
324
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
325
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
326
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
327
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
328
|
+
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
329
|
+
* import { delay } from "@daiso-tech/core/async";
|
|
330
|
+
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
331
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
332
|
+
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
333
|
+
*
|
|
334
|
+
* const eventBus = new EventBus({
|
|
335
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
336
|
+
* });
|
|
337
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
338
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
339
|
+
* serde,
|
|
340
|
+
* adapter: new MemoryLockAdapter({
|
|
341
|
+
* rootGroup: "@global"
|
|
342
|
+
* }),
|
|
343
|
+
* eventBus,
|
|
344
|
+
* });
|
|
345
|
+
*
|
|
346
|
+
* const cache: IGroupableCache = new Cache({
|
|
347
|
+
* // Let's pretend when the cache adapter increments a key it will occur in 2 async, get the value, increment the value in memory and update the key.
|
|
348
|
+
* adapter: new MemoryCacheAdapter({
|
|
349
|
+
* rootGroup: "@global"
|
|
350
|
+
* }),
|
|
351
|
+
* eventBus,
|
|
352
|
+
* });
|
|
353
|
+
* await cache.put("a", 0);
|
|
354
|
+
*
|
|
355
|
+
* const lock = lockProvider.create("a");
|
|
356
|
+
*
|
|
357
|
+
* async function fn(): Promise<void> {
|
|
358
|
+
* // The lock will ensure the key will be incremented correctly
|
|
359
|
+
* await lock.runBlocking(cache.increment("a", 1));
|
|
360
|
+
* }
|
|
361
|
+
*
|
|
362
|
+
* await Promise.allSettled([
|
|
363
|
+
* fn(),
|
|
364
|
+
* fn(),
|
|
365
|
+
* fn(),
|
|
366
|
+
* fn(),
|
|
367
|
+
* ]);
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
runBlocking<TValue = void>(asyncFn: LazyPromiseable<TValue>, settings?: AquireBlockingSettings): LazyPromise<Result<TValue, KeyAlreadyAcquiredLockError>>;
|
|
371
|
+
/**
|
|
372
|
+
* @example
|
|
373
|
+
* ```ts
|
|
374
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
375
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
376
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
377
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
378
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
379
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
380
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
381
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
382
|
+
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
383
|
+
* import { delay } from "@daiso-tech/core/async";
|
|
384
|
+
*
|
|
385
|
+
* const eventBus = new EventBus({
|
|
386
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
387
|
+
* });
|
|
388
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
389
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
390
|
+
* serde,
|
|
391
|
+
* adapter: new MemoryLockAdapter({
|
|
392
|
+
* rootGroup: "@global"
|
|
393
|
+
* }),
|
|
394
|
+
* eventBus,
|
|
395
|
+
* });
|
|
396
|
+
*
|
|
397
|
+
* const lock = lockProvider.create("a");
|
|
398
|
+
*
|
|
399
|
+
* async function fn(): Promise<void> {
|
|
400
|
+
* // Use try-finally when acquiring a lock to ensure it’s released if an error happens.
|
|
401
|
+
* try {
|
|
402
|
+
* const hasAquired = await lock.acquire();
|
|
403
|
+
* if (!hasAquired) {
|
|
404
|
+
* return;
|
|
405
|
+
* }
|
|
406
|
+
* console.log("START: ", 1);
|
|
407
|
+
* // Let’s pretend we’re doing async database work.
|
|
408
|
+
* await delay(TimeSpan.fromSeconds(2));
|
|
409
|
+
* console.log("END: ", 2);
|
|
410
|
+
* }
|
|
411
|
+
* finally {
|
|
412
|
+
* await lock.release();
|
|
413
|
+
* }
|
|
414
|
+
* }
|
|
415
|
+
*
|
|
416
|
+
* await Promise.allSettled([
|
|
417
|
+
* fn(),
|
|
418
|
+
* fn(),
|
|
419
|
+
* fn(),
|
|
420
|
+
* fn(),
|
|
421
|
+
* ]);
|
|
422
|
+
* ```
|
|
423
|
+
*/
|
|
60
424
|
acquire(): LazyPromise<boolean>;
|
|
425
|
+
/**
|
|
426
|
+
* @example
|
|
427
|
+
* ```ts
|
|
428
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
429
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
430
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
431
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
432
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
433
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
434
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
435
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
436
|
+
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
437
|
+
* import { delay } from "@daiso-tech/core/async";
|
|
438
|
+
*
|
|
439
|
+
* const eventBus = new EventBus({
|
|
440
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
441
|
+
* });
|
|
442
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
443
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
444
|
+
* serde,
|
|
445
|
+
* adapter: new MemoryLockAdapter({
|
|
446
|
+
* rootGroup: "@global"
|
|
447
|
+
* }),
|
|
448
|
+
* eventBus,
|
|
449
|
+
* });
|
|
450
|
+
*
|
|
451
|
+
* const lock = lockProvider.create("a");
|
|
452
|
+
*
|
|
453
|
+
* async function fn(): Promise<void> {
|
|
454
|
+
* // Use try-finally when acquiring a lock to ensure it’s released if an error happens.
|
|
455
|
+
* try {
|
|
456
|
+
* // Retries 4 times to acquire the lock, then throws an error.
|
|
457
|
+
* await lock.acquireOrFail().setRetryAttempts(4);
|
|
458
|
+
* console.log("START: ", 1);
|
|
459
|
+
* // Let’s pretend we’re doing async database work.
|
|
460
|
+
* await delay(TimeSpan.fromSeconds(2));
|
|
461
|
+
* console.log("END: ", 2);
|
|
462
|
+
* }
|
|
463
|
+
* finally {
|
|
464
|
+
* await lock.release();
|
|
465
|
+
* }
|
|
466
|
+
* }
|
|
467
|
+
*
|
|
468
|
+
* await Promise.allSettled([
|
|
469
|
+
* fn(),
|
|
470
|
+
* fn(),
|
|
471
|
+
* fn(),
|
|
472
|
+
* fn(),
|
|
473
|
+
* ]);
|
|
474
|
+
* ```
|
|
475
|
+
*/
|
|
61
476
|
acquireOrFail(): LazyPromise<void>;
|
|
477
|
+
/**
|
|
478
|
+
* @example
|
|
479
|
+
* ```ts
|
|
480
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
481
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
482
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
483
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
484
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
485
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
486
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
487
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
488
|
+
* import { TimeSpan } from "@daiso-tech/core/utilities";
|
|
489
|
+
* import { delay } from "@daiso-tech/core/async";
|
|
490
|
+
*
|
|
491
|
+
* const eventBus = new EventBus({
|
|
492
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
493
|
+
* });
|
|
494
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
495
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
496
|
+
* serde,
|
|
497
|
+
* adapter: new MemoryLockAdapter({
|
|
498
|
+
* rootGroup: "@global"
|
|
499
|
+
* }),
|
|
500
|
+
* eventBus,
|
|
501
|
+
* });
|
|
502
|
+
*
|
|
503
|
+
* const lock = lockProvider.create("a");
|
|
504
|
+
*
|
|
505
|
+
* async function fn(): Promise<void> {
|
|
506
|
+
* // Use try-finally when acquiring a lock to ensure it’s released if an error happens.
|
|
507
|
+
* try {
|
|
508
|
+
* const hasAquired = await lock.acquireBlocking();
|
|
509
|
+
* if (!hasAquired) {
|
|
510
|
+
* return;
|
|
511
|
+
* }
|
|
512
|
+
* console.log("START: ", 1);
|
|
513
|
+
* // Let’s pretend we’re doing async database work.
|
|
514
|
+
* await delay(TimeSpan.fromSeconds(2));
|
|
515
|
+
* console.log("END: ", 2);
|
|
516
|
+
* }
|
|
517
|
+
* finally {
|
|
518
|
+
* await lock.release();
|
|
519
|
+
* }
|
|
520
|
+
* }
|
|
521
|
+
*
|
|
522
|
+
* await Promise.allSettled([
|
|
523
|
+
* fn(),
|
|
524
|
+
* fn(),
|
|
525
|
+
* fn(),
|
|
526
|
+
* fn(),
|
|
527
|
+
* ]);
|
|
528
|
+
* ```
|
|
529
|
+
*/
|
|
530
|
+
acquireBlocking(settings?: AquireBlockingSettings): LazyPromise<boolean>;
|
|
62
531
|
release(): LazyPromise<boolean>;
|
|
63
532
|
releaseOrFail(): LazyPromise<void>;
|
|
64
533
|
forceRelease(): LazyPromise<void>;
|
|
@@ -68,11 +537,275 @@ export declare class Lock implements ILock {
|
|
|
68
537
|
refreshOrFail(ttl?: TimeSpan): LazyPromise<void>;
|
|
69
538
|
getRemainingTime(): LazyPromise<TimeSpan | null>;
|
|
70
539
|
getOwner(): LazyPromise<string>;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
540
|
+
/**
|
|
541
|
+
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
542
|
+
*
|
|
543
|
+
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
544
|
+
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
545
|
+
* @example
|
|
546
|
+
* ```ts
|
|
547
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
548
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
549
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
550
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
551
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
552
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
553
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
554
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
555
|
+
*
|
|
556
|
+
* const eventBus = new EventBus({
|
|
557
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
558
|
+
* });
|
|
559
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
560
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
561
|
+
* serde,
|
|
562
|
+
* adapter: new MemoryLockAdapter({
|
|
563
|
+
* rootGroup: "@global"
|
|
564
|
+
* }),
|
|
565
|
+
* eventBus,
|
|
566
|
+
* });
|
|
567
|
+
*
|
|
568
|
+
* const lock = lockProvider.create("a");
|
|
569
|
+
*
|
|
570
|
+
* const listener: Invokable <LockEvents> = event => {
|
|
571
|
+
* console.log(event);
|
|
572
|
+
* }
|
|
573
|
+
* await lock.addListener(KeyAcquiredLockEvent, listener);
|
|
574
|
+
* await lock.acquire();
|
|
575
|
+
* ```
|
|
576
|
+
*/
|
|
577
|
+
addListener<TEventClass extends EventClass<LockEvents>>(event: TEventClass, listener: Invokable<EventInstance<TEventClass>>): LazyPromise<void>;
|
|
578
|
+
/**
|
|
579
|
+
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
580
|
+
*
|
|
581
|
+
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
582
|
+
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
583
|
+
* @example
|
|
584
|
+
* ```ts
|
|
585
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
586
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
587
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
588
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
589
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
590
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
591
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
592
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
593
|
+
*
|
|
594
|
+
* const eventBus = new EventBus({
|
|
595
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
596
|
+
* });
|
|
597
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
598
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
599
|
+
* serde,
|
|
600
|
+
* adapter: new MemoryLockAdapter({
|
|
601
|
+
* rootGroup: "@global"
|
|
602
|
+
* }),
|
|
603
|
+
* eventBus,
|
|
604
|
+
* });
|
|
605
|
+
*
|
|
606
|
+
* const lock = lockProvider.create("a");
|
|
607
|
+
*
|
|
608
|
+
* const listener: Invokable <LockEvents> = event => {
|
|
609
|
+
* console.log(event);
|
|
610
|
+
* }
|
|
611
|
+
* await lock.addListenerMany([KeyAcquiredLockEvent], listener);
|
|
612
|
+
* await lock.acquire();
|
|
613
|
+
* ```
|
|
614
|
+
*/
|
|
615
|
+
addListenerMany<TEventClass extends EventClass<LockEvents>>(events: TEventClass[], listener: Invokable<EventInstance<TEventClass>>): LazyPromise<void>;
|
|
616
|
+
/**
|
|
617
|
+
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
618
|
+
*
|
|
619
|
+
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
620
|
+
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
621
|
+
* @example
|
|
622
|
+
* ```ts
|
|
623
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
624
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
625
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
626
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
627
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
628
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
629
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
630
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
631
|
+
*
|
|
632
|
+
* const eventBus = new EventBus({
|
|
633
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
634
|
+
* });
|
|
635
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
636
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
637
|
+
* serde,
|
|
638
|
+
* adapter: new MemoryLockAdapter({
|
|
639
|
+
* rootGroup: "@global"
|
|
640
|
+
* }),
|
|
641
|
+
* eventBus,
|
|
642
|
+
* });
|
|
643
|
+
*
|
|
644
|
+
* const lock = lockProvider.create("a");
|
|
645
|
+
*
|
|
646
|
+
* const listener: Invokable <LockEvents> = event => {
|
|
647
|
+
* console.log(event);
|
|
648
|
+
* }
|
|
649
|
+
* await lock.addListener(KeyAcquiredLockEvent, listener);
|
|
650
|
+
* await lock.removeListener(KeyAcquiredLockEvent, listener);
|
|
651
|
+
* await lock.acquire();
|
|
652
|
+
* ```
|
|
653
|
+
*/
|
|
654
|
+
removeListener<TEventClass extends EventClass<LockEvents>>(event: TEventClass, listener: Invokable<EventInstance<TEventClass>>): LazyPromise<void>;
|
|
655
|
+
/**
|
|
656
|
+
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
657
|
+
*
|
|
658
|
+
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
659
|
+
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
660
|
+
* @example
|
|
661
|
+
* ```ts
|
|
662
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
663
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
664
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
665
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
666
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
667
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
668
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
669
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
670
|
+
*
|
|
671
|
+
* const eventBus = new EventBus({
|
|
672
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
673
|
+
* });
|
|
674
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
675
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
676
|
+
* serde,
|
|
677
|
+
* adapter: new MemoryLockAdapter({
|
|
678
|
+
* rootGroup: "@global"
|
|
679
|
+
* }),
|
|
680
|
+
* eventBus,
|
|
681
|
+
* });
|
|
682
|
+
*
|
|
683
|
+
* const lock = lockProvider.create("a");
|
|
684
|
+
*
|
|
685
|
+
* const listener: Invokable <LockEvents> = event => {
|
|
686
|
+
* console.log(event);
|
|
687
|
+
* }
|
|
688
|
+
* await lock.addListenerMany([KeyAcquiredLockEvent], listener);
|
|
689
|
+
* await lock.removeListenerMany([KeyAcquiredLockEvent], listener);
|
|
690
|
+
* await lock.acquire();
|
|
691
|
+
* ```
|
|
692
|
+
*/
|
|
693
|
+
removeListenerMany<TEventClass extends EventClass<LockEvents>>(events: TEventClass[], listener: Invokable<EventInstance<TEventClass>>): LazyPromise<void>;
|
|
694
|
+
/**
|
|
695
|
+
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
696
|
+
*
|
|
697
|
+
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
698
|
+
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
699
|
+
* @example
|
|
700
|
+
* ```ts
|
|
701
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
702
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
703
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
704
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
705
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
706
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
707
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
708
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
709
|
+
*
|
|
710
|
+
* const eventBus = new EventBus({
|
|
711
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
712
|
+
* });
|
|
713
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
714
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
715
|
+
* serde,
|
|
716
|
+
* adapter: new MemoryLockAdapter({
|
|
717
|
+
* rootGroup: "@global"
|
|
718
|
+
* }),
|
|
719
|
+
* eventBus,
|
|
720
|
+
* });
|
|
721
|
+
*
|
|
722
|
+
* const lock = lockProvider.create("a");
|
|
723
|
+
*
|
|
724
|
+
* const listener: Invokable <LockEvents> = event => {
|
|
725
|
+
* console.log(event);
|
|
726
|
+
* }
|
|
727
|
+
* await lock.listenOnce(KeyAcquiredLockEvent, listener);
|
|
728
|
+
* await lock.acquire();
|
|
729
|
+
* ```
|
|
730
|
+
*/
|
|
731
|
+
listenOnce<TEventClass extends EventClass<LockEvents>>(event: TEventClass, listener: Invokable<EventInstance<TEventClass>>): LazyPromise<void>;
|
|
732
|
+
asPromise<TEventClass extends EventClass<LockEvents>>(event: TEventClass): LazyPromise<EventInstance<TEventClass>>;
|
|
733
|
+
/**
|
|
734
|
+
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
735
|
+
*
|
|
736
|
+
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
737
|
+
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
738
|
+
* @example
|
|
739
|
+
* ```ts
|
|
740
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
741
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
742
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
743
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
744
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
745
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
746
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
747
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
748
|
+
*
|
|
749
|
+
* const eventBus = new EventBus({
|
|
750
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
751
|
+
* });
|
|
752
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
753
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
754
|
+
* serde,
|
|
755
|
+
* adapter: new MemoryLockAdapter({
|
|
756
|
+
* rootGroup: "@global"
|
|
757
|
+
* }),
|
|
758
|
+
* eventBus,
|
|
759
|
+
* });
|
|
760
|
+
*
|
|
761
|
+
* const lock = lockProvider.create("a");
|
|
762
|
+
*
|
|
763
|
+
* const listener: Invokable <LockEvents> = event => {
|
|
764
|
+
* console.log(event);
|
|
765
|
+
* }
|
|
766
|
+
* const unsubscribe = await lock.subscribe(KeyAcquiredLockEvent, listener);
|
|
767
|
+
* await lock.acquire();
|
|
768
|
+
* await unsubscribe();
|
|
769
|
+
* ```
|
|
770
|
+
*/
|
|
771
|
+
subscribe<TEventClass extends EventClass<LockEvents>>(event: TEventClass, listener: Invokable<EventInstance<TEventClass>>): LazyPromise<Unsubscribe>;
|
|
772
|
+
/**
|
|
773
|
+
* You can listen to different events of the current <i>Lock</i> class instance.
|
|
774
|
+
*
|
|
775
|
+
* Refer to <i>{@link LockEvents}</i>, to se all events dispatched by <i>LockProvider</i> class instance.
|
|
776
|
+
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
777
|
+
* @example
|
|
778
|
+
* ```ts
|
|
779
|
+
* import { type IGroupableLockProvider, type LockEvents, KeyAcquiredLockEvent } from "@daiso-tech/core/lock/contracts";
|
|
780
|
+
* import { LockProvider } from "@daiso-tech/core/lock/implementations/derivables";
|
|
781
|
+
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/implementations/adapters";
|
|
782
|
+
* import type { Invokable } from "@daiso-tech/core/event-bus/contracts";
|
|
783
|
+
* import { EventBus } from "@daiso-tech/core/event-bus/implementations/derivables";
|
|
784
|
+
* import { MemoryEventBusAdapter } from "@daiso-tech/core/event-bus/implementations/adapters";
|
|
785
|
+
* import { Serde } from "@daiso-tech/core/serde/implementations/derivables";
|
|
786
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/implementations/adapters";
|
|
787
|
+
*
|
|
788
|
+
* const eventBus = new EventBus({
|
|
789
|
+
* adapter: new MemoryEventBusAdapter({ rootGroup: "@global" })
|
|
790
|
+
* });
|
|
791
|
+
* const serde = new Serde(SuperJsonSerdeAdapter);
|
|
792
|
+
* const lockProvider: IGroupableLockProvider = new LockProvider({
|
|
793
|
+
* serde,
|
|
794
|
+
* adapter: new MemoryLockAdapter({
|
|
795
|
+
* rootGroup: "@global"
|
|
796
|
+
* }),
|
|
797
|
+
* eventBus,
|
|
798
|
+
* });
|
|
799
|
+
*
|
|
800
|
+
* const lock = lockProvider.create("a");
|
|
801
|
+
*
|
|
802
|
+
* const listener: Invokable <LockEvents> = event => {
|
|
803
|
+
* console.log(event);
|
|
804
|
+
* }
|
|
805
|
+
* const unsubscribe = await lock.subscribeMany([KeyAcquiredLockEvent], listener);
|
|
806
|
+
* await lock.acquire();
|
|
807
|
+
* await unsubscribe();
|
|
808
|
+
* ```
|
|
809
|
+
*/
|
|
810
|
+
subscribeMany<TEventClass extends EventClass<LockEvents>>(events: TEventClass[], listener: Invokable<EventInstance<TEventClass>>): LazyPromise<Unsubscribe>;
|
|
78
811
|
}
|