ciorent 0.4.4 → 0.5.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
@@ -7,3 +7,160 @@ 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 * as co 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 co.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
+ Cross-runtime synchronous and asynchronous sleep functions.
50
+ ```ts
51
+ import * as co 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 co.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
+ co.sleepSync(500);
65
+ logTime('After another 0.5s');
66
+ ````
67
+
68
+ ### Semaphores
69
+ Semaphore is a concurrency primitive used to control access to a common resource by multiple processes.
70
+ ```ts
71
+ import * as semaphore from 'ciorent/semaphore';
72
+ import * as co from 'ciorent';
73
+
74
+ // Only allow 2 task to run concurrently
75
+ const sem = semaphore.init(2);
76
+
77
+ const task = async (id: number) => {
78
+ // Acquire the semaphore or wait for the semaphore to be available
79
+ await semaphore.acquire(sem);
80
+
81
+ console.log('Task', id, 'started');
82
+
83
+ // Let the main thread schedules other tasks
84
+ for (let i = 1; i <= 5; i++) await co.nextTick;
85
+
86
+ console.log('Task', id, 'end');
87
+
88
+ // Release the semaphore
89
+ semaphore.release(sem);
90
+ };
91
+
92
+ for (let i = 1; i <= 5; i++) task(i);
93
+ ```
94
+
95
+ ### Fibers
96
+ Virtual threads with more controlled execution.
97
+ ```ts
98
+ import * as co from 'ciorent';
99
+ import * as fiber from 'ciorent/fiber';
100
+
101
+ const logTime = (label: string) =>
102
+ console.log(`${label}: ${Math.floor(performance.now())}ms`);
103
+
104
+ const f1 = fiber.fn(function* () {
105
+ // Wait for a promise
106
+ console.log('Fiber 1 waiting: 1s');
107
+ yield co.sleep(1000);
108
+
109
+ // Wait for a promise and return its result
110
+ const res = yield* fiber.unwrap(Promise.resolve(1));
111
+ console.log('Fiber 1 recieved:', res);
112
+
113
+ return Math.random();
114
+ });
115
+
116
+ {
117
+ // Start the fiber process on next event loop cycle
118
+ const main = fiber.spawn(function* (proc) {
119
+ // Start f1, wait for the process to complete and get the result
120
+ console.log('Fiber 2: joins fiber 1');
121
+ const res = yield* fiber.join(fiber.spawn(f1));
122
+ console.log('Fiber 2 recieved:', res);
123
+
124
+ // Start f1 and make its lifetime depends on current fiber
125
+ console.log('Fiber 2: spawns fiber 1');
126
+ const childProc = fiber.spawn(f1);
127
+ fiber.mount(childProc, proc);
128
+ });
129
+
130
+ console.log('Fiber 2 started:', fiber.running(main));
131
+
132
+ // Wait for the fiber process to finish
133
+ await fiber.complete(main);
134
+
135
+ // Check finish status
136
+ console.log('Fiber 2 completed:', fiber.completed(main));
137
+ }
138
+
139
+ {
140
+ console.log('------------------------');
141
+
142
+ const main = fiber.spawn(f1);
143
+ console.log('Fiber 1 started:', fiber.running(main));
144
+
145
+ // Interrupt a fiber
146
+ fiber.interrupt(main);
147
+
148
+ // Execution will be stopped on the last yield
149
+ await fiber.complete(main);
150
+
151
+ console.log('Fiber 1 interrupted:', fiber.interrupted(main));
152
+ }
153
+
154
+ {
155
+ console.log('------------------------');
156
+
157
+ const main = fiber.spawn(f1);
158
+ logTime('Fiber 1 started');
159
+
160
+ // Wait for a time period then interrupt the fiber
161
+ fiber.timeout(main, 500);
162
+ await fiber.complete(main);
163
+
164
+ logTime('Fiber 1 interrupted');
165
+ }
166
+ ```
package/fiber.d.ts CHANGED
@@ -51,7 +51,7 @@ export declare function join<T extends Process>(t: T): Generator<Awaited<T[0]>,
51
51
  * Wait for a fiber to finish and retrieve its result
52
52
  * @param t
53
53
  */
54
- export declare const done: <T extends Process>(t: T) => T[0];
54
+ export declare const complete: <T extends Process>(t: T) => T[0];
55
55
  /**
56
56
  * Mount child fiber lifetime to parent lifetime
57
57
  * @param child
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 done=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
@@ -12,6 +12,12 @@ export declare const nextTick: Promise<void>;
12
12
  */
13
13
  export declare const sleep: (ms: number) => Promise<void>;
14
14
  /**
15
+ * Timeout a promise after ms milliseconds
16
+ * @param promise - Target promise to timeout
17
+ * @param ms - Timeout duration
18
+ */
19
+ export declare const timeout: <T>(promise: Promise<T>, ms: number) => Promise<T | void>;
20
+ /**
15
21
  * Sleep for a duration synchronously.
16
22
  *
17
23
  * This method blocks the current thread.
@@ -20,24 +26,3 @@ export declare const sleep: (ms: number) => Promise<void>;
20
26
  * @param ms - Sleep duration in milliseconds
21
27
  */
22
28
  export declare const sleepSync: (ms: number) => void;
23
- /**
24
- * Spawn n sequential task
25
- * @param n
26
- * @param task - The function to run
27
- */
28
- export declare const sequential: <const T extends any[]>(n: number, task: (...args: [...T, id: number]) => Promise<any>, ...args: T) => Promise<void>;
29
- /**
30
- * Spawn n concurrent tasks
31
- * @param n
32
- * @param task - The function to run
33
- */
34
- export declare const spawn: <
35
- const T extends any[],
36
- const R
37
- >(n: number, task: (...args: [...T, id: number]) => Promise<R>, ...args: T) => Promise<R>[];
38
- /**
39
- * Throttle function execution for a time period
40
- * @param ms - The time in milliseconds
41
- * @param limit - The call limit in the time period
42
- */
43
- export declare const throttle: (ms: number, limit: number) => (() => Promise<void>);
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)}));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)});
package/package.json CHANGED
@@ -1,11 +1,10 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.4.4",
3
+ "version": "0.5.1",
4
4
  "description": "A lightweight, low-overhead concurrency library",
5
- "homepage": "https://ciorent.netlify.app",
6
5
  "repository": {
7
- "type": "github",
8
- "url": "https://github.com/re-utils/ciorent"
6
+ "type": "git",
7
+ "url": "git+https://github.com/re-utils/ciorent.git"
9
8
  },
10
9
  "keywords": ["low-overhead", "lightweight", "concurrency", "cross-runtime"],
11
10
  "license": "MIT",
@@ -13,10 +12,10 @@
13
12
  "main": "./index.js",
14
13
  "types": "./index.d.ts",
15
14
  "exports": {
16
- "./queue": "./queue.js",
17
15
  "./fiber": "./fiber.js",
18
- "./stream": "./stream.js",
19
- ".": "./index.js",
20
- "./semaphore": "./semaphore.js"
16
+ "./queue": "./queue.js",
17
+ "./rate-limit": "./rate-limit.js",
18
+ "./semaphore": "./semaphore.js",
19
+ ".": "./index.js"
21
20
  }
22
21
  }
package/queue.d.ts CHANGED
@@ -1,14 +1,11 @@
1
1
  /**
2
- * @module Queue utilities
2
+ * @module Queue types
3
+ * @private
3
4
  */
4
5
  /**
5
- * Describe a fixed-sized queue
6
- */
7
- export type FixedQueue<T extends {} = {}> = [buffer: (T | undefined | null)[], capacity: number, head: number, tail: number];
8
- /**
9
6
  * Describe a queue node (singly linked list node)
10
7
  */
11
- export type QueueNode<T> = [next: QueueNode<T> | null, value: T];
8
+ export type QueueNode<T> = [next: QueueNode<T> | undefined, value: T];
12
9
  /**
13
10
  * Describe an unbounded queue
14
11
  */
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @module Rate limit
3
+ */
4
+ /**
5
+ * Describe a rate limiter
6
+ */
7
+ export type Limiter = (limit: number, ms: number) => () => boolean;
8
+ /**
9
+ * Fixed window strategy
10
+ * @param limit
11
+ * @param ms
12
+ */
13
+ export declare const fixed: Limiter;
14
+ /**
15
+ * Sliding window strategy
16
+ * @param limit
17
+ * @param ms
18
+ */
19
+ export declare const sliding: Limiter;
20
+ /**
21
+ * Token bucket strategy
22
+ * @param limit
23
+ * @param ms
24
+ */
25
+ export declare const bucket: Limiter;
package/rate-limit.js ADDED
@@ -0,0 +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);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]++};
package/stream.d.ts DELETED
@@ -1,27 +0,0 @@
1
- /**
2
- * @module Streams
3
- */
4
- import type { PromiseFn, UnboundedQueue } from "./queue.js";
5
- /**
6
- * Describe a stream
7
- */
8
- export type Stream<T extends {} = {}> = [...UnboundedQueue<T | ((val?: T) => void)>, callback: PromiseFn<T>, queueing: boolean];
9
- /**
10
- * Create a stream
11
- */
12
- export declare const init: <T extends {} = {}>() => Stream<T>;
13
- /**
14
- * Write a value to the stream
15
- * @param s
16
- * @param v
17
- */
18
- export declare const write: <T extends {} = {}>(s: Stream<T>, v: T) => void;
19
- /**
20
- * Read a value from the stream
21
- * @param s
22
- */
23
- export declare const read: <T extends {} = {}>(s: Stream<T>) => Promise<T | undefined>;
24
- /**
25
- * Release all pending read with undefined
26
- */
27
- export declare const flush: (s: Stream) => void;
package/stream.js DELETED
@@ -1 +0,0 @@
1
- export let init=()=>{let e=[null];let r=[e,e,e=>{r[0]=r[0][0]=[null,e]},false];return r};export let write=(e,r)=>{if(!e[3]){if(e[1][0]!==null){(e[1]=e[1][0])[1](r);return}e[3]=true}e[0]=e[0][0]=[null,r]};export let read=async e=>{if(e[3]){e[1]=e[1][0];if(e[1][0]===null)e[3]=false;return e[1][1]}return new Promise(e[2])};export let flush=e=>{if(!e[3])while(e[1][0]!==null)(e[1]=e[1][0])[1]()};