@@ -69,7 +69,7 @@ function sleep(time: number) {
69
69
** This is a placeholder for any asynchronous task.**
70
70
71
71
> ** Note:**
72
- > Avoid using this package on _ "production"_ code.
72
+ > _ Avoid _ using this package on _ "production"_ code.
73
73
> Go [ here] ( #when-to-use ) to understand why.
74
74
75
75
### Mutex
@@ -112,6 +112,8 @@ However `myVar.i` is not protected, then the final value is `1`.
112
112
If a mutex locks the task, then the variable is protected:
113
113
114
114
``` typescript
115
+ import { Mutex } from " @heap-code/concurrency-synchronization" ;
116
+
115
117
const mutex = new Mutex ();
116
118
const myVar = { i: 0 };
117
119
@@ -150,6 +152,8 @@ It is possible to try to lock a mutex in a given time limit.
150
152
The function will then throw an exception if the mutex could not lock in time:
151
153
152
154
``` typescript
155
+ import { ConcurrencyExceedTimeoutException } from " @heap-code/concurrency-synchronization" ;
156
+
153
157
mutex .tryLock (250 ).catch ((error : unknown ) => {
154
158
if (error instanceof ConcurrencyExceedTimeoutException ) {
155
159
console .log (" Could not lock in the given time." );
@@ -165,6 +169,8 @@ A mutex can be interrupted at any time.
165
169
All awaiting _ "threads"_ will then receive an exception:
166
170
167
171
``` typescript
172
+ import { ConcurrencyInterruptedException , Mutex } from " @heap-code/concurrency-synchronization" ;
173
+
168
174
const mutex = new Mutex ();
169
175
const myVar = { i: 0 };
170
176
@@ -195,14 +201,16 @@ bootstrap();
195
201
196
202
### Semaphore
197
203
198
- From [ wikipedia] ( https://en.wikipedia.org/wiki/Semaphore_(programming) ) :
199
-
204
+ > From [ wikipedia] ( https://en.wikipedia.org/wiki/Semaphore_(programming) ) :
205
+ >
200
206
> Semaphores are a type of synchronization primitive.
201
207
202
208
They can be used to protect certain resources (like mutexes),
203
209
but are generally used for synchronization:
204
210
205
211
``` typescript
212
+ import { Semaphore } from " @heap-code/concurrency-synchronization" ;
213
+
206
214
async function bootstrap() {
207
215
const semaphore = new Semaphore (0 );
208
216
@@ -230,6 +238,11 @@ It is possible to try to acquire a semaphore in a given time limit.
230
238
The function will then throw an exception if the semaphore could not acquire in time:
231
239
232
240
``` typescript
241
+ import {
242
+ ConcurrencyExceedTimeoutException ,
243
+ Semaphore
244
+ } from " @heap-code/concurrency-synchronization" ;
245
+
233
246
async function bootstrap() {
234
247
const semaphore = new Semaphore (2 );
235
248
@@ -254,6 +267,8 @@ A semaphore can be interrupted at any time.
254
267
All awaiting _ "threads"_ will then receive an exception:
255
268
256
269
``` typescript
270
+ import { ConcurrencyInterruptedException , Semaphore } from " @heap-code/concurrency-synchronization" ;
271
+
257
272
async function bootstrap() {
258
273
const semaphore = new Semaphore (1 );
259
274
@@ -283,6 +298,8 @@ Very similar to [interrupt](#semaphore-interrupt),
283
298
but it does not throw an exception.
284
299
285
300
``` typescript
301
+ import { Semaphore } from " @heap-code/concurrency-synchronization" ;
302
+
286
303
async function bootstrap() {
287
304
const semaphore = new Semaphore (1 );
288
305
@@ -312,19 +329,21 @@ but it returns values on _acquire_.
312
329
By default, all readings use an array:
313
330
314
331
``` typescript
332
+ import { ProducerConsumer } from " @heap-code/concurrency-synchronization" ;
333
+
315
334
async function bootstrap() {
316
335
const producerConsumer = new ProducerConsumer ([1 ]);
317
336
318
337
const time1 = 100 ;
319
338
const time2 = 150 ;
320
339
321
- sleep (time1 ).then (() => semaphore .write (3 , 4 ));
322
- sleep (time2 ).then (() => semaphore .write (2 ));
340
+ sleep (time1 ).then (() => producerConsumer .write (3 , 4 ));
341
+ sleep (time2 ).then (() => producerConsumer .write (2 ));
323
342
324
343
const maxTime = Math .max (time1 , time2 );
325
344
326
345
const before = performance .now ();
327
- const valuesRead = await semaphore .read (4 ); // waiting until all is read
346
+ const valuesRead = await producerConsumer .read (4 ); // waiting until all is read
328
347
const after = performance .now ();
329
348
330
349
const elapsed = after - before ; // ~150
@@ -340,6 +359,11 @@ It is possible to try to read some values in a given time limit.
340
359
The function will then throw an exception if it could not read in time:
341
360
342
361
``` typescript
362
+ import {
363
+ ConcurrencyExceedTimeoutException ,
364
+ ProducerConsumer
365
+ } from " @heap-code/concurrency-synchronization" ;
366
+
343
367
async function bootstrap() {
344
368
const producerConsumer = new ProducerConsumer ([1 , 2 , 3 ]);
345
369
@@ -364,16 +388,18 @@ The `read` and `tryRead` have their "one"-method
364
388
that do the same thing but return only one value instead of an array:
365
389
366
390
``` typescript
391
+ import { ProducerConsumer } from " @heap-code/concurrency-synchronization" ;
392
+
367
393
async function bootstrap() {
368
394
const producerConsumer = new ProducerConsumer ([1 , 2 ]);
369
395
370
396
// const [value1] = producerConsumer.read(1);
371
397
// can be written:
372
- const value1 = producerConsumer .readOne ();
398
+ const value1 = await producerConsumer .readOne ();
373
399
374
400
// const [value2] = producerConsumer.tryRead(100, 1);
375
401
// can be written:
376
- const value2 = producerConsumer .tryReadOne (100 );
402
+ const value2 = await producerConsumer .tryReadOne (100 );
377
403
378
404
console .log (value1 , value2 ); // 1 2
379
405
}
@@ -386,14 +412,19 @@ A `ProducerConsumer` can be interrupted at any time.
386
412
All awaiting _ "threads"_ will then receive an exception:
387
413
388
414
``` typescript
415
+ import {
416
+ ConcurrencyInterruptedException ,
417
+ ProducerConsumer
418
+ } from " @heap-code/concurrency-synchronization" ;
419
+
389
420
async function bootstrap() {
390
421
const producerConsumer = new ProducerConsumer ([1 ]);
391
422
392
423
void sleep (100 ).then (() => producerConsumer .interrupt ({ code: 502 }, [1 , 2 , 3 ]));
393
424
394
425
const succeed = await Promise .all ([
395
426
producerConsumer .read (3 ),
396
- producerConsumer .readOne (2 ),
427
+ producerConsumer .readOne (),
397
428
producerConsumer .tryRead (200 , 3 ),
398
429
producerConsumer .tryReadOne (200 )
399
430
]).catch ((error : unknown ) => {
@@ -421,6 +452,8 @@ if it comes from _regular_ observable or [subjects](https://rxjs.dev/guide/subje
421
452
So this difference can be omitted with the following:
422
453
423
454
``` typescript
455
+ import { ProducerConsumer } from " @heap-code/concurrency-synchronization" ;
456
+
424
457
describe (" My test" , () => {
425
458
it (" should work" , async () => {
426
459
const producerConsumer = new ProducerConsumer ();
0 commit comments