ciorent 0.0.24 → 0.0.25
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 +36 -36
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
@@ -24,40 +24,6 @@ const sem = semaphore.init(2);
|
|
24
24
|
cio.concurrent(6, (sem, id) => task(sem, id), sem);
|
25
25
|
```
|
26
26
|
|
27
|
-
## Fibers
|
28
|
-
Virtual threads with more controlled execution.
|
29
|
-
|
30
|
-
```ts
|
31
|
-
import * as cio from 'ciorent';
|
32
|
-
import * as fiber from 'ciorent/fiber';
|
33
|
-
|
34
|
-
const thread1 = fiber.fn(function* () {
|
35
|
-
console.log('Fiber 1 started');
|
36
|
-
|
37
|
-
// Thread1 will be interrupted by thread2
|
38
|
-
// As thread2 will end first
|
39
|
-
yield cio.sleep(1000);
|
40
|
-
|
41
|
-
console.log('Fiber 1 done');
|
42
|
-
});
|
43
|
-
|
44
|
-
const thread2 = fiber.fn(function* (thread) {
|
45
|
-
console.log('Fiber 2 started');
|
46
|
-
|
47
|
-
yield;
|
48
|
-
console.log('Fiber 2 resumed');
|
49
|
-
|
50
|
-
// Start thread 1 and make thread1
|
51
|
-
// lifetime depends on thread2
|
52
|
-
fiber.mount(fiber.spawn(thread1), thread);
|
53
|
-
|
54
|
-
console.log('Fiber 2 done');
|
55
|
-
});
|
56
|
-
|
57
|
-
// Start running the thread
|
58
|
-
fiber.spawn(thread2);
|
59
|
-
```
|
60
|
-
|
61
27
|
## Latch
|
62
28
|
Latch is a synchronization primitive that allows one process to wait until another completes an operation before continuing execution.
|
63
29
|
|
@@ -101,6 +67,40 @@ await main();
|
|
101
67
|
await main();
|
102
68
|
```
|
103
69
|
|
70
|
+
## Fibers
|
71
|
+
Virtual threads with more controlled execution.
|
72
|
+
|
73
|
+
```ts
|
74
|
+
import * as cio from 'ciorent';
|
75
|
+
import * as fiber from 'ciorent/fiber';
|
76
|
+
|
77
|
+
const thread1 = fiber.fn(function* () {
|
78
|
+
console.log('Fiber 1 started');
|
79
|
+
|
80
|
+
// Thread1 will be interrupted by thread2
|
81
|
+
// As thread2 will end first
|
82
|
+
yield cio.sleep(1000);
|
83
|
+
|
84
|
+
console.log('Fiber 1 done');
|
85
|
+
});
|
86
|
+
|
87
|
+
const thread2 = fiber.fn(function* (thread) {
|
88
|
+
console.log('Fiber 2 started');
|
89
|
+
|
90
|
+
yield;
|
91
|
+
console.log('Fiber 2 resumed');
|
92
|
+
|
93
|
+
// Start thread 1 and make thread1
|
94
|
+
// lifetime depends on thread2
|
95
|
+
fiber.mount(fiber.spawn(thread1), thread);
|
96
|
+
|
97
|
+
console.log('Fiber 2 done');
|
98
|
+
});
|
99
|
+
|
100
|
+
// Start running the thread
|
101
|
+
fiber.spawn(thread2);
|
102
|
+
```
|
103
|
+
|
104
104
|
## Pubsub
|
105
105
|
A fast, simple publish-subscribe API.
|
106
106
|
|
@@ -256,12 +256,12 @@ await cio.sleep(100);
|
|
256
256
|
fn(2); // fn(2) gets executed
|
257
257
|
```
|
258
258
|
|
259
|
-
###
|
259
|
+
### Rate limit
|
260
260
|
Limit function calls for a specific period of time.
|
261
261
|
```ts
|
262
262
|
import * as cio from 'ciorent';
|
263
263
|
|
264
|
-
const fn = cio.
|
264
|
+
const fn = cio.rateLimit((id: number) => {
|
265
265
|
console.log('ID:', id);
|
266
266
|
}, 500, 1);
|
267
267
|
|
package/index.d.ts
CHANGED
@@ -45,4 +45,4 @@ export declare const debounce: <const Args extends any[]>(f: (...args: Args) =>
|
|
45
45
|
* @param ms - The time period in milliseconds
|
46
46
|
* @param limit - The call limit in the time period
|
47
47
|
*/
|
48
|
-
export declare const
|
48
|
+
export declare const rateLimit: <const Args extends any[]>(f: (...args: Args) => any, ms: number, limit: number) => ((...args: Args) => void);
|
package/index.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export let pause=Promise.resolve();export let sleep=globalThis.Bun?.sleep??globalThis.process?.getBuiltinModule?.("timers/promises").setTimeout??((ms)=>new Promise((res)=>{setTimeout(res,ms)}));let sharedBuf=new Int32Array(new SharedArrayBuffer(4));export let sleepSync=globalThis.Bun?.sleepSync??((ms)=>{Atomics.wait(sharedBuf,0,0,ms)});export let sequential=async(n,task,...args)=>{for(let i=0;i<n;i++)await task(...args,i)};export let concurrent=(n,task,...args)=>{let arr=new Array(n);for(let i=0;i<n;i++)arr[i]=task(...args,i);return Promise.all(arr)};export let debounce=(f,ms)=>{let id;return(...a)=>{clearTimeout(id);id=setTimeout(f,ms,...a)}};export let
|
1
|
+
export let pause=Promise.resolve();export let sleep=globalThis.Bun?.sleep??globalThis.process?.getBuiltinModule?.("timers/promises").setTimeout??((ms)=>new Promise((res)=>{setTimeout(res,ms)}));let sharedBuf=new Int32Array(new SharedArrayBuffer(4));export let sleepSync=globalThis.Bun?.sleepSync??((ms)=>{Atomics.wait(sharedBuf,0,0,ms)});export let sequential=async(n,task,...args)=>{for(let i=0;i<n;i++)await task(...args,i)};export let concurrent=(n,task,...args)=>{let arr=new Array(n);for(let i=0;i<n;i++)arr[i]=task(...args,i);return Promise.all(arr)};export let debounce=(f,ms)=>{let id;return(...a)=>{clearTimeout(id);id=setTimeout(f,ms,...a)}};export let rateLimit=(f,ms,limit)=>{let cur=limit;let call=()=>{cur++};return(...a)=>{if(cur>0){cur--;f(...a);setTimeout(call,ms)}}};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "ciorent",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.25",
|
4
4
|
"description": "A lightweight, low-overhead concurrency library",
|
5
5
|
"homepage": "https://ciorent.netlify.app",
|
6
6
|
"repository": {
|
@@ -18,14 +18,14 @@
|
|
18
18
|
"main": "./index.js",
|
19
19
|
"types": "./index.d.ts",
|
20
20
|
"exports": {
|
21
|
+
"./latch": "./latch.js",
|
21
22
|
"./sliding-queue": "./sliding-queue.js",
|
23
|
+
"./channel": "./channel.js",
|
22
24
|
"./fixed-queue": "./fixed-queue.js",
|
23
|
-
"./dropping-queue": "./dropping-queue.js",
|
24
|
-
"./latch": "./latch.js",
|
25
|
-
"./semaphore": "./semaphore.js",
|
26
25
|
".": "./index.js",
|
26
|
+
"./dropping-queue": "./dropping-queue.js",
|
27
27
|
"./topic": "./topic.js",
|
28
|
-
"./
|
28
|
+
"./semaphore": "./semaphore.js",
|
29
29
|
"./fiber": "./fiber.js"
|
30
30
|
}
|
31
31
|
}
|