ciorent 0.0.20 → 0.0.21

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
@@ -38,7 +38,7 @@ const thread1 = fiber.fn(function* () {
38
38
  yield cio.sleep(1000);
39
39
 
40
40
  console.log('Fiber 1 done');
41
- })
41
+ });
42
42
 
43
43
  const thread2 = fiber.fn(function* (thread) {
44
44
  console.log('Fiber 2 started');
@@ -57,42 +57,6 @@ const thread2 = fiber.fn(function* (thread) {
57
57
  fiber.start(thread2);
58
58
  ```
59
59
 
60
- ## Pubsub
61
- A fast, simple publish-subscribe API.
62
-
63
- ```ts
64
- import * as topic from 'ciorent/topic';
65
- import * as cio from 'ciorent';
66
-
67
- const messages = topic.init<number>();
68
-
69
- // A task that publish messages
70
- const publisher = async () => {
71
- for (let i = 0; i < 5; i++) {
72
- await cio.sleep(100);
73
- topic.pub(messages, i);
74
- }
75
-
76
- // Resolve all waiting promises
77
- // And clear the value queue
78
- topic.flush(messages);
79
- }
80
-
81
- // Spawn 5 tasks that recieve messages
82
- cio.concurrent(5, async (id: number) => {
83
- const sub = topic.sub(messages);
84
-
85
- while (true) {
86
- // Block until the value is sent
87
- const x = await topic.recieve(sub);
88
- if (x == null) break;
89
- console.log(`Task ${id}: ${x}`);
90
- }
91
- });
92
-
93
- publisher();
94
- ```
95
-
96
60
  ## Latch
97
61
  Latch is a synchronization primitive that allows one process to wait until another completes an operation before continuing execution.
98
62
 
@@ -136,6 +100,42 @@ await main();
136
100
  await main();
137
101
  ```
138
102
 
103
+ ## Pubsub
104
+ A fast, simple publish-subscribe API.
105
+
106
+ ```ts
107
+ import * as topic from 'ciorent/topic';
108
+ import * as cio from 'ciorent';
109
+
110
+ const messages = topic.init<number>();
111
+
112
+ // A task that publish messages
113
+ const publisher = async () => {
114
+ for (let i = 0; i < 5; i++) {
115
+ await cio.sleep(100);
116
+ topic.pub(messages, i);
117
+ }
118
+
119
+ // Resolve all waiting promises
120
+ // And clear the value queue
121
+ topic.flush(messages);
122
+ }
123
+
124
+ // Spawn 5 tasks that recieve messages
125
+ cio.concurrent(5, async (id: number) => {
126
+ const sub = topic.sub(messages);
127
+
128
+ while (true) {
129
+ // Block until the value is sent
130
+ const x = await topic.recieve(sub);
131
+ if (x == null) break;
132
+ console.log(`Task ${id}: ${x}`);
133
+ }
134
+ });
135
+
136
+ publisher();
137
+ ```
138
+
139
139
  ## Channel
140
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
141
 
package/fiber.d.ts CHANGED
@@ -79,6 +79,12 @@ export declare const finish: <T extends Thread>(t: T) => T[1];
79
79
  * @param parent
80
80
  */
81
81
  export declare const mount: (child: Thread, parent: Thread) => void;
82
+ /**
83
+ * Control the fiber with an abort signal
84
+ * @param t
85
+ * @param signal
86
+ */
87
+ export declare const control: (t: Thread, signal: AbortSignal) => void;
82
88
  /**
83
89
  * Unwrap a promise result
84
90
  */
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)=>{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}
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 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.0.20",
3
+ "version": "0.0.21",
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
- "./fiber": "./fiber.js",
24
22
  "./channel": "./channel.js",
25
- "./dropping-queue": "./dropping-queue.js",
23
+ "./semaphore": "./semaphore.js",
26
24
  "./topic": "./topic.js",
27
25
  ".": "./index.js",
28
- "./semaphore": "./semaphore.js",
26
+ "./dropping-queue": "./dropping-queue.js",
27
+ "./fiber": "./fiber.js",
28
+ "./fixed-queue": "./fixed-queue.js",
29
29
  "./latch": "./latch.js"
30
30
  }
31
31
  }