ciorent 0.8.0 → 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 CHANGED
@@ -6,153 +6,5 @@ A lightweight, low-overhead concurrency library.
6
6
  - Small bundle size.
7
7
  - Fully type-safe.
8
8
 
9
- ## Examples
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
- ### Signal
115
- A module to interrupt executions of 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('Task 1 waiting: 1s');
125
- await sleep(1000);
126
-
127
- // Interruption point
128
- if (signal.aborted(sig)) return;
129
-
130
- const res = Math.random();
131
- console.log('Task 1 result:', res);
132
-
133
- return res;
134
- };
135
-
136
- {
137
- console.log('------------------------');
138
-
139
- logTime('Task 1 started');
140
- const sig = signal.init();
141
- const promise = f1(sig);
142
-
143
- // Interrupt the signal
144
- signal.abort(sig);
145
-
146
- // Execution will be stopped on the last interruption point
147
- await promise;
148
- }
149
-
150
- {
151
- console.log('------------------------');
152
-
153
- logTime('Task 1 started');
154
-
155
- // Interrupt the function after 500ms
156
- await f1(signal.timeout(500));
157
- }
158
- ```
9
+ ## Docs
10
+ Documentation is available at [re-utils/concurrency](https://re-utils.pages.dev/concurrency/references).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "A lightweight, low-overhead concurrency library",
5
5
  "repository": {
6
6
  "type": "git",
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 t=[,];let n=[t,t,e=>{n[0]=n[0][0]=[,e]},e];return n};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]()};
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]()};