ciorent 0.5.0 → 0.6.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 CHANGED
@@ -7,3 +7,178 @@ A lightweight, low-overhead concurrency library.
7
7
  - Fully type-safe.
8
8
 
9
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
+ ### Fibers
115
+ Virtual threads with more controlled execution.
116
+ ```ts
117
+ import { fiber, sleep } from 'ciorent';
118
+
119
+ const logTime = (label: string) =>
120
+ console.log(`${label}: ${Math.floor(performance.now())}ms`);
121
+
122
+ const f1 = fiber.fn(function* () {
123
+ // Wait for a promise
124
+ console.log('Fiber 1 waiting: 1s');
125
+ yield sleep(1000);
126
+
127
+ // Wait for a promise and return its result
128
+ const res = yield* fiber.unwrap(Promise.resolve(1));
129
+ console.log('Fiber 1 recieved:', res);
130
+
131
+ return Math.random();
132
+ });
133
+
134
+ {
135
+ // Start the fiber process on next event loop cycle
136
+ const main = fiber.spawn(function* (proc) {
137
+ // Start f1, wait for the process to complete and get the result
138
+ console.log('Fiber 2: joins fiber 1');
139
+ const res = yield* fiber.join(fiber.spawn(f1));
140
+ console.log('Fiber 2 recieved:', res);
141
+
142
+ // Start f1 and make its lifetime depends on current fiber
143
+ console.log('Fiber 2: spawns fiber 1');
144
+ const childProc = fiber.spawn(f1);
145
+ fiber.mount(childProc, proc);
146
+ });
147
+
148
+ console.log('Fiber 2 started:', fiber.running(main));
149
+
150
+ // Wait for the fiber process to finish
151
+ await fiber.complete(main);
152
+
153
+ // Check finish status
154
+ console.log('Fiber 2 completed:', fiber.completed(main));
155
+ }
156
+
157
+ {
158
+ console.log('------------------------');
159
+
160
+ const main = fiber.spawn(f1);
161
+ console.log('Fiber 1 started:', fiber.running(main));
162
+
163
+ // Interrupt a fiber
164
+ fiber.interrupt(main);
165
+
166
+ // Execution will be stopped on the last yield
167
+ await fiber.complete(main);
168
+
169
+ console.log('Fiber 1 interrupted:', fiber.interrupted(main));
170
+ }
171
+
172
+ {
173
+ console.log('------------------------');
174
+
175
+ const main = fiber.spawn(f1);
176
+ logTime('Fiber 1 started');
177
+
178
+ // Wait for a time period then interrupt the fiber
179
+ fiber.timeout(main, 500);
180
+ await fiber.complete(main);
181
+
182
+ logTime('Fiber 1 interrupted');
183
+ }
184
+ ```
package/fiber.js CHANGED
@@ -1 +1 @@
1
- import{nextTick,sleep}from"./index.js";export let running=e=>e[1]===1;export let completed=e=>e[1]===2;export let interrupted=e=>e[1]===3;let invoke=async(f,p)=>{await nextTick;try{let e=f.next();while(!e.done){let m=await e.value;if(p[1]===3)return;e=f.next(m)}p[1]=2;return e.value}finally{if(p[1]!==2)p[1]=3;p[2].forEach(interrupt)}};export let fn=e=>e;export let spawn=(e,...f)=>{let p=[null,1,[]];p[0]=invoke(e(p,...f),p);return p};export let interrupt=e=>{if(e[1]!==2)e[1]=3};export let timeout=async(e,p)=>{await sleep(p);interrupt(e)};export function*join(e){return yield e[0]}export let complete=e=>e[0];export let mount=(e,f)=>{f[2].push(e)};export let control=(e,f)=>{f.addEventListener(`abort`,()=>{interrupt(e)})};export function*unwrap(e){return yield e}
1
+ import{nextTick,sleep}from"./index.js";export let running=e=>e[1]===1;export let completed=e=>e[1]===2;export let interrupted=e=>e[1]===3;let invoke=async(f,p)=>{await nextTick;try{let e=f.next();while(!e.done){let m=await e.value;if(p[1]===3)return;e=f.next(m)}p[1]=2;return e.value}finally{if(p[1]!==2)p[1]=3;p[2].forEach(interrupt)}};export let fn=e=>e;export let spawn=(e,...f)=>{let p=[,1,[]];p[0]=invoke(e(p,...f),p);return p};export let interrupt=e=>{if(e[1]!==2)e[1]=3};export let timeout=async(e,p)=>{await sleep(p);interrupt(e)};export function*join(e){return yield e[0]}export let complete=e=>e[0];export let mount=(e,f)=>{f[2].push(e)};export let control=(e,f)=>{f.addEventListener(`abort`,()=>{interrupt(e)})};export function*unwrap(e){return yield e}
package/index.d.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  /**
2
+ * @module Other utilities
3
+ */
4
+ /**
2
5
  * Continue the execution on next event loop cycle.
3
6
  *
4
7
  * You can `await` this **occasionally** in an expensive synchronous operation to avoid
@@ -26,24 +29,7 @@ export declare const timeout: <T>(promise: Promise<T>, ms: number) => Promise<T
26
29
  * @param ms - Sleep duration in milliseconds
27
30
  */
