ciorent 0.3.0 → 0.3.2

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
@@ -74,8 +74,8 @@ co.spawn(8, async (id) => {
74
74
  });
75
75
  ```
76
76
 
77
- #### Pausing
78
- Delay the execution of a function for other asynchronous tasks to run.
77
+ #### Yield
78
+ Continue the execution on next tick, allowing other asynchronous tasks to run.
79
79
  ```ts
80
80
  import * as co from 'ciorent';
81
81
 
@@ -85,7 +85,7 @@ const task1 = async () => {
85
85
 
86
86
  // Yield control back to the runtime, allowing it to
87
87
  // schedule other tasks
88
- await co.pause;
88
+ await co.nextTick;
89
89
 
90
90
  // Simulate heavy operation
91
91
  for (let i = 0; i < (Math.random() + 15) * 1e6; i++)
@@ -159,7 +159,7 @@ const task = async (id: number) => {
159
159
  console.log('Task', id, 'started');
160
160
 
161
161
  // Let the main thread schedules other tasks
162
- for (let i = 1; i <= 5; i++) await co.pause;
162
+ for (let i = 1; i <= 5; i++) await co.nextTick;
163
163
 
164
164
  console.log('Task', id, 'end');
165
165
 
@@ -181,7 +181,7 @@ const startFetch = latch.init();
181
181
 
182
182
  const task = async () => {
183
183
  // Blocks until the latch is open
184
- await latch.pause(startFetch);
184
+ await latch.wait(startFetch);
185
185
 
186
186
  console.log('Start fetching...');
187
187
  const res = await fetch('http://example.com');
package/defer.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @module Deferred values
3
+ */
4
+ /**
5
+ * Describe a defer
6
+ */
7
+ export type Defer<T = any> = [
8
+ pause: Promise<T>,
9
+ open: (value: T | PromiseLike<T>) => void
10
+ ];
11
+ /**
12
+ * Create a latch
13
+ */
14
+ export declare const init: <T>() => Defer<T>;
15
+ /**
16
+ * Pause until a deferred is resolved
17
+ */
18
+ export declare const wait: <T>(d: Defer<T>) => Promise<T>;
19
+ /**
20
+ * Open a latch
21
+ */
22
+ export declare const resolve: <T>(d: Defer<T>, p: T | PromiseLike<T>) => void;
package/defer.js ADDED
@@ -0,0 +1 @@
1
+ export let init=()=>{let r;return[new Promise((res)=>{r=res}),r]};export let wait=(d)=>d[0];export let resolve=(d,p)=>{d[1](p)};
package/index.d.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * blocking the main thread and let other asynchronous task to run.
10
10
  */
11
- export declare const pause: Promise<void>;
11
+ export declare const nextTick: Promise<void>;
12
12
  /**
13
13
  * Sleep for a duration.
14
14
  * @param ms - Sleep duration in milliseconds
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 spawn=(n,task,...args)=>{let arr=new Array(n);for(let i=0;i<n;i++)arr[i]=task(...args,i);return arr};export let debounce=(f,ms)=>{let id;return(...a)=>{clearTimeout(id);id=setTimeout(f,ms,...a)}};export let throttle=(ms,limit)=>{let head=[null];let tail=head;let cur=limit;let scheduled=false;let unlock=()=>{cur=limit;if(tail===head){scheduled=false;return}do{cur--;(tail=tail[0])[1]()}while(cur>0&&tail!==head);setTimeout(unlock,ms)};return()=>{if(cur===0){return new Promise((res)=>{head=head[0]=[null,res]})}if(!scheduled){scheduled=true;setTimeout(unlock,ms)}cur--;return pause}};
1
+ export let nextTick=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 spawn=(n,task,...args)=>{let arr=new Array(n);for(let i=0;i<n;i++)arr[i]=task(...args,i);return arr};export let debounce=(f,ms)=>{let id;return(...a)=>{clearTimeout(id);id=setTimeout(f,ms,...a)}};export let throttle=(ms,limit)=>{let head=[null];let tail=head;let cur=limit;let scheduled=false;let unlock=()=>{cur=limit;if(tail===head){scheduled=false;return}do{cur--;(tail=tail[0])[1]()}while(cur>0&&tail!==head);setTimeout(unlock,ms)};return()=>{if(cur===0){return new Promise((res)=>{head=head[0]=[null,res]})}if(!scheduled){scheduled=true;setTimeout(unlock,ms)}cur--;return nextTick}};
package/latch.d.ts CHANGED
@@ -1,23 +1,20 @@
1
1
  /**
2
2
  * @module Latches
3
3
  */
4
+ import { type Defer } from './defer.js';
4
5
  /**
5
6
  * Describe a latch
6
7
  */
7
- export type Latch = [pause: Promise<void>, open: () => void];
8
+ export type Latch = Defer<void>;
8
9
  /**
9
10
  * Create a latch
10
11
  */
11
12
  export declare const init: () => Latch;
12
13
  /**
13
- * Pause until a latch is opened
14
+ * Wait until a latch is opened
14
15
  */
15
- export declare const pause: (latch: Latch) => Promise<void>;
16
+ export declare const wait: (d: Latch) => Promise<void>;
16
17
  /**
17
18
  * Open a latch
18
19
  */
19
20
  export declare const open: (latch: Latch) => void;
20
- /**
21
- * Close a latch
22
- */
23
- export declare const close: (latch: Latch) => void;
package/latch.js CHANGED
@@ -1 +1 @@
1
- import{pause as endPromise}from"./index.js";export let init=()=>{let r;return[new Promise((res)=>{r=res}),r]};export let pause=(latch)=>latch[0];export let open=(latch)=>{latch[1]();latch[0]=endPromise};export let close=(latch)=>{if(latch[0]===endPromise){let r;latch[0]=new Promise((res)=>{r=res});latch[1]=r}};
1
+ import{init as deferInit,wait as deferWait}from"./defer.js";export let init=deferInit;export let wait=deferWait;export let open=(latch)=>{latch[1]()};
package/lock.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @module Lock utilities
3
+ */
1
4
  import type { Node as QueueNode } from './queue.js';
2
5
  /**
3
6
  * Describe a lock
@@ -15,7 +18,6 @@ export interface Lock<T = any> {
15
18
  /**
16
19
  * Acquire an item
17
20
  * @param lock
18
- * @param value
19
21
  */
20
22
  export declare const acquire: <T>(lock: Lock<T>) => Promise<T | undefined>;
21
23
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "A lightweight, low-overhead concurrency library",
5
5
  "homepage": "https://ciorent.netlify.app",
6
6
  "repository": {
@@ -18,15 +18,16 @@
18
18
  "main": "./index.js",
19
19
  "types": "./index.d.ts",
20
20
  "exports": {
21
+ "./queue": "./queue.js",
22
+ "./topic": "./topic.js",
21
23
  "./sliding-queue": "./sliding-queue.js",
22
- "./latch": "./latch.js",
23
24
  "./lock": "./lock.js",
24
- "./dropping-queue": "./dropping-queue.js",
25
+ "./latch": "./latch.js",
26
+ "./defer": "./defer.js",
25
27
  "./semaphore": "./semaphore.js",
28
+ "./dropping-queue": "./dropping-queue.js",
26
29
  "./channel": "./channel.js",
27
- "./fiber": "./fiber.js",
28
- "./queue": "./queue.js",
29
- "./topic": "./topic.js",
30
- ".": "./index.js"
30
+ ".": "./index.js",
31
+ "./fiber": "./fiber.js"
31
32
  }
32
33
  }
package/queue.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  /**
2
- * @private
3
2
  * @module Queue utilities
4
3
  */
5
4
  /**
package/semaphore.js CHANGED
@@ -1 +1 @@
1
- import{pause as resolvedPromise}from"./index.js";import{acquire as lockAcquire,release as lockRelease}from"./lock.js";export let init=(n)=>{let root=[null];return[root,root,n]};export let acquire=(s)=>{s[2]--;return s[2]>=0?resolvedPromise:lockAcquire(s)};export let release=(s)=>{if(s[2]<0)lockRelease(s);s[2]++};export let bind=(f,s)=>async(...a)=>{s[2]--;if(s[2]<0)await acquire(s);try{return await f(...a)}finally{if(s[2]<0)release(s);s[2]++}};
1
+ import{nextTick as resolvedPromise}from"./index.js";import{acquire as lockAcquire,release as lockRelease}from"./lock.js";export let init=(n)=>{let root=[null];return[root,root,n]};export let acquire=(s)=>{s[2]--;return s[2]>=0?resolvedPromise:lockAcquire(s)};export let release=(s)=>{if(s[2]<0)lockRelease(s);s[2]++};export let bind=(f,s)=>async(...a)=>{s[2]--;if(s[2]<0)await acquire(s);try{return await f(...a)}finally{if(s[2]<0)release(s);s[2]++}};