ciorent 0.3.9 → 0.4.0

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
@@ -141,6 +141,34 @@ for (let i = 0; i < 3; i++) {
141
141
  topic.flush(numbers);
142
142
  ```
143
143
 
144
+ ### Stream
145
+ Send and recieve data asynchronously through streams.
146
+
147
+ ```ts
148
+ import * as stream from 'ciorent/stream';
149
+ import * as co from 'ciorent';
150
+
151
+ const numbers = stream.init<number>();
152
+
153
+ // Spawn 3 tasks that read from the stream
154
+ co.spawn(3, async (id) => {
155
+ while (true) {
156
+ const msg = await stream.read(numbers);
157
+ if (msg == null) return;
158
+ console.log('Task', id, 'recieved:', msg);
159
+ }
160
+ });
161
+
162
+ // Write messages to the stream
163
+ for (let i = 0; i < 3; i++) {
164
+ stream.write(numbers, i);
165
+ await co.nextTick;
166
+ }
167
+
168
+ // Send undefined to every stream
169
+ stream.flush(numbers);
170
+ ```
171
+
144
172
  ### Semaphore
145
173
  Semaphore is a concurrency primitive used to control access to a common resource by multiple processes.
146
174
 
@@ -170,34 +198,6 @@ const task = async (id: number) => {
170
198
  co.spawn(5, task);
171
199
  ```
172
200
 
173
- ### Stream
174
- Send and recieve data asynchronously through streams.
175
-
176
- ```ts
177
- import * as stream from 'ciorent/stream';
178
- import * as co from 'ciorent';
179
-
180
- const numbers = stream.init<number>();
181
-
182
- // Spawn 3 tasks that read from the stream
183
- co.spawn(3, async (id) => {
184
- while (true) {
185
- const msg = await stream.read(numbers);
186
- if (msg == null) return;
187
- console.log('Task', id, 'recieved:', msg);
188
- }
189
- });
190
-
191
- // Write messages to the stream
192
- for (let i = 0; i < 3; i++) {
193
- stream.write(numbers, i);
194
- await co.nextTick;
195
- }
196
-
197
- // Send undefined to every stream
198
- stream.flush(numbers);
199
- ```
200
-
201
201
  ### Fibers
202
202
  Virtual threads with more controlled execution.
203
203
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.3.9",
3
+ "version": "0.4.0",
4
4
  "description": "A lightweight, low-overhead concurrency library",
5
5
  "homepage": "https://ciorent.netlify.app",
6
6
  "repository": {
@@ -18,13 +18,11 @@
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",
24
- "./semaphore": "./semaphore.js",
25
- "./topic": "./topic.js",
26
21
  "./fiber": "./fiber.js",
22
+ "./queue": "./queue.d.ts",
23
+ "./semaphore": "./semaphore.js",
27
24
  "./stream": "./stream.js",
28
- "./dropping-queue": "./dropping-queue.js"
25
+ "./topic": "./topic.js",
26
+ ".": "./index.js"
29
27
  }
30
28
  }
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
  /**
package/topic.d.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * @module Pubsub
3
3
  */
4
- import type { QueueNode } from "./queue.js";
4
+ import type { PromiseFn, QueueNode } from './queue.js';
5
5
  /**
6
6
  * Describe a topic
7
7
  */
8
8
  export type Topic<T extends {} = {}> = [
9
9
  head: QueueNode<T>,
10
+ callback: PromiseFn<void>,
10
11
  resolve: (() => void) | null,
11
12
  pending: Promise<void> | null
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}};