ciorent 0.5.1 → 0.6.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 CHANGED
@@ -11,7 +11,7 @@ A lightweight, low-overhead concurrency library.
11
11
  ### Pausing
12
12
  Continue the execution on next tick, allowing other asynchronous tasks to run.
13
13
  ```ts
14
- import * as co from 'ciorent';
14
+ import { nextTick } from 'ciorent';
15
15
 
16
16
  const logTime = (label: string) =>
17
17
  console.log(`${label}: ${Math.floor(performance.now())}ms`);
@@ -24,7 +24,7 @@ const task1 = async () => {
24
24
  for (let i = 0, l = (Math.random() + 15) * 1e6; i < l; i++) {
25
25
  // Yield control back occasionally to the runtime, allowing
26
26
  // it to schedule other tasks
27
- if (i % 1e5 === 0) await co.nextTick;
27
+ if (i % 1e5 === 0) await nextTick;
28
28
 
29
29
  x += Math.random() * 32 + i * Math.round(Math.random() * 16);
30
30
  }
@@ -46,9 +46,9 @@ for (let i = 1; i <= 5; i++) task2(i);
46
46
  ```
47
47
 
48
48
  ### Sleep
49
- Cross-runtime synchronous and asynchronous sleep functions.
49
+ Runtime-agnostic synchronous and asynchronous sleep functions.
50
50
  ```ts
51
- import * as co from 'ciorent';
51
+ import { sleep, sleepSync } from 'ciorent';
52
52
 
53
53
  const logTime = (label: string) =>
54
54
  console.log(`${label}: ${Math.floor(performance.now())}ms`);
@@ -56,20 +56,39 @@ const logTime = (label: string) =>
56
56
  logTime('Start');
57
57
 
58
58
  // Non-blocking
59
- await co.sleep(500);
59
+ await sleep(500);
60
60
  logTime('After about 0.5s');
61
61
 
62
62
  // This blocks the event loop
63
63
  // On the browser this only works in workers and blocks the worker thread
64
- co.sleepSync(500);
64
+ sleepSync(500);
65
65
  logTime('After another 0.5s');
66
- ````
66
+ ```
67
+
68
+ ### Latches
69
+ Latches are a type of synchronization primitive that allows one thread to wait until another thread completes an operation before continuing execution.
70
+ ```ts
71
+ import { sleep, latch } from 'ciorent';
72
+
73
+ const startFetch = latch.init();
74
+
75
+ (async () => {
76
+ // Wait until the latch is opened
77
+ await latch.wait(startFetch);
78
+
79
+ const res = await fetch('http://example.com');
80
+ return res.text();
81
+ })();
82
+
83
+ // Fetch starts after 500ms
84
+ await sleep(500);
85
+ latch.open(startFetch);
86
+ ```
67
87
 
68
88
  ### Semaphores
69
89
  Semaphore is a concurrency primitive used to control access to a common resource by multiple processes.
70
90
  ```ts
71
- import * as semaphore from 'ciorent/semaphore';
72
- import * as co from 'ciorent';
91
+ import { semaphore, nextTick } from 'ciorent';
73
92
 
74
93
  // Only allow 2 task to run concurrently
75
94
  const sem = semaphore.init(2);
@@ -81,7 +100,7 @@ const task = async (id: number) => {
81
100
  console.log('Task', id, 'started');
82
101
 
83
102
  // Let the main thread schedules other tasks
84
- for (let i = 1; i <= 5; i++) await co.nextTick;
103
+ for (let i = 1; i <= 5; i++) await nextTick;
85
104
 
86
105
  console.log('Task', id, 'end');
87
106
 
@@ -95,8 +114,7 @@ for (let i = 1; i <= 5; i++) task(i);
95
114
  ### Fibers
96
115
  Virtual threads with more controlled execution.
97
116
  ```ts
98
- import * as co from 'ciorent';
99
- import * as fiber from 'ciorent/fiber';
117
+ import { fiber, sleep } from 'ciorent';
100
118
 
101
119
  const logTime = (label: string) =>
102
120
  console.log(`${label}: ${Math.floor(performance.now())}ms`);
@@ -104,7 +122,7 @@ const logTime = (label: string) =>
104
122
  const f1 = fiber.fn(function* () {
105
123
  // Wait for a promise
106
124
  console.log('Fiber 1 waiting: 1s');
107
- yield co.sleep(1000);
125
+ yield sleep(1000);
108
126
 
109
127
  // Wait for a promise and return its result
110
128
  const res = yield* fiber.unwrap(Promise.resolve(1));
package/index.d.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  /**
2
+ * @module Other utilities
3
+ */
4
+ /**
2
5
  * Continue the execution on next event loop cycle.
3
6
  *
4
7
  * You can `await` this **occasionally** in an expensive synchronous operation to avoid
@@ -26,3 +29,7 @@ export declare const timeout: <T>(promise: Promise<T>, ms: number) => Promise<T
26
29
  * @param ms - Sleep duration in milliseconds
27
30
  */
28
31
  export declare const sleepSync: (ms: number) => void;
32
+ export * as fiber from "./fiber.js";
33
+ export * as latch from "./latch.js";
34
+ export * as rateLimit from "./rate-limit.js";
35
+ export * as semaphore from "./semaphore.js";
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(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)});
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)});export*as fiber from"./fiber.js";export*as latch from"./latch.js";export*as rateLimit from"./rate-limit.js";export*as semaphore from"./semaphore.js";
package/latch.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Describe a latch
3
+ */
4
+ export type Latch = [p: Promise<void>, res: () => void, cb: (res: () => void) => void];
5
+ export declare const init: <T>() => Latch;
6
+ /**
7
+ * Reclose the latch
8
+ * @param c
9
+ */
10
+ export declare const close: (c: Latch) => void;
11
+ /**
12
+ * Open the latch
13
+ * @param c
14
+ */
15
+ export declare const open: (c: Latch) => void;
16
+ /**
17
+ * Wait for the latch to open
18
+ * @param c
19
+ */
20
+ export declare const wait: (c: Latch) => Promise<void>;
package/latch.js ADDED
@@ -0,0 +1 @@
1
+ export let init=()=>{let e=[,,r=>{e[1]=r}];close(e);return e};export let close=e=>{e[0]=new Promise(e[2])};export let open=e=>{e[1]()};export let wait=e=>e[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.5.1",
3
+ "version": "0.6.1",
4
4
  "description": "A lightweight, low-overhead concurrency library",
5
5
  "repository": {
6
6
  "type": "git",
@@ -10,12 +10,5 @@
10
10
  "license": "MIT",
11
11
  "type": "module",
12
12
  "main": "./index.js",
13
- "types": "./index.d.ts",
14
- "exports": {
15
- "./fiber": "./fiber.js",
16
- "./queue": "./queue.js",
17
- "./rate-limit": "./rate-limit.js",
18
- "./semaphore": "./semaphore.js",
19
- ".": "./index.js"
20
- }
13
+ "types": "./index.d.ts"
21
14
  }