ciorent 0.1.2 → 0.1.3

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.
Files changed (4) hide show
  1. package/README.md +59 -61
  2. package/fiber.d.ts +31 -27
  3. package/fiber.js +1 -1
  4. package/package.json +5 -5
package/README.md CHANGED
@@ -28,67 +28,6 @@ const task = async (id: number) => {
28
28
  cio.concurrent(5, task);
29
29
  ```
30
30
 
31
- ## Fibers
32
- Virtual threads with more controlled execution.
33
-
34
- ```ts
35
- import * as cio from 'ciorent';
36
- import * as fiber from 'ciorent/fiber';
37
-
38
- const thread1 = fiber.fn(function* () {
39
- console.log('Fiber 1 started');
40
-
41
- // Thread1 will be interrupted by thread2
42
- // As thread2 will end first
43
- yield cio.sleep(1000);
44
-
45
- console.log('Fiber 1 done');
46
- });
47
-
48
- const thread2 = fiber.fn(function* (thread) {
49
- console.log('Fiber 2 started');
50
-
51
- yield;
52
- console.log('Fiber 2 resumed');
53
-
54
- // Start thread 1 and make thread1
55
- // lifetime depends on thread2
56
- fiber.mount(fiber.spawn(thread1), thread);
57
-
58
- console.log('Fiber 2 done');
59
- });
60
-
61
- // Start running the thread
62
- fiber.spawn(thread2);
63
- ```
64
-
65
- ## Latch
66
- Latch is a synchronization primitive that allows one process to wait until another completes an operation before continuing execution.
67
-
68
- ```ts
69
- import * as latch from 'ciorent/latch';
70
-
71
- const startFetch = latch.init();
72
-
73
- const task = async () => {
74
- // Blocks until the latch is open
75
- await latch.pause(startFetch);
76
-
77
- console.log('Start fetching...');
78
- const res = await fetch('http://example.com');
79
- console.log('Fetch status:', res.status);
80
- }
81
-
82
- const prepare = () => {
83
- // This always run first
84
- console.log('Run before fetch:', performance.now().toFixed(2));
85
- latch.open(startFetch);
86
- }
87
-
88
- task();
89
- prepare();
90
- ```
91
-
92
31
  ## Pubsub
93
32
  A fast, simple publish-subscribe API.
94
33
 
@@ -163,6 +102,65 @@ run();
163
102
  console.log('Starting...');
164
103
  ```
165
104
 
105
+ ## Latch
106
+ Latch is a synchronization primitive that allows one process to wait until another completes an operation before continuing execution.
107
+
108
+ ```ts
109
+ import * as latch from 'ciorent/latch';
110
+
111
+ const startFetch = latch.init();
112
+
113
+ const task = async () => {
114
+ // Blocks until the latch is open
115
+ await latch.pause(startFetch);
116
+
117
+ console.log('Start fetching...');
118
+ const res = await fetch('http://example.com');
119
+ console.log('Fetch status:', res.status);
120
+ }
121
+
122
+ const prepare = () => {
123
+ // This always run first
124
+ console.log('Run before fetch:', performance.now().toFixed(2));
125
+ latch.open(startFetch);
126
+ }
127
+
128
+ task();
129
+ prepare();
130
+ ```
131
+
132
+ ## Fibers
133
+ Virtual threads with more controlled execution.
134
+
135
+ ```ts
136
+ import * as cio from 'ciorent';
137
+ import * as fiber from 'ciorent/fiber';
138
+
139
+ const f1 = fiber.fn(function* () {
140
+ console.log('Fiber 1 started');
141
+
142
+ // Wait for a promise
143
+ yield cio.sleep(1000);
144
+
145
+ console.log('Fiber 1 done');
146
+ return Math.random();
147
+ });
148
+
149
+ fiber.spawn(function* (proc) {
150
+ console.log('Fiber 2 started');
151
+
152
+ // Start f1, wait for it to finish and get the result
153
+ const res = yield* fiber.join(fiber.spawn(f1));
154
+ console.log('Fiber 1 result:', res);
155
+
156
+ // Start f1 and make its lifetime depends on current fiber
157
+ fiber.mount(fiber.spawn(f1), proc);
158
+
159
+ // The runtime will interrupt f1
160
+ console.log('Fiber 2 done');
161
+ });
162
+ ```
163
+
166
164
  ## Utilities
167
165
  ### Pausing
168
166
  Delay the execution of a function for other asynchronous tasks to run.
package/fiber.d.ts CHANGED
@@ -2,47 +2,51 @@
2
2
  * @module Fibers
3
3
  */
4
4
  /**
5
- * Check whether the fiber is paused
5
+ * Describe a fiber process
6
6
  */
7
- export declare const paused: (t: Thread) => boolean;
8
- /**
9
- * Check whether the fiber is running
10
- */
11
- export declare const running: (t: Thread) => boolean;
12
- /**
13
- * Check whether the fiber is done
14
- */
15
- export declare const done: (t: Thread) => boolean;
16
- /**
17
- * Describe a fiber
18
- */
19
- export interface Thread<T = unknown, TReturn = unknown> {
7
+ export interface Process<TReturn = unknown> {
20
8
  /**
21
9
  * The waiting promise
22
10
  */
23
- 0: Promise<T | TReturn>;
11
+ 0: Promise<TReturn | undefined>;
24
12
  /**
25
13
  * Fiber status
26
14
  */
27
- 1: 0 | 1 | 2;
15
+ 1: 0 | 1 | 2 | 3;
28
16
  /**
29
17
  * Callback to continue running the fiber
30
18
  */
31
19
  2: null | (() => void);
32
20
  /**
33
- * Bounded threads
21
+ * Bounded fibers
34
22
  */
35
- 3: Thread[];
23
+ 3: Process[];
36
24
  }
37
25
  /**
38
26
  * Describe a fiber runtime
39
27
  */
40
- export type Runtime = <const T, const TReturn, const Args extends any[]>(gen: (thread: Thread<T, TReturn>, ...args: Args) => Generator<T, TReturn>, ...args: Args) => Thread<T, TReturn>;
28
+ export type Runtime = <const TReturn, const Args extends any[]>(gen: (proc: Process<TReturn>, ...args: Args) => Generator<any, TReturn>, ...args: Args) => Process<TReturn>;
29
+ /**
30
+ * Check whether the fiber is paused
31
+ */
32
+ export declare const paused: (t: Process) => boolean;
33
+ /**
34
+ * Check whether the fiber is running
35
+ */
36
+ export declare const running: (t: Process) => boolean;
37
+ /**
38
+ * Check whether the fiber is finished
39
+ */
40
+ export declare const done: (t: Process) => boolean;
41
+ /**
42
+ * Check whether the fiber is interrupted
43
+ */
44
+ export declare const stopped: (t: Process) => boolean;
41
45
  /**
42
46
  * Create a fiber function
43
47
  * @param f
44
48
  */
45
- export declare const fn: <const Fn extends (thread: Thread, ...args: any[]) => Generator>(f: Fn) => Fn;
49
+ export declare const fn: <const Fn extends (thread: Process, ...args: any[]) => Generator>(f: Fn) => Fn;
46
50
  /**
47
51
  * A basic fiber runtime
48
52
  * @param g
@@ -52,39 +56,39 @@ export declare const spawn: Runtime;
52
56
  * Pause the execution of a fiber
53
57
  * @param t
54
58
  */
55
- export declare const pause: (t: Thread) => void;
59
+ export declare const pause: (t: Process) => void;
56
60
  /**
57
61
  * Resume the execution of a fiber
58
62
  * @param t
59
63
  */
60
- export declare const resume: (t: Thread) => void;
64
+ export declare const resume: (t: Process) => void;
61
65
  /**
62
66
  * Stop the execution of a fiber
63
67
  * @param t
64
68
  */
65
- export declare const stop: (t: Thread) => void;
69
+ export declare const stop: (t: Process) => void;
66
70
  /**
67
71
  * Wait for a fiber and retrieve its result
68
72
  * @param t
69
73
  */
70
- export declare function join<T extends Thread>(t: T): Generator<Awaited<T[1]>, Awaited<T[1]>>;
74
+ export declare function join<T extends Process>(t: T): Generator<Awaited<T[0]>, Awaited<T[0]>>;
71
75
  /**
72
76
  * Wait for a fiber to finish and retrieve its result
73
77
  * @param t
74
78
  */
75
- export declare const finish: <T extends Thread>(t: T) => T[1];
79
+ export declare const finish: <T extends Process>(t: T) => T[3];
76
80
  /**
77
81
  * Mount child fiber lifetime to parent lifetime
78
82
  * @param child
79
83
  * @param parent
80
84
  */
81
- export declare const mount: (child: Thread, parent: Thread) => void;
85
+ export declare const mount: (child: Process, parent: Process) => void;
82
86
  /**
83
87
  * Control the fiber with an abort signal
84
88
  * @param t
85
89
  * @param signal
86
90
  */
87
- export declare const control: (t: Thread, signal: AbortSignal) => void;
91
+ export declare const control: (t: Process, signal: AbortSignal) => void;
88
92
  /**
89
93
  * Unwrap a promise result
90
94
  */
package/fiber.js CHANGED
@@ -1 +1 @@
1
- export let paused=(t)=>t[1]===0;export let running=(t)=>t[1]===1;export let done=(t)=>t[1]===2;let invoke=async(g,thread)=>{try{let t=g.next();while(!t.done){let v=await t.value;if(thread[1]===0){let r;let p=new Promise((res)=>{r=res});thread[2]=r;await p}if(thread[1]===2)return v;t=g.next(v)}thread[1]=2;return t.value}finally{thread[3].forEach(stop)}};export let fn=(f)=>f;export let spawn=(f,...args)=>{let thread=[null,1,null,[]];thread[0]=invoke(f(thread,...args),thread);return thread};export let pause=(t)=>{if(t[1]===1)t[1]=0};export let resume=(t)=>{if(t[1]===0){t[1]=1;t[2]?.()}};export let stop=(t)=>{if(t[1]===0){t[1]=2;t[2]?.()}else t[1]=2};export function*join(t){return yield t[1]}export let finish=(t)=>t[1];export let mount=(child,parent)=>{parent[3].push(child)};export let control=(t,signal)=>{signal.addEventListener("abort",()=>{stop(t)})};export function*unwrap(t){return yield t}
1
+ export let paused=(t)=>t[1]===0;export let running=(t)=>t[1]===1;export let done=(t)=>t[1]===2;export let stopped=(t)=>t[1]===3;let invoke=async(g,thread)=>{try{let t=g.next();while(!t.done){let v=await t.value;if(thread[1]===0){let r;let p=new Promise((res)=>{r=res});thread[2]=r;await p}if(thread[1]===3)return;t=g.next(v)}thread[1]=2;return t.value}finally{thread[3].forEach(stop)}};export let fn=(f)=>f;export let spawn=(f,...args)=>{let thread=[null,1,null,[]];thread[0]=invoke(f(thread,...args),thread);return thread};export let pause=(t)=>{if(t[1]===1)t[1]=0};export let resume=(t)=>{if(t[1]===0){t[1]=1;t[2]?.()}};export let stop=(t)=>{if(t[1]===0)t[2]?.();t[1]=3};export function*join(t){return yield t[0]}export let finish=(t)=>t[3];export let mount=(child,parent)=>{parent[3].push(child)};export let control=(t,signal)=>{signal.addEventListener("abort",()=>{stop(t)})};export function*unwrap(t){return yield t}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
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",
22
21
  "./sliding-queue": "./sliding-queue.js",
23
- "./dropping-queue": "./dropping-queue.js",
22
+ "./fixed-queue": "./fixed-queue.js",
24
23
  "./fiber": "./fiber.js",
25
- "./channel": "./channel.js",
26
24
  ".": "./index.js",
27
- "./topic": "./topic.js",
28
25
  "./semaphore": "./semaphore.js",
26
+ "./dropping-queue": "./dropping-queue.js",
27
+ "./topic": "./topic.js",
28
+ "./channel": "./channel.js",
29
29
  "./latch": "./latch.js"
30
30
  }
31
31
  }