ciorent 0.0.22 → 0.0.24

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
@@ -24,6 +24,40 @@ 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
+
27
61
  ## Latch
28
62
  Latch is a synchronization primitive that allows one process to wait until another completes an operation before continuing execution.
29
63
 
@@ -103,40 +137,6 @@ cio.concurrent(5, async (id: number) => {
103
137
  publisher();
104
138
  ```
105
139
 
106
- ## Fibers
107
- Virtual threads with more controlled execution.
108
-
109
- ```ts
110
- import * as cio from 'ciorent';
111
- import * as fiber from 'ciorent/fiber';
112
-
113
- const thread1 = fiber.fn(function* () {
114
- console.log('Fiber 1 started');
115
-
116
- // Thread1 will be interrupted by thread2
117
- // As thread2 will end first
118
- yield cio.sleep(1000);
119
-
120
- console.log('Fiber 1 done');
121
- });
122
-
123
- const thread2 = fiber.fn(function* (thread) {
124
- console.log('Fiber 2 started');
125
-
126
- yield;
127
- console.log('Fiber 2 resumed');
128
-
129
- // Start thread 1 and make thread1
130
- // lifetime depends on thread2
131
- fiber.mount(fiber.spawn(thread1), thread);
132
-
133
- console.log('Fiber 2 done');
134
- });
135
-
136
- // Start running the thread
137
- fiber.spawn(thread2);
138
- ```
139
-
140
140
  ## Channel
141
141
  Channel is a synchronization primitive via message passing. A message may be sent over a channel, and another process is able to receive messages sent over a channel it has a reference to.
142
142
 
@@ -242,3 +242,33 @@ console.log('Running 5 tasks concurrently:');
242
242
  cio.concurrent(5, task);
243
243
  ```
244
244
 
245
+ ### Debounce
246
+ Dropping tasks for a period of time.
247
+ ```ts
248
+ import * as cio from 'ciorent';
249
+
250
+ const fn = cio.debounce((id: number) => {
251
+ console.log('ID:', id);
252
+ }, 500);
253
+
254
+ fn(1); // fn(1) gets skipped
255
+ await cio.sleep(100);
256
+ fn(2); // fn(2) gets executed
257
+ ```
258
+
259
+ ### Throttle
260
+ Limit function calls for a specific period of time.
261
+ ```ts
262
+ import * as cio from 'ciorent';
263
+
264
+ const fn = cio.throttle((id: number) => {
265
+ console.log('ID:', id);
266
+ }, 500, 1);
267
+
268
+ fn(1); // fn(1) gets executed
269
+ await cio.sleep(100);
270
+ fn(2); // fn(2) gets skipped
271
+ await cio.sleep(500);
272
+ fn(3); // fn(3) gets executed
273
+ ```
274
+
package/index.d.ts CHANGED
@@ -32,4 +32,17 @@ export declare const sequential: <const T extends any[]>(n: number, task: (...ar
32
32
  * @param n
33
33
  * @param task - The function to run
34
34
  */
35
- export declare const concurrent: <const T extends any[]>(n: number, task: (...args: [...T, id: number]) => Promise<any>, ...args: T) => Promise<any>;
35
+ export declare const concurrent: <const T extends any[], const R>(n: number, task: (...args: [...T, id: number]) => Promise<R>, ...args: T) => Promise<R[]>;
36
+ /**
37
+ * Drop function calls until it doesn't get called for a specific period.
38
+ * @param f - The target function to debounce
39
+ * @param ms - The time period in milliseconds
40
+ */
41
+ export declare const debounce: <const Args extends any[]>(f: (...args: Args) => any, ms: number) => ((...args: Args) => void);
42
+ /**
43
+ * Drop function calls for a specific period
44
+ * @param f - The target function to throttle
45
+ * @param ms - The time period in milliseconds
46
+ * @param limit - The call limit in the time period
47
+ */
48
+ export declare const throttle: <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)};
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 throttle=(f,ms,limit)=>{let cur=limit;let call=(...a)=>{cur++;f(...a)};return(...a)=>{if(cur>0){cur--;setTimeout(call,ms,...a)}}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "description": "A lightweight, low-overhead concurrency library",
5
5
  "homepage": "https://ciorent.netlify.app",
6
6
  "repository": {
@@ -20,12 +20,12 @@
20
20
  "exports": {
21
21
  "./sliding-queue": "./sliding-queue.js",
22
22
  "./fixed-queue": "./fixed-queue.js",
23
- "./latch": "./latch.js",
24
23
  "./dropping-queue": "./dropping-queue.js",
25
- "./channel": "./channel.js",
24
+ "./latch": "./latch.js",
25
+ "./semaphore": "./semaphore.js",
26
26
  ".": "./index.js",
27
27
  "./topic": "./topic.js",
28
- "./semaphore": "./semaphore.js",
28
+ "./channel": "./channel.js",
29
29
  "./fiber": "./fiber.js"
30
30
  }
31
31
  }
@@ -11,6 +11,6 @@ export { init } from './fixed-queue.js';
11
11
  export declare const push: <T extends {}>(q: FixedQueue<T>, item: T) => void;
12
12
  /**
13
13
  * Pop an item from the queue
14
- * @param q - The queue to pull from
14
+ * @param q - The queue to pop from
15
15
  */
16
16
  export declare const pop: <T extends {}>(q: FixedQueue<T>) => T | undefined;