ciorent 0.4.0 → 0.4.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
@@ -111,34 +111,33 @@ task1();
111
111
  co.spawn(5, task2);
112
112
  ```
113
113
 
114
- ### Pubsub
115
- Pubsub allows broadcasting messages to topics that can be recieved by subscribers.
114
+ ### Semaphore
115
+ Semaphore is a concurrency primitive used to control access to a common resource by multiple processes.
116
116
 
117
117
  ```ts
118
- import * as topic from 'ciorent/topic';
118
+ import * as semaphore from 'ciorent/semaphore';
119
119
  import * as co from 'ciorent';
120
120
 
121
- const numbers = topic.init<number>();
121
+ // Only allow 2 task to run concurrently
122
+ const sem = semaphore.init(2);
122
123
 
123
- // Spawn 3 tasks that subscribe to the topic
124
- co.spawn(3, async (id) => {
125
- const subscriber = topic.subscribe(numbers);
124
+ const task = async (id: number) => {
125
+ // Acquire the semaphore or wait for the semaphore to be available
126
+ await semaphore.acquire(sem);
126
127
 
127
- while (true) {
128
- const msg = await topic.dispatch(subscriber);
129
- if (msg == null) return;
130
- console.log('Task', id, 'recieved:', msg);
131
- }
132
- });
128
+ console.log('Task', id, 'started');
133
129
 
134
- // Publish messages to the topic
135
- for (let i = 0; i < 3; i++) {
136
- topic.publish(numbers, i);
137
- await co.nextTick;
130
+ // Let the main thread schedules other tasks
131
+ for (let i = 1; i <= 5; i++) await co.nextTick;
132
+
133
+ console.log('Task', id, 'end');
134
+
135
+ // Release the semaphore
136
+ semaphore.release(sem);
138
137
  }
139
138
 
140
- // Send undefined to every topic
141
- topic.flush(numbers);
139
+ // Try to run 5 tasks concurrently
140
+ co.spawn(5, task);
142
141
  ```
143
142
 
144
143
  ### Stream
@@ -169,35 +168,6 @@ for (let i = 0; i < 3; i++) {
169
168
  stream.flush(numbers);
170
169
  ```
171
170
 
172
- ### Semaphore
173
- Semaphore is a concurrency primitive used to control access to a common resource by multiple processes.
174
-
175
- ```ts
176
- import * as semaphore from 'ciorent/semaphore';
177
- import * as co from 'ciorent';
178
-
179
- // Only allow 2 task to run concurrently
180
- const sem = semaphore.init(2);
181
-
182
- const task = async (id: number) => {
183
- // Acquire the semaphore or wait for the semaphore to be available
184
- await semaphore.acquire(sem);
185
-
186
- console.log('Task', id, 'started');
187
-
188
- // Let the main thread schedules other tasks
189
- for (let i = 1; i <= 5; i++) await co.nextTick;
190
-
191
- console.log('Task', id, 'end');
192
-
193
- // Release the semaphore
194
- semaphore.release(sem);
195
- }
196
-
197
- // Try to run 5 tasks concurrently
198
- co.spawn(5, task);
199
- ```
200
-
201
171
  ### Fibers
202
172
  Virtual threads with more controlled execution.
203
173
 
package/fiber.d.ts CHANGED
@@ -6,22 +6,17 @@
6
6
  */
7
7
  export type Process<TReturn = unknown> = [
8
8
  proc: Promise<TReturn | undefined>,
9
- status: 0 | 1 | 2 | 3,
10
- resume: null | ((state: 1 | 3) => void),
9
+ status: 1 | 2 | 3,
11
10
  children: Process[]
12
11
  ];
13
12
  /**
14
13
  * Describe a fiber runtime
15
14
  */
16
15
  export type Runtime = <const TReturn, const Args extends any[]>(gen: (proc: Process<TReturn>, ...args: Args) => Generator<any, TReturn>, ...args: Args) => Process<TReturn>;
17
- /**
18
- * Check whether the fiber has been paused
19
- */
20
- export declare const paused: (t: Process) => boolean;
21
16
  /**
22
17
  * Check whether the fiber is running
23
18
  */
24
- export declare const resumed: (t: Process) => boolean;
19
+ export declare const running: (t: Process) => boolean;
25
20
  /**
26
21
  * Check whether the fiber has completed
27
22
  */
@@ -40,16 +35,6 @@ export declare const fn: <const Fn extends (thread: Process, ...args: any[]) =>
40
35
  * @param g
41
36
  */
42
37
  export declare const spawn: Runtime;
43
- /**
44
- * Pause the execution of a fiber
45
- * @param t
46
- */
47
- export declare const pause: (t: Process) => void;
48
- /**
49
- * Resume the execution of a fiber
50
- * @param t
51
- */
52
- export declare const resume: (t: Process) => void;
53
38
  /**
54
39
  * Interrupt the execution of a fiber
55
40
  * @param t
package/fiber.js CHANGED
@@ -1 +1 @@
1
- import{sleep}from"./index.js";export let paused=(t)=>t[1]===0;export let resumed=(t)=>t[1]===1;export let completed=(t)=>t[1]===2;export let interrupted=(t)=>t[1]===3;let invoke=async(g,thread)=>{await 0;try{let t=g.next();while(!t.done){let v=await t.value;if(thread[1]===0)thread[1]=await new Promise((res)=>{thread[2]=res});if(thread[1]===3)return;t=g.next(v)}thread[1]=2;return t.value}finally{if(thread[1]!==2)thread[1]=3;thread[3].forEach(interrupt)}};export let fn=(f)=>f;export let spawn=(f,...args)=>{let t=[null,1,null,[]];t[0]=invoke(f(t,...args),t);return t};export let pause=(t)=>{if(t[1]===1)t[1]=0};export let resume=(t)=>{if(t[1]===0){if(t[2]===null)t[1]=1;else t[2](1)}};export let interrupt=(t)=>{if(t[1]!==2){if(t[1]===0&&t[2]!==null)t[2](3);else t[1]=3}};export let timeout=async(t,ms)=>{await sleep(ms);interrupt(t)};export function*join(t){return yield t[0]}export let done=(t)=>t[0];export let mount=(child,parent)=>{parent[3].push(child)};export let control=(t,signal)=>{signal.addEventListener("abort",()=>{interrupt(t)})};export function*unwrap(t){return yield t}
1
+ import{nextTick,sleep}from"./index.js";export let running=(t)=>t[1]===1;export let completed=(t)=>t[1]===2;export let interrupted=(t)=>t[1]===3;let invoke=async(g,thread)=>{await nextTick;try{let t=g.next();while(!t.done){let v=await t.value;if(thread[1]===3)return;t=g.next(v)}thread[1]=2;return t.value}finally{if(thread[1]!==2)thread[1]=3;thread[2].forEach(interrupt)}};export let fn=(f)=>f;export let spawn=(f,...args)=>{let t=[null,1,[]];t[0]=invoke(f(t,...args),t);return t};export let interrupt=(t)=>{if(t[1]!==2)t[1]=3};export let timeout=async(t,ms)=>{await sleep(ms);interrupt(t)};export function*join(t){return yield t[0]}export let done=(t)=>t[0];export let mount=(child,parent)=>{parent[2].push(child)};export let control=(t,signal)=>{signal.addEventListener("abort",()=>{interrupt(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.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "A lightweight, low-overhead concurrency library",
5
5
  "homepage": "https://ciorent.netlify.app",
6
6
  "repository": {
@@ -18,11 +18,10 @@
18
18
  "main": "./index.js",
19
19
  "types": "./index.d.ts",
20
20
  "exports": {
21
- "./fiber": "./fiber.js",
22
21
  "./queue": "./queue.d.ts",
23
22
  "./semaphore": "./semaphore.js",
23
+ "./fiber": "./fiber.js",
24
24
  "./stream": "./stream.js",
25
- "./topic": "./topic.js",
26
25
  ".": "./index.js"
27
26
  }
28
27
  }
package/topic.d.ts DELETED
@@ -1,45 +0,0 @@
1
- /**
2
- * @module Pubsub
3
- */
4
- import type { PromiseFn, QueueNode } from './queue.js';
5
- /**
6
- * Describe a topic
7
- */
8
- export type Topic<T extends {} = {}> = [
9
- head: QueueNode<T>,
10
- callback: PromiseFn<void>,
11
- resolve: (() => void) | null,
12
- pending: Promise<void> | null
13
- ];
14
- /**
15
- * Describe a subscriber
16
- */
17
- export type Subscriber<T extends {} = {}> = [
18
- topic: Topic<T>,
19
- tail: QueueNode<T>
20
- ];
21
- /**
22
- * Create a topic
23
- */
24
- export declare const init: <T extends {} = {}>() => Topic<T>;
25
- /**
26
- * Publish a message to the topic
27
- * @param t - The topic
28
- * @param m - The message to publish
29
- */
30
- export declare const publish: <T extends {} = {}>(t: Topic<T>, m: T) => void;
31
- /**
32
- * Resolve all pending dispatch
33
- */
34
- export declare const flush: (t: Topic) => void;
35
- /**
36
- * Subscribe to a topic
37
- * @param t
38
- * @returns A subscriber object
39
- */
40
- export declare const subscribe: <T extends {}>(t: Topic<T>) => Subscriber<T>;
41
- /**
42
- * Wait for messages from the topic
43
- * @param s
44
- */
45
- export declare const dispatch: <T extends {}>(s: Subscriber<T>) => Promise<T | undefined>;
package/topic.js DELETED
@@ -1 +0,0 @@
1
- export let init=()=>{let t=[[null],(res)=>{t[2]=res},null,null];return t};export let publish=(t,m)=>{t[0]=t[0][0]=[null,m];t[2]?.();t[2]=null};export let flush=publish;export let subscribe=(t)=>[t,t[0]];export let dispatch=async(s)=>{if(s[1][0]===null)await(s[0][2]!==null?s[0][3]:s[0][3]=new Promise(s[0][1]));return(s[1]=s[1][0])[1]};