ciorent 0.7.1 → 0.8.1
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 +2 -154
- package/package.json +1 -1
- package/semaphore.d.ts +6 -0
- package/semaphore.js +1 -1
- package/signal.d.ts +12 -18
- package/signal.js +1 -1
package/README.md
CHANGED
@@ -6,157 +6,5 @@ A lightweight, low-overhead concurrency library.
|
|
6
6
|
- Small bundle size.
|
7
7
|
- Fully type-safe.
|
8
8
|
|
9
|
-
##
|
10
|
-
|
11
|
-
### Pausing
|
12
|
-
Continue the execution on next tick, allowing other asynchronous tasks to run.
|
13
|
-
```ts
|
14
|
-
import { nextTick } from 'ciorent';
|
15
|
-
|
16
|
-
const logTime = (label: string) =>
|
17
|
-
console.log(`${label}: ${Math.floor(performance.now())}ms`);
|
18
|
-
|
19
|
-
// Expensive sync task
|
20
|
-
const task1 = async () => {
|
21
|
-
let x = 0;
|
22
|
-
|
23
|
-
// Simulate heavy operation
|
24
|
-
for (let i = 0, l = (Math.random() + 15) * 1e6; i < l; i++) {
|
25
|
-
// Yield control back occasionally to the runtime, allowing
|
26
|
-
// it to schedule other tasks
|
27
|
-
if (i % 1e5 === 0) await nextTick;
|
28
|
-
|
29
|
-
x += Math.random() * 32 + i * Math.round(Math.random() * 16);
|
30
|
-
}
|
31
|
-
|
32
|
-
console.log('Task 1 result:', x);
|
33
|
-
};
|
34
|
-
|
35
|
-
// Short async task
|
36
|
-
const task2 = async (id: number) => {
|
37
|
-
logTime('Task 2.' + id + ' start fetching');
|
38
|
-
await fetch('http://localhost:3000').catch(() => {});
|
39
|
-
logTime('Task 2.' + id + ' done fetching');
|
40
|
-
};
|
41
|
-
|
42
|
-
task1();
|
43
|
-
|
44
|
-
// Task 2 will not get blocked by task 1
|
45
|
-
for (let i = 1; i <= 5; i++) task2(i);
|
46
|
-
```
|
47
|
-
|
48
|
-
### Sleep
|
49
|
-
Runtime-agnostic synchronous and asynchronous sleep functions.
|
50
|
-
```ts
|
51
|
-
import { sleep, sleepSync } from 'ciorent';
|
52
|
-
|
53
|
-
const logTime = (label: string) =>
|
54
|
-
console.log(`${label}: ${Math.floor(performance.now())}ms`);
|
55
|
-
|
56
|
-
logTime('Start');
|
57
|
-
|
58
|
-
// Non-blocking
|
59
|
-
await sleep(500);
|
60
|
-
logTime('After about 0.5s');
|
61
|
-
|
62
|
-
// This blocks the event loop
|
63
|
-
// On the browser this only works in workers and blocks the worker thread
|
64
|
-
sleepSync(500);
|
65
|
-
logTime('After another 0.5s');
|
66
|
-
```
|
67
|
-
|
68
|
-
### Latches
|
69
|
-
Latches are a type of synchronization primitive that allows one thread to wait until another thread completes an operation before continuing execution.
|
70
|
-
```ts
|
71
|
-
import { sleep, latch } from 'ciorent';
|
72
|
-
|
73
|
-
const startFetch = latch.init();
|
74
|
-
|
75
|
-
(async () => {
|
76
|
-
// Wait until the latch is opened
|
77
|
-
await latch.wait(startFetch);
|
78
|
-
|
79
|
-
const res = await fetch('http://example.com');
|
80
|
-
return res.text();
|
81
|
-
})();
|
82
|
-
|
83
|
-
// Fetch starts after 500ms
|
84
|
-
await sleep(500);
|
85
|
-
latch.open(startFetch);
|
86
|
-
```
|
87
|
-
|
88
|
-
### Semaphores
|
89
|
-
Semaphore is a concurrency primitive used to control access to a common resource by multiple processes.
|
90
|
-
```ts
|
91
|
-
import { semaphore, nextTick } from 'ciorent';
|
92
|
-
|
93
|
-
// Only allow 2 task to run concurrently
|
94
|
-
const sem = semaphore.init(2);
|
95
|
-
|
96
|
-
const task = async (id: number) => {
|
97
|
-
// Acquire the semaphore or wait for the semaphore to be available
|
98
|
-
await semaphore.acquire(sem);
|
99
|
-
|
100
|
-
console.log('Task', id, 'started');
|
101
|
-
|
102
|
-
// Let the main thread schedules other tasks
|
103
|
-
for (let i = 1; i <= 5; i++) await nextTick;
|
104
|
-
|
105
|
-
console.log('Task', id, 'end');
|
106
|
-
|
107
|
-
// Release the semaphore
|
108
|
-
semaphore.release(sem);
|
109
|
-
};
|
110
|
-
|
111
|
-
for (let i = 1; i <= 5; i++) task(i);
|
112
|
-
```
|
113
|
-
|
114
|
-
### Fibers
|
115
|
-
A module to interrupt executions of async functions.
|
116
|
-
```ts
|
117
|
-
import { signal, sleep } from 'ciorent';
|
118
|
-
|
119
|
-
const logTime = (label: string) =>
|
120
|
-
console.log(`${label}: ${Math.floor(performance.now())}ms`);
|
121
|
-
|
122
|
-
const f1 = async (sig: signal.Signal) => {
|
123
|
-
// Wait for a promise
|
124
|
-
console.log('Fiber 1 waiting: 1s');
|
125
|
-
await sleep(1000);
|
126
|
-
|
127
|
-
// Interruption point
|
128
|
-
if (signal.interrupted(sig)) return;
|
129
|
-
|
130
|
-
const res = Math.random();
|
131
|
-
console.log('Fiber 1 result:', res);
|
132
|
-
|
133
|
-
return res;
|
134
|
-
};
|
135
|
-
|
136
|
-
{
|
137
|
-
console.log('------------------------');
|
138
|
-
|
139
|
-
console.log('Fiber 1 started');
|
140
|
-
const sig = signal.init();
|
141
|
-
const promise = f1(sig);
|
142
|
-
|
143
|
-
// Interrupt the signal
|
144
|
-
signal.interrupt(sig);
|
145
|
-
|
146
|
-
// Execution will be stopped on the last interruption point
|
147
|
-
await promise;
|
148
|
-
|
149
|
-
console.log('Fiber 1 interrupted');
|
150
|
-
}
|
151
|
-
|
152
|
-
{
|
153
|
-
console.log('------------------------');
|
154
|
-
|
155
|
-
logTime('Fiber 1 started');
|
156
|
-
|
157
|
-
// Interrupt the function after 500ms
|
158
|
-
await f1(signal.duration(500));
|
159
|
-
|
160
|
-
logTime('Fiber 1 interrupted');
|
161
|
-
}
|
162
|
-
```
|
9
|
+
## Docs
|
10
|
+
Documentation is available at [re-utils/concurrency](https://re-utils.pages.dev/concurrency/references).
|
package/package.json
CHANGED
package/semaphore.d.ts
CHANGED
@@ -11,6 +11,12 @@ export type Semaphore = [...UnboundedQueue<() => void>, callback: PromiseFn<void
|
|
11
11
|
*/
|
12
12
|
export declare const init: (n: number) => Semaphore;
|
13
13
|
/**
|
14
|
+
* Queue a task
|
15
|
+
* @param s
|
16
|
+
* @param cb
|
17
|
+
*/
|
18
|
+
export declare const queue: (s: Semaphore, cb: () => Promise<any>) => Promise<void>;
|
19
|
+
/**
|
14
20
|
* Wait until the semaphore allows access
|
15
21
|
*/
|
16
22
|
export declare const acquire: (s: Semaphore) => Promise<void>;
|
package/semaphore.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export let init=e=>{let
|
1
|
+
export let init=e=>{let n=[,];let r=[n,n,e=>{r[0]=r[0][0]=[,e]},e];return r};export let queue=async(e,n)=>{if(--e[3]<0){e[2](n)}else{await n();release(e)}};export let acquire=async e=>{if(--e[3]<0)return new Promise(e[2])};export let release=e=>{if(e[3]++<0)(e[1]=e[1][0])[1]()};
|
package/signal.d.ts
CHANGED
@@ -1,39 +1,33 @@
|
|
1
1
|
/**
|
2
2
|
* Describe a signal
|
3
3
|
*/
|
4
|
-
export type Signal = [interrupted: boolean];
|
4
|
+
export type Signal = [interrupted: boolean, ...parents: Signal[]];
|
5
5
|
/**
|
6
6
|
* Create a signal
|
7
7
|
*/
|
8
8
|
export declare const init: () => Signal;
|
9
9
|
/**
|
10
|
-
*
|
11
|
-
* @param
|
10
|
+
* Create a signal that aborts when any of the input signals abort
|
11
|
+
* @param sigs
|
12
12
|
*/
|
13
|
-
export declare const
|
13
|
+
export declare const any: (...sigs: Signal[]) => Signal;
|
14
14
|
/**
|
15
|
-
*
|
15
|
+
* Check whether the signal has been aborted
|
16
16
|
* @param t
|
17
17
|
*/
|
18
|
-
export declare const
|
18
|
+
export declare const aborted: (t: Signal) => boolean;
|
19
19
|
/**
|
20
|
-
*
|
20
|
+
* Abort a signal
|
21
21
|
* @param t
|
22
22
|
*/
|
23
|
-
export declare const
|
23
|
+
export declare const abort: (t: Signal) => void;
|
24
24
|
/**
|
25
|
-
*
|
25
|
+
* Abort a signal after a duration
|
26
26
|
* @param t
|
27
|
-
* @param signal
|
28
27
|
*/
|
29
|
-
export declare const
|
28
|
+
export declare const abortAfter: (ms: number, t: Signal) => Promise<void>;
|
30
29
|
/**
|
31
|
-
* Create a signal that
|
30
|
+
* Create a signal that aborts after ms
|
32
31
|
* @param ms
|
33
32
|
*/
|
34
|
-
export declare const
|
35
|
-
/**
|
36
|
-
* Create a signal that aborts when the abort signal aborts
|
37
|
-
* @param signal
|
38
|
-
*/
|
39
|
-
export declare const bind: (signal: AbortSignal) => Signal;
|
33
|
+
export declare const timeout: (ms: number) => Signal;
|
package/signal.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
import{sleep}from".";export let init=()=>[false];export let
|
1
|
+
import{sleep}from".";export let init=()=>[false];export let any=(...e)=>{let a=[false];for(let o=0;o<e.length;o++)e[o].push(a);return a};export let aborted=e=>e[0];export let abort=e=>{if(!e[0]){e[0]=true;if(e.length>1)for(let a=1;a<e.length;a++)abort(e[a])}};export let abortAfter=async(a,o)=>{await sleep(a);abort(o)};export let timeout=e=>{let a=[false];abortAfter(e,a);return a};
|