ciorent 0.0.25 → 0.0.26

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
@@ -243,7 +243,7 @@ cio.concurrent(5, task);
243
243
  ```
244
244
 
245
245
  ### Debounce
246
- Dropping tasks for a period of time.
246
+ Postpones execution until after an idle period.
247
247
  ```ts
248
248
  import * as cio from 'ciorent';
249
249
 
@@ -256,11 +256,12 @@ await cio.sleep(100);
256
256
  fn(2); // fn(2) gets executed
257
257
  ```
258
258
 
259
- ### Rate limit
260
- Limit function calls for a specific period of time.
259
+ ### Rate Limit
260
+ Limits the number of calls within a time window.
261
261
  ```ts
262
262
  import * as cio from 'ciorent';
263
263
 
264
+ // Allow 1 call in 500ms
264
265
  const fn = cio.rateLimit((id: number) => {
265
266
  console.log('ID:', id);
266
267
  }, 500, 1);
@@ -272,3 +273,17 @@ await cio.sleep(500);
272
273
  fn(3); // fn(3) gets executed
273
274
  ```
274
275
 
276
+ ### Throttle
277
+ Executes a function at a regular interval.
278
+ ```ts
279
+ import * as cio from 'ciorent';
280
+
281
+ // Allow 2 calls in 500ms
282
+ const fn = cio.throttle((id: number) => {
283
+ console.log(id + ': ' + Math.floor(performance.now()) + 'ms');
284
+ }, 500, 2);
285
+
286
+ for (let i = 0; i < 8; i++)
287
+ fn(i);
288
+ ```
289
+
package/index.d.ts CHANGED
@@ -41,8 +41,15 @@ export declare const concurrent: <const T extends any[], const R>(n: number, tas
41
41
  export declare const debounce: <const Args extends any[]>(f: (...args: Args) => any, ms: number) => ((...args: Args) => void);
42
42
  /**
43
43
  * Drop function calls for a specific period
44
- * @param f - The target function to throttle
44
+ * @param f - The target function to rate limit
45
45
  * @param ms - The time period in milliseconds
46
46
  * @param limit - The call limit in the time period
47
47
  */
48
48
  export declare const rateLimit: <const Args extends any[]>(f: (...args: Args) => any, ms: number, limit: number) => ((...args: Args) => void);
49
+ /**
50
+ * Throttle function execution for a time period
51
+ * @param f - The function to throttle
52
+ * @param ms - The time in milliseconds
53
+ * @param limit - The call limit in the time period
54
+ */
55
+ export declare const throttle: <const Args extends any[], const R>(f: (...args: Args) => R, ms: number, limit: number) => ((...args: Args) => Promise<Awaited<R>>);
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 rateLimit=(f,ms,limit)=>{let cur=limit;let call=()=>{cur++};return(...a)=>{if(cur>0){cur--;f(...a);setTimeout(call,ms)}}};
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 call=()=>{limit++};return(...a)=>{if(limit>0){limit--;try{f(...a)}finally{setTimeout(call,ms)}}}};export let throttle=(f,ms,limit)=>{let head=[null,null,null];let tail=head;let unlock=()=>{if(tail!==head){tail=tail[2];tail[0](f(...tail[1]));setTimeout(unlock,ms)}else limit++};return(...a)=>{if(limit===0){let r;let p=new Promise((res)=>{r=res});head=head[2]=[r,a,null];return p}limit--;setTimeout(unlock,ms);return f(...a)}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.0.25",
3
+ "version": "0.0.26",
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
+ "./fixed-queue": "./fixed-queue.js",
21
22
  "./latch": "./latch.js",
22
23
  "./sliding-queue": "./sliding-queue.js",
23
- "./channel": "./channel.js",
24
- "./fixed-queue": "./fixed-queue.js",
25
24
  ".": "./index.js",
26
- "./dropping-queue": "./dropping-queue.js",
27
- "./topic": "./topic.js",
28
25
  "./semaphore": "./semaphore.js",
26
+ "./topic": "./topic.js",
27
+ "./channel": "./channel.js",
28
+ "./dropping-queue": "./dropping-queue.js",
29
29
  "./fiber": "./fiber.js"
30
30
  }
31
31
  }