ciorent 0.5.0 → 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 +157 -0
- package/fiber.js +1 -1
- package/index.d.ts +0 -21
- package/index.js +1 -1
- package/package.json +4 -5
- package/queue.d.ts +1 -1
- package/rate-limit.d.ts +1 -1
- package/rate-limit.js +1 -1
- package/semaphore.d.ts +0 -4
- package/semaphore.js +1 -1
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.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=[
|
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
@@ -26,24 +26,3 @@ export declare const timeout: <T>(promise: Promise<T>, ms: number) => Promise<T
|
|
26
26
|
* @param ms - Sleep duration in milliseconds
|
27
27
|
*/
|
28
28
|
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>);
|
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(
|
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,8 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "ciorent",
|
3
|
-
"version": "0.5.
|
3
|
+
"version": "0.5.1",
|
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"
|
@@ -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
|
-
"
|
16
|
+
"./queue": "./queue.js",
|
19
17
|
"./rate-limit": "./rate-limit.js",
|
20
|
-
"./semaphore": "./semaphore.js"
|
18
|
+
"./semaphore": "./semaphore.js",
|
19
|
+
".": "./index.js"
|
21
20
|
}
|
22
21
|
}
|
package/queue.d.ts
CHANGED
package/rate-limit.d.ts
CHANGED
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
|
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
|
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]++};
|