ciorent 0.0.18 → 0.0.20
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 +54 -53
- package/{fiber/index.d.ts → fiber.d.ts} +27 -16
- package/fiber.js +1 -0
- package/package.json +6 -7
- package/concurrent.d.ts +0 -11
- package/fiber/index.js +0 -1
package/README.md
CHANGED
@@ -30,70 +30,31 @@ Virtual threads with more controlled execution.
|
|
30
30
|
import * as cio from 'ciorent';
|
31
31
|
import * as fiber from 'ciorent/fiber';
|
32
32
|
|
33
|
-
function*
|
33
|
+
const thread1 = fiber.fn(function* () {
|
34
34
|
console.log('Fiber 1 started');
|
35
|
-
|
35
|
+
|
36
|
+
// Thread1 will be interrupted by thread2
|
37
|
+
// As thread2 will end first
|
38
|
+
yield cio.sleep(1000);
|
39
|
+
|
36
40
|
console.log('Fiber 1 done');
|
37
|
-
}
|
41
|
+
})
|
38
42
|
|
39
|
-
function*
|
43
|
+
const thread2 = fiber.fn(function* (thread) {
|
40
44
|
console.log('Fiber 2 started');
|
41
45
|
|
42
46
|
yield;
|
43
47
|
console.log('Fiber 2 resumed');
|
44
48
|
|
45
|
-
//
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
// Start task 1 and wait for it to be done
|
50
|
-
yield* fiber.join(
|
51
|
-
fiber.start(thread1())
|
52
|
-
);
|
49
|
+
// Start thread 1 and make thread1
|
50
|
+
// lifetime depends on thread2
|
51
|
+
fiber.mount(fiber.start(thread1), thread);
|
53
52
|
|
54
53
|
console.log('Fiber 2 done');
|
55
|
-
}
|
54
|
+
});
|
56
55
|
|
57
56
|
// Start running the thread
|
58
|
-
fiber.start(thread2
|
59
|
-
```
|
60
|
-
|
61
|
-
## Channel
|
62
|
-
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.
|
63
|
-
|
64
|
-
```ts
|
65
|
-
import * as channel from 'ciorent/channel';
|
66
|
-
import * as cio from 'ciorent';
|
67
|
-
|
68
|
-
const c = channel.init<number>();
|
69
|
-
|
70
|
-
const run = async () => {
|
71
|
-
for (let i = 0; i < 10; i++) {
|
72
|
-
await cio.sleep(10);
|
73
|
-
channel.send(c, i);
|
74
|
-
console.log('Sent', i);
|
75
|
-
}
|
76
|
-
|
77
|
-
// Resolve all waiting promises with `undefined`
|
78
|
-
// This is a way to tell the reciever to not listen to more data
|
79
|
-
channel.flush(c);
|
80
|
-
};
|
81
|
-
|
82
|
-
const log = async () => {
|
83
|
-
while (true) {
|
84
|
-
// Wait until a value is sent
|
85
|
-
const x = await channel.recieve(c);
|
86
|
-
if (x == null) break;
|
87
|
-
|
88
|
-
console.log('Recieved', x);
|
89
|
-
};
|
90
|
-
}
|
91
|
-
|
92
|
-
run();
|
93
|
-
log();
|
94
|
-
|
95
|
-
// This runs first
|
96
|
-
console.log('Starting...');
|
57
|
+
fiber.start(thread2);
|
97
58
|
```
|
98
59
|
|
99
60
|
## Pubsub
|
@@ -175,6 +136,44 @@ await main();
|
|
175
136
|
await main();
|
176
137
|
```
|
177
138
|
|
139
|
+
## Channel
|
140
|
+
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.
|
141
|
+
|
142
|
+
```ts
|
143
|
+
import * as channel from 'ciorent/channel';
|
144
|
+
import * as cio from 'ciorent';
|
145
|
+
|
146
|
+
const c = channel.init<number>();
|
147
|
+
|
148
|
+
const run = async () => {
|
149
|
+
for (let i = 0; i < 10; i++) {
|
150
|
+
await cio.sleep(10);
|
151
|
+
channel.send(c, i);
|
152
|
+
console.log('Sent', i);
|
153
|
+
}
|
154
|
+
|
155
|
+
// Resolve all waiting promises with `undefined`
|
156
|
+
// This is a way to tell the reciever to not listen to more data
|
157
|
+
channel.flush(c);
|
158
|
+
};
|
159
|
+
|
160
|
+
const log = async () => {
|
161
|
+
while (true) {
|
162
|
+
// Wait until a value is sent
|
163
|
+
const x = await channel.recieve(c);
|
164
|
+
if (x == null) break;
|
165
|
+
|
166
|
+
console.log('Recieved', x);
|
167
|
+
};
|
168
|
+
}
|
169
|
+
|
170
|
+
run();
|
171
|
+
log();
|
172
|
+
|
173
|
+
// This runs first
|
174
|
+
console.log('Starting...');
|
175
|
+
```
|
176
|
+
|
178
177
|
## Utilities
|
179
178
|
### Pausing
|
180
179
|
Delay the execution of a function for other asynchronous tasks to run.
|
@@ -207,13 +206,15 @@ task2();
|
|
207
206
|
```
|
208
207
|
|
209
208
|
### Sleep
|
210
|
-
|
209
|
+
Cross-runtime synchronous and asynchronous sleep functions.
|
211
210
|
```ts
|
212
211
|
import { sleep, sleepSync } from 'ciorent';
|
213
212
|
|
214
213
|
await sleep(500);
|
215
214
|
console.log('Hi');
|
216
215
|
|
216
|
+
// This blocks the current thread
|
217
|
+
// On the browser this only works in workers
|
217
218
|
sleepSync(500);
|
218
219
|
console.log('Hi');
|
219
220
|
```
|
@@ -1,43 +1,48 @@
|
|
1
1
|
/**
|
2
|
-
* @module
|
2
|
+
* @module Fibers
|
3
3
|
*/
|
4
4
|
/**
|
5
|
-
*
|
5
|
+
* Check whether the fiber is paused
|
6
6
|
*/
|
7
7
|
export declare const paused: (t: Thread) => boolean;
|
8
8
|
/**
|
9
|
-
*
|
9
|
+
* Check whether the fiber is running
|
10
10
|
*/
|
11
11
|
export declare const running: (t: Thread) => boolean;
|
12
12
|
/**
|
13
|
-
*
|
13
|
+
* Check whether the fiber is done
|
14
14
|
*/
|
15
15
|
export declare const done: (t: Thread) => boolean;
|
16
16
|
/**
|
17
17
|
* Describe a fiber
|
18
18
|
*/
|
19
19
|
export interface Thread<T = unknown, TReturn = unknown> {
|
20
|
-
/**
|
21
|
-
* The original generator
|
22
|
-
*/
|
23
|
-
0: Generator<T, TReturn>;
|
24
20
|
/**
|
25
21
|
* The waiting promise
|
26
22
|
*/
|
27
|
-
|
23
|
+
0: Promise<T | TReturn>;
|
28
24
|
/**
|
29
25
|
* Fiber status
|
30
26
|
*/
|
31
|
-
|
27
|
+
1: 0 | 1 | 2;
|
32
28
|
/**
|
33
29
|
* Callback to continue running the fiber
|
34
30
|
*/
|
35
|
-
|
31
|
+
2: null | (() => void);
|
32
|
+
/**
|
33
|
+
* Bounded threads
|
34
|
+
*/
|
35
|
+
3: Thread[];
|
36
36
|
}
|
37
37
|
/**
|
38
38
|
* Describe a fiber runtime
|
39
39
|
*/
|
40
|
-
export type Runtime = <const T, const TReturn>(gen: Generator<T, TReturn
|
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>;
|
41
|
+
/**
|
42
|
+
* Create a fiber function
|
43
|
+
* @param f
|
44
|
+
*/
|
45
|
+
export declare const fn: <const Fn extends (thread: Thread, ...args: any[]) => Generator>(f: Fn) => Fn;
|
41
46
|
/**
|
42
47
|
* A basic fiber runtime
|
43
48
|
* @param g
|
@@ -54,10 +59,10 @@ export declare const pause: (t: Thread) => void;
|
|
54
59
|
*/
|
55
60
|
export declare const resume: (t: Thread) => void;
|
56
61
|
/**
|
57
|
-
* Stop the execution of a fiber
|
62
|
+
* Stop the execution of a fiber
|
58
63
|
* @param t
|
59
64
|
*/
|
60
|
-
export declare const stop:
|
65
|
+
export declare const stop: (t: Thread) => void;
|
61
66
|
/**
|
62
67
|
* Wait for a fiber and retrieve its result
|
63
68
|
* @param t
|
@@ -69,6 +74,12 @@ export declare function join<T extends Thread>(t: T): Generator<Awaited<T[1]>, A
|
|
69
74
|
*/
|
70
75
|
export declare const finish: <T extends Thread>(t: T) => T[1];
|
71
76
|
/**
|
72
|
-
*
|
77
|
+
* Mount child fiber lifetime to parent lifetime
|
78
|
+
* @param child
|
79
|
+
* @param parent
|
80
|
+
*/
|
81
|
+
export declare const mount: (child: Thread, parent: Thread) => void;
|
82
|
+
/**
|
83
|
+
* Unwrap a promise result
|
73
84
|
*/
|
74
|
-
export declare function
|
85
|
+
export declare function unwrap<T extends Promise<any>>(t: T): Generator<Awaited<T>, Awaited<T>>;
|
package/fiber.js
ADDED
@@ -0,0 +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)=>{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){thread[3].forEach(stop);return v}t=g.next(v)}thread[1]=2;thread[3].forEach(stop);return t.value};export let fn=(f)=>f;export let start=(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 function*unwrap(t){return yield t}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "ciorent",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.20",
|
4
4
|
"description": "A lightweight, low-overhead concurrency library",
|
5
5
|
"homepage": "https://ciorent.netlify.app",
|
6
6
|
"repository": {
|
@@ -20,13 +20,12 @@
|
|
20
20
|
"exports": {
|
21
21
|
"./fixed-queue": "./fixed-queue.js",
|
22
22
|
"./sliding-queue": "./sliding-queue.js",
|
23
|
-
"
|
23
|
+
"./fiber": "./fiber.js",
|
24
|
+
"./channel": "./channel.js",
|
24
25
|
"./dropping-queue": "./dropping-queue.js",
|
26
|
+
"./topic": "./topic.js",
|
27
|
+
".": "./index.js",
|
25
28
|
"./semaphore": "./semaphore.js",
|
26
|
-
"./
|
27
|
-
"./fiber": "./fiber/index.js",
|
28
|
-
"./latch": "./latch.js",
|
29
|
-
"./concurrent": "./concurrent.d.ts",
|
30
|
-
"./topic": "./topic.js"
|
29
|
+
"./latch": "./latch.js"
|
31
30
|
}
|
32
31
|
}
|
package/concurrent.d.ts
DELETED
package/fiber/index.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export let paused=(t)=>t[2]===0;export let running=(t)=>t[2]===1;export let done=(t)=>t[2]===2;let invoke=async(g,thread)=>{let t=g.next();while(!t.done){let v=await t.value;if(thread[2]===0){let r;let p=new Promise((res)=>{r=res});thread[3]=r;await p}if(thread[2]===2)return v;t=g.next(v)}thread[2]=2;return t.value};export let start=(g)=>{let thread=[g,null,1,null];thread[1]=invoke(g,thread);return thread};export let pause=(t)=>{if(t[2]===1)t[2]=0};export let resume=(t)=>{if(t[2]===0){t[2]=1;t[3]?.()}};export let stop=(t)=>{if(t[2]===0){t[2]=2;t[3]()}else t[2]=2;return t[1]};export function*join(t){return yield t[1]}export let finish=(t)=>t[1];export function*wait(t){return yield t}
|