ciorent 0.3.9 → 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,36 +111,6 @@ 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.
116
-
117
- ```ts
118
- import * as topic from 'ciorent/topic';
119
- import * as co from 'ciorent';
120
-
121
- const numbers = topic.init<number>();
122
-
123
- // Spawn 3 tasks that subscribe to the topic
124
- co.spawn(3, async (id) => {
125
- const subscriber = topic.subscribe(numbers);
126
-
127
- while (true) {
128
- const msg = await topic.dispatch(subscriber);
129
- if (msg == null) return;
130
- console.log('Task', id, 'recieved:', msg);
131
- }
132
- });
133
-
134
- // Publish messages to the topic
135
- for (let i = 0; i < 3; i++) {
136
- topic.publish(numbers, i);
137
- await co.nextTick;
138
- }
139
-
140
- // Send undefined to every topic
141
- topic.flush(numbers);
142
- ```
143
-
144
114
  ### Semaphore
145
115
  Semaphore is a concurrency primitive used to control access to a common resource by multiple processes.
146
116
 
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.3.9",
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,13 +18,10 @@
18
18
  "main": "./index.js",
19
19
  "types": "./index.d.ts",
20
20
  "exports": {
21
- "./queue": "./queue.js",
22
- "./sliding-queue": "./sliding-queue.js",
23
- ".": "./index.js",
21
+ "./queue": "./queue.d.ts",
24
22
  "./semaphore": "./semaphore.js",
25
- "./topic": "./topic.js",
26
23
  "./fiber": "./fiber.js",
27
24
  "./stream": "./stream.js",
28
- "./dropping-queue": "./dropping-queue.js"
25
+ ".": "./index.js"
29
26
  }
30
27
  }
package/queue.d.ts CHANGED
@@ -17,12 +17,8 @@ export type QueueNode<T> = [next: QueueNode<T> | null, value: T];
17
17
  /**
18
18
  * Describe an unbounded queue
19
19
  */
20
- export type UnboundedQueue<T> = [
21
- head: QueueNode<T>,
22
- tail: QueueNode<T>
23
- ];
20
+ export type UnboundedQueue<T> = [head: QueueNode<T>, tail: QueueNode<T>];
24
21
  /**
25
- * Create a fixed queue
26
- * @param n - The queue size
22
+ * Cached promise callback
27
23
  */
28
- export declare const fixed: <T extends {} = {}>(n: number) => FixedQueue<T>;
24
+ export type PromiseFn<T = any> = (res: (value?: T) => void) => void;
package/semaphore.d.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * @module Semaphores
3
3
  */
4
- import type { UnboundedQueue } from './queue.js';
4
+ import type { PromiseFn, UnboundedQueue } from './queue.js';
5
5
  /**
6
6
  * Describe a semaphore
7
7
  */
8
8
  export type Semaphore = [
9
9
  ...UnboundedQueue<() => void>,
10
+ callback: PromiseFn<void>,
10
11
  remain: number
11
12
  ];
12
13
  /**
package/stream.d.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * @module Streams
3
3
  */
4
- import type { UnboundedQueue } from './queue.js';
4
+ import type { PromiseFn, UnboundedQueue } from './queue.js';
5
5
  /**
6
6
  * Describe a stream
7
7
  */
8
8
  export type Stream<T extends {} = {}> = [
9
9
  ...UnboundedQueue<T | ((val?: T) => void)>,
10
+ callback: PromiseFn<T>,
10
11
  queueing: boolean
11
12
  ];
12
13
  /**
@@ -1,12 +0,0 @@
1
- /**
2
- * @module Dropping queues
3
- */
4
- import type { FixedQueue } from './queue.js';
5
- export { fixed as init } from './queue.js';
6
- export { pop } from './sliding-queue.js';
7
- /**
8
- * Push an item to a dropping queue
9
- * @param q - The queue to push to
10
- * @param item
11
- */
12
- export declare const push: <T extends {}>(q: FixedQueue<T>, item: T) => boolean;
package/dropping-queue.js DELETED
@@ -1 +0,0 @@
1
- export{fixed as init}from"./queue.js";export{pop}from"./sliding-queue.js";export let push=(q,item)=>{if(q[0][(q[2]+1)%q[1]]!=null)return false;q[0][q[2]=(q[2]+1)%q[1]]=item;return true};
package/queue.js DELETED
@@ -1 +0,0 @@
1
- export let fixed=(n)=>[new Array(n),n,-1,-1];
@@ -1,16 +0,0 @@
1
- /**
2
- * @module Sliding queues
3
- */
4
- import type { FixedQueue } from './queue.js';
5
- export { fixed as init } from './queue.js';
6
- /**
7
- * Push an item to a sliding queue
8
- * @param q - The queue to push to
9
- * @param item
10
- */
11
- export declare const push: <T extends {}>(q: FixedQueue<T>, item: T) => void;
12
- /**
13
- * Pop an item from the queue
14
- * @param q - The queue to pop from
15
- */
16
- export declare const pop: <T extends {}>(q: FixedQueue<T>) => T | undefined;
package/sliding-queue.js DELETED
@@ -1 +0,0 @@
1
- export{fixed as init}from"./queue.js";export let push=(q,item)=>{q[0][q[2]=(q[2]+1)%q[1]]=item};export let pop=(q)=>{let val=q[0][(q[3]+1)%q[1]];if(val!=null){q[0][q[3]=(q[3]+1)%q[1]]=null;return val}};
package/topic.d.ts DELETED
@@ -1,44 +0,0 @@
1
- /**
2
- * @module Pubsub
3
- */
4
- import type { QueueNode } from "./queue.js";
5
- /**
6
- * Describe a topic
7
- */
8
- export type Topic<T extends {} = {}> = [
9
- head: QueueNode<T>,
10
- resolve: (() => void) | null,
11
- pending: Promise<void> | null
12
- ];
13
- /**
14
- * Describe a subscriber
15
- */
16
- export type Subscriber<T extends {} = {}> = [
17
- topic: Topic<T>,
18
- tail: QueueNode<T>
19
- ];
20
- /**
21
- * Create a topic
22
- */
23
- export declare const init: <T extends {} = {}>() => Topic<T>;
24
- /**
25
- * Publish a message to the topic
26
- * @param t - The topic
27
- * @param m - The message to publish
28
- */
29
- export declare const publish: <T extends {} = {}>(t: Topic<T>, m: T) => void;
30
- /**
31
- * Resolve all pending dispatch
32
- */
33
- export declare const flush: (t: Topic) => void;
34
- /**
35
- * Subscribe to a topic
36
- * @param t
37
- * @returns A subscriber object
38
- */
39
- export declare const subscribe: <T extends {}>(t: Topic<T>) => Subscriber<T>;
40
- /**
41
- * Wait for messages from the topic
42
- * @param s
43
- */
44
- 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]};