28
31
  export declare const sleepSync: (ms: number) => void;
29
- /**
30
- * Spawn n sequential task
31
- * @param n
32
- * @param task - The function to run
33
- */
34
- export declare const sequential: <const T extends any[]>(n: number, task: (...args: [...T, id: number]) => Promise<any>, ...args: T) => Promise<void>;
35
- /**
36
- * Spawn n concurrent tasks
37
- * @param n
38
- * @param task - The function to run
39
- */
40
- export declare const spawn: <
41
- const T extends any[],
42
- const R
43
- >(n: number, task: (...args: [...T, id: number]) => Promise<R>, ...args: T) => Promise<R>[];
44
- /**
45
- * Throttle function execution for a time period
46
- * @param ms - The time in milliseconds
47
- * @param limit - The call limit in the time period
48
- */
49
- export declare const throttle: (ms: number, limit: number) => (() => Promise<void>);
32
+ export * as fiber from "./fiber.js";
33
+ export * as latch from "./latch.js";
34
+ export * as rateLimit from "./rate-limit.js";
35
+ export * as semaphore from "./semaphore.js";
package/index.js CHANGED
@@ -1 +1 @@
1
- export let nextTick=Promise.resolve();export let sleep=globalThis.Bun?.sleep??globalThis.process?.getBuiltinModule?.(`timers/promises`).setTimeout??(e=>new Promise(t=>{setTimeout(t,e)}));export let timeout=(e,n)=>Promise.race([e,sleep(n)]);let sharedBuf=new Int32Array(new SharedArrayBuffer(4));export let sleepSync=globalThis.Bun?.sleepSync??(e=>{Atomics.wait(sharedBuf,0,0,e)});export let sequential=async(e,t,...n)=>{for(let r=0;r<e;r++)await t(...n,r)};export let spawn=(e,t,...n)=>{let r=new Array(e);for(let i=0;i<e;i++)r[i]=t(...n,i);return r};export let throttle=(e,t)=>{let n=[null];let r=n;let i=e=>{n=n[0]=[null,e]};let a=t;let o=false;let s=()=>{a=t;if(r===n){o=false;return}do{a--;(r=r[0])[1]()}while(a>0&&r!==n);setTimeout(s,e)};return async()=>{if(a===0)return new Promise(i);if(!o){o=true;setTimeout(s,e)}a--}};
1
+ export let nextTick=Promise.resolve();export let sleep=globalThis.Bun?.sleep??globalThis.process?.getBuiltinModule?.(`timers/promises`).setTimeout??(e=>new Promise(r=>{setTimeout(r,e)}));export let timeout=(e,i)=>Promise.race([e,sleep(i)]);let sharedBuf=new Int32Array(new SharedArrayBuffer(4));export let sleepSync=globalThis.Bun?.sleepSync??(e=>{Atomics.wait(sharedBuf,0,0,e)});export*as fiber from"./fiber.js";export*as latch from"./latch.js";export*as rateLimit from"./rate-limit.js";export*as semaphore from"./semaphore.js";
package/latch.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Describe a latch
3
+ */
4
+ export type Latch = [p: Promise<void>, res: () => void, cb: (res: () => void) => void];
5
+ export declare const init: <T>() => Latch;
6
+ /**
7
+ * Reclose the latch
8
+ * @param c
9
+ */
10
+ export declare const close: (c: Latch) => void;
11
+ /**
12
+ * Open the latch
13
+ * @param c
14
+ */
15
+ export declare const open: (c: Latch) => void;
16
+ /**
17
+ * Wait for the latch to open
18
+ * @param c
19
+ */
20
+ export declare const wait: (c: Latch) => Promise<void>;
package/latch.js ADDED
@@ -0,0 +1 @@
1
+ export let init=()=>{let e=[,,r=>{e[1]=r}];close(e);return e};export let close=e=>{e[0]=new Promise(e[2])};export let open=e=>{e[1]()};export let wait=e=>e[0];
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "A lightweight, low-overhead concurrency library",
5
- "homepage": "https://ciorent.netlify.app",
6
5
  "repository": {
7
6
  "type": "git",
8
7
  "url": "git+https://github.com/re-utils/ciorent.git"
@@ -12,11 +11,5 @@
12
11
  "type": "module",
13
12
  "main": "./index.js",
14
13
  "types": "./index.d.ts",
15
- "exports": {
16
- "./queue": "./queue.js",
17
- "./fiber": "./fiber.js",
18
- ".": "./index.js",
19
- "./rate-limit": "./rate-limit.js",
20
- "./semaphore": "./semaphore.js"
21
- }
14
+ "exports": {}
22
15
  }
package/queue.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Describe a queue node (singly linked list node)
7
7
  */
8
- export type QueueNode<T> = [next: QueueNode<T> | null, value: T];
8
+ export type QueueNode<T> = [next: QueueNode<T> | undefined, value: T];
9
9
  /**
10
10
  * Describe an unbounded queue
11
11
  */
package/rate-limit.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  /**
5
5
  * Describe a rate limiter
6
6
  */
7
- export type Limiter = (calls: number, ms: number) => () => boolean;
7
+ export type Limiter = (limit: number, ms: number) => () => boolean;
8
8
  /**
9
9
  * Fixed window strategy
10
10
  * @param limit
package/rate-limit.js CHANGED
@@ -1 +1 @@
1
- export let fixed=(e,t)=>{let n=e;let r=()=>{n=e};return()=>{if(n===0)return false;if(n===e)setTimeout(r,t);n--;return true}};export let sliding=(e,t)=>{let n=e;let r=()=>{n++};return()=>{if(n===0)return false;n--;setTimeout(r,t);return true}};export let bucket=(e,t)=>{let n=e;t/=e;let r=()=>{n++;if(n<e)setTimeout(r,t)};return()=>{if(n===0)return false;if(n===e)setTimeout(r,t);n--;return true}};
1
+ export let fixed=(e,t)=>{let n=e;let r=()=>{n=e};return()=>{if(n===0)return false;if(n--===e)setTimeout(r,t);return true}};export let sliding=(e,t)=>{let n=e;let r=()=>{n++};return()=>{if(n===0)return false;n--;setTimeout(r,t);return true}};export let bucket=(e,t)=>{let n=e;t/=e;let r=()=>{if(n++<e)setTimeout(r,t)};return()=>{if(n===0)return false;if(n--===e)setTimeout(r,t);return true}};
package/semaphore.d.ts CHANGED
@@ -18,7 +18,3 @@ export declare const acquire: (s: Semaphore) => Promise<void>;
18
18
  * Signal to the semaphore to release access
19
19
  */
20
20
  export declare const release: (s: Semaphore) => void;
21
- /**
22
- * Bind a task to a semaphore
23
- */
24
- export declare const bind: <T extends (...args: any[]) => Promise<any>>(f: T, s: Semaphore) => T;
package/semaphore.js CHANGED
@@ -1 +1 @@
1
- export let init=e=>{let n=[null];let r=[n,n,e=>{r[0]=r[0][0]=[null,e]},e];return r};export let acquire=async e=>{e[3]--;if(e[3]<0)return new Promise(e[2])};export let release=e=>{if(e[3]<0)(e[1]=e[1][0])[1]();e[3]++};export let bind=(e,n)=>async(...r)=>{n[3]--;if(n[3]<0)await new Promise(n[2]);try{return await e(...r)}finally{if(n[3]<0)(n[1]=n[1][0])[1]();n[3]++}};
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=>{e[3]--;if(e[3]<0)return new Promise(e[2])};export let release=e=>{if(e[3]<0)(e[1]=e[1][0])[1]();e[3]++};