ciorent 0.3.8 → 0.3.9
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 +4 -33
- package/dropping-queue.d.ts +2 -2
- package/fiber.d.ts +0 -12
- package/index.js +1 -1
- package/package.json +5 -7
- package/queue.d.ts +10 -15
- package/semaphore.d.ts +4 -6
- package/semaphore.js +1 -1
- package/sliding-queue.d.ts +3 -3
- package/stream.d.ts +3 -18
- package/stream.js +1 -1
- package/topic.d.ts +2 -4
- package/defer.d.ts +0 -22
- package/defer.js +0 -1
- package/lock.d.ts +0 -33
- package/lock.js +0 -1
package/README.md
CHANGED
@@ -118,7 +118,7 @@ Pubsub allows broadcasting messages to topics that can be recieved by subscriber
|
|
118
118
|
import * as topic from 'ciorent/topic';
|
119
119
|
import * as co from 'ciorent';
|
120
120
|
|
121
|
-
const numbers = topic.init();
|
121
|
+
const numbers = topic.init<number>();
|
122
122
|
|
123
123
|
// Spawn 3 tasks that subscribe to the topic
|
124
124
|
co.spawn(3, async (id) => {
|
@@ -170,14 +170,14 @@ const task = async (id: number) => {
|
|
170
170
|
co.spawn(5, task);
|
171
171
|
```
|
172
172
|
|
173
|
-
###
|
174
|
-
|
173
|
+
### Stream
|
174
|
+
Send and recieve data asynchronously through streams.
|
175
175
|
|
176
176
|
```ts
|
177
177
|
import * as stream from 'ciorent/stream';
|
178
178
|
import * as co from 'ciorent';
|
179
179
|
|
180
|
-
const numbers = stream.init();
|
180
|
+
const numbers = stream.init<number>();
|
181
181
|
|
182
182
|
// Spawn 3 tasks that read from the stream
|
183
183
|
co.spawn(3, async (id) => {
|
@@ -198,35 +198,6 @@ for (let i = 0; i < 3; i++) {
|
|
198
198
|
stream.flush(numbers);
|
199
199
|
```
|
200
200
|
|
201
|
-
### Defer
|
202
|
-
Wait for a value to be resolved.
|
203
|
-
|
204
|
-
```ts
|
205
|
-
import * as defer from 'ciorent/defer';
|
206
|
-
|
207
|
-
const logTime = (label: string) => console.log(`${label}: ${Math.floor(performance.now())}ms`);
|
208
|
-
|
209
|
-
const deferredUrl = defer.init<string>();
|
210
|
-
|
211
|
-
const task = async () => {
|
212
|
-
// Blocks until the defer is resolved
|
213
|
-
const url = await defer.wait(deferredUrl);
|
214
|
-
|
215
|
-
logTime('Start fetching');
|
216
|
-
await fetch(url).catch(() => {});
|
217
|
-
logTime('Done fetching');
|
218
|
-
}
|
219
|
-
|
220
|
-
const prepare = () => {
|
221
|
-
// This always run first as task is waiting
|
222
|
-
logTime('Run before fetch');
|
223
|
-
defer.resolve(deferredUrl, 'http://localhost:3000');
|
224
|
-
}
|
225
|
-
|
226
|
-
task();
|
227
|
-
prepare();
|
228
|
-
```
|
229
|
-
|
230
201
|
### Fibers
|
231
202
|
Virtual threads with more controlled execution.
|
232
203
|
|
package/dropping-queue.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
/**
|
2
2
|
* @module Dropping queues
|
3
3
|
*/
|
4
|
-
import type {
|
4
|
+
import type { FixedQueue } from './queue.js';
|
5
5
|
export { fixed as init } from './queue.js';
|
6
6
|
export { pop } from './sliding-queue.js';
|
7
7
|
/**
|
@@ -9,4 +9,4 @@ export { pop } from './sliding-queue.js';
|
|
9
9
|
* @param q - The queue to push to
|
10
10
|
* @param item
|
11
11
|
*/
|
12
|
-
export declare const push: <T extends {}>(q:
|
12
|
+
export declare const push: <T extends {}>(q: FixedQueue<T>, item: T) => boolean;
|
package/fiber.d.ts
CHANGED
@@ -5,21 +5,9 @@
|
|
5
5
|
* Describe a fiber process
|
6
6
|
*/
|
7
7
|
export type Process<TReturn = unknown> = [
|
8
|
-
/**
|
9
|
-
* The waiting promise
|
10
|
-
*/
|
11
8
|
proc: Promise<TReturn | undefined>,
|
12
|
-
/**
|
13
|
-
* Fiber status
|
14
|
-
*/
|
15
9
|
status: 0 | 1 | 2 | 3,
|
16
|
-
/**
|
17
|
-
* Callback to resume the fiber
|
18
|
-
*/
|
19
10
|
resume: null | ((state: 1 | 3) => void),
|
20
|
-
/**
|
21
|
-
* Bounded fibers
|
22
|
-
*/
|
23
11
|
children: Process[]
|
24
12
|
];
|
25
13
|
/**
|
package/index.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export let nextTick=Promise.resolve();export let sleep=globalThis.Bun?.sleep??globalThis.process?.getBuiltinModule?.("timers/promises").setTimeout??((ms)=>new Promise((res)=>{setTimeout(res,ms)}));let sharedBuf=new Int32Array(new SharedArrayBuffer(4));export let sleepSync=globalThis.Bun?.sleepSync??((ms)=>{Atomics.wait(sharedBuf,0,0,ms)});export let sequential=async(n,task,...args)=>{for(let i=0;i<n;i++)await task(...args,i)};export let spawn=(n,task,...args)=>{let arr=new Array(n);for(let i=0;i<n;i++)arr[i]=task(...args,i);return arr};export let debounce=(f,ms)=>{let id;return(...a)=>{clearTimeout(id);id=setTimeout(f,ms,...a)}};export let throttle=(ms,limit)=>{let head=[null];let tail=head;let promiseCb=(res)=>{head=head[0]=[null,res]};let cur=limit;let scheduled=false;let unlock=()=>{cur=limit;if(tail===head){scheduled=false;return}do{cur--;(tail=tail[0])[1]()}while(cur>0&&tail!==head);setTimeout(unlock,ms)};return()=>{if(cur===0)return new Promise(promiseCb);if(!scheduled){scheduled=true;setTimeout(unlock,ms)}cur
|
1
|
+
export let nextTick=Promise.resolve();export let sleep=globalThis.Bun?.sleep??globalThis.process?.getBuiltinModule?.("timers/promises").setTimeout??((ms)=>new Promise((res)=>{setTimeout(res,ms)}));let sharedBuf=new Int32Array(new SharedArrayBuffer(4));export let sleepSync=globalThis.Bun?.sleepSync??((ms)=>{Atomics.wait(sharedBuf,0,0,ms)});export let sequential=async(n,task,...args)=>{for(let i=0;i<n;i++)await task(...args,i)};export let spawn=(n,task,...args)=>{let arr=new Array(n);for(let i=0;i<n;i++)arr[i]=task(...args,i);return arr};export let debounce=(f,ms)=>{let id;return(...a)=>{clearTimeout(id);id=setTimeout(f,ms,...a)}};export let throttle=(ms,limit)=>{let head=[null];let tail=head;let promiseCb=(res)=>{head=head[0]=[null,res]};let cur=limit;let scheduled=false;let unlock=()=>{cur=limit;if(tail===head){scheduled=false;return}do{cur--;(tail=tail[0])[1]()}while(cur>0&&tail!==head);setTimeout(unlock,ms)};return async()=>{if(cur===0)return new Promise(promiseCb);if(!scheduled){scheduled=true;setTimeout(unlock,ms)}cur--}};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "ciorent",
|
3
|
-
"version": "0.3.
|
3
|
+
"version": "0.3.9",
|
4
4
|
"description": "A lightweight, low-overhead concurrency library",
|
5
5
|
"homepage": "https://ciorent.netlify.app",
|
6
6
|
"repository": {
|
@@ -20,13 +20,11 @@
|
|
20
20
|
"exports": {
|
21
21
|
"./queue": "./queue.js",
|
22
22
|
"./sliding-queue": "./sliding-queue.js",
|
23
|
-
"./dropping-queue": "./dropping-queue.js",
|
24
|
-
"./semaphore": "./semaphore.js",
|
25
|
-
"./lock": "./lock.js",
|
26
|
-
"./defer": "./defer.js",
|
27
23
|
".": "./index.js",
|
28
|
-
"./
|
24
|
+
"./semaphore": "./semaphore.js",
|
29
25
|
"./topic": "./topic.js",
|
30
|
-
"./fiber": "./fiber.js"
|
26
|
+
"./fiber": "./fiber.js",
|
27
|
+
"./stream": "./stream.js",
|
28
|
+
"./dropping-queue": "./dropping-queue.js"
|
31
29
|
}
|
32
30
|
}
|
package/queue.d.ts
CHANGED
@@ -4,30 +4,25 @@
|
|
4
4
|
/**
|
5
5
|
* Describe a fixed-sized queue
|
6
6
|
*/
|
7
|
-
export type
|
8
|
-
/**
|
9
|
-
* Pre-allocated queue
|
10
|
-
*/
|
7
|
+
export type FixedQueue<T extends {} = {}> = [
|
11
8
|
buffer: (T | undefined | null)[],
|
12
|
-
/**
|
13
|
-
* Queue capacity
|
14
|
-
*/
|
15
9
|
capacity: number,
|
16
|
-
/**
|
17
|
-
* Head pointer
|
18
|
-
*/
|
19
10
|
head: number,
|
20
|
-
/**
|
21
|
-
* Tail pointer
|
22
|
-
*/
|
23
11
|
tail: number
|
24
12
|
];
|
25
13
|
/**
|
26
14
|
* Describe a queue node (singly linked list node)
|
27
15
|
*/
|
28
|
-
export type
|
16
|
+
export type QueueNode<T> = [next: QueueNode<T> | null, value: T];
|
17
|
+
/**
|
18
|
+
* Describe an unbounded queue
|
19
|
+
*/
|
20
|
+
export type UnboundedQueue<T> = [
|
21
|
+
head: QueueNode<T>,
|
22
|
+
tail: QueueNode<T>
|
23
|
+
];
|
29
24
|
/**
|
30
25
|
* Create a fixed queue
|
31
26
|
* @param n - The queue size
|
32
27
|
*/
|
33
|
-
export declare const fixed: <T extends {} = {}>(n: number) =>
|
28
|
+
export declare const fixed: <T extends {} = {}>(n: number) => FixedQueue<T>;
|
package/semaphore.d.ts
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
/**
|
2
2
|
* @module Semaphores
|
3
3
|
*/
|
4
|
-
import {
|
4
|
+
import type { UnboundedQueue } from './queue.js';
|
5
5
|
/**
|
6
6
|
* Describe a semaphore
|
7
7
|
*/
|
8
|
-
export type Semaphore =
|
9
|
-
|
10
|
-
* Current remaining process allowed
|
11
|
-
*/
|
8
|
+
export type Semaphore = [
|
9
|
+
...UnboundedQueue<() => void>,
|
12
10
|
remain: number
|
13
|
-
]
|
11
|
+
];
|
14
12
|
/**
|
15
13
|
* Create a semaphore that allows n accesses
|
16
14
|
*/
|
package/semaphore.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
export let init=(n)=>{let root=[null];let sem=[root,root,(res)=>{sem[0]=sem[0][0]=[null,res]},n];return sem};export let acquire=async(s)=>{s[3]--;if(s[3]<0)return new Promise(s[2])};export let release=(s)=>{if(s[3]<0)(s[1]=s[1][0])[1]();s[3]++};export let bind=(f,s)=>async(...a)=>{s[3]--;if(s[3]<0)await new Promise(s[2]);try{return await f(...a)}finally{if(s[3]<0)(s[1]=s[1][0])[1]();s[3]++}};
|
package/sliding-queue.d.ts
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
/**
|
2
2
|
* @module Sliding queues
|
3
3
|
*/
|
4
|
-
import type {
|
4
|
+
import type { FixedQueue } from './queue.js';
|
5
5
|
export { fixed as init } from './queue.js';
|
6
6
|
/**
|
7
7
|
* Push an item to a sliding queue
|
8
8
|
* @param q - The queue to push to
|
9
9
|
* @param item
|
10
10
|
*/
|
11
|
-
export declare const push: <T extends {}>(q:
|
11
|
+
export declare const push: <T extends {}>(q: FixedQueue<T>, item: T) => void;
|
12
12
|
/**
|
13
13
|
* Pop an item from the queue
|
14
14
|
* @param q - The queue to pop from
|
15
15
|
*/
|
16
|
-
export declare const pop: <T extends {}>(q:
|
16
|
+
export declare const pop: <T extends {}>(q: FixedQueue<T>) => T | undefined;
|
package/stream.d.ts
CHANGED
@@ -1,28 +1,13 @@
|
|
1
1
|
/**
|
2
2
|
* @module Streams
|
3
3
|
*/
|
4
|
-
import type {
|
5
|
-
import type { AcquireCallback } from './lock.js';
|
4
|
+
import type { UnboundedQueue } from './queue.js';
|
6
5
|
/**
|
7
6
|
* Describe a stream
|
8
7
|
*/
|
9
8
|
export type Stream<T extends {} = {}> = [
|
10
|
-
|
11
|
-
|
12
|
-
*/
|
13
|
-
head: QueueNode<T | ((val?: T) => void)>,
|
14
|
-
/**
|
15
|
-
* Queue tail
|
16
|
-
*/
|
17
|
-
tail: QueueNode<T | ((val?: T) => void)>,
|
18
|
-
/**
|
19
|
-
* Whether the queue is containing items
|
20
|
-
*/
|
21
|
-
queueing: boolean,
|
22
|
-
/**
|
23
|
-
* Cached callback
|
24
|
-
*/
|
25
|
-
callback: AcquireCallback<T>
|
9
|
+
...UnboundedQueue<T | ((val?: T) => void)>,
|
10
|
+
queueing: boolean
|
26
11
|
];
|
27
12
|
/**
|
28
13
|
* Create a stream
|
package/stream.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export let init=()=>{let queue=[null];let s=[queue,queue,
|
1
|
+
export let init=()=>{let queue=[null];let s=[queue,queue,(res)=>{s[0]=s[0][0]=[null,res]},false];return s};export let write=(s,v)=>{if(!s[3]){if(s[1][0]!==null){(s[1]=s[1][0])[1](v);return}s[3]=true}s[0]=s[0][0]=[null,v]};export let read=async(s)=>{if(s[3]){s[1]=s[1][0];if(s[1][0]===null)s[3]=false;return s[1][1]}return new Promise(s[2])};export let flush=(s)=>{if(!s[3])while(s[1][0]!==null)(s[1]=s[1][0])[1]()};
|
package/topic.d.ts
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
/**
|
2
2
|
* @module Pubsub
|
3
3
|
*/
|
4
|
-
import type {
|
5
|
-
import type { Node as QueueNode } from "./queue.js";
|
4
|
+
import type { QueueNode } from "./queue.js";
|
6
5
|
/**
|
7
6
|
* Describe a topic
|
8
7
|
*/
|
9
8
|
export type Topic<T extends {} = {}> = [
|
10
9
|
head: QueueNode<T>,
|
11
|
-
callback: AcquireCallback<void>,
|
12
10
|
resolve: (() => void) | null,
|
13
11
|
pending: Promise<void> | null
|
14
12
|
];
|
@@ -41,6 +39,6 @@ export declare const flush: (t: Topic) => void;
|
|
41
39
|
export declare const subscribe: <T extends {}>(t: Topic<T>) => Subscriber<T>;
|
42
40
|
/**
|
43
41
|
* Wait for messages from the topic
|
44
|
-
* @param
|
42
|
+
* @param s
|
45
43
|
*/
|
46
44
|
export declare const dispatch: <T extends {}>(s: Subscriber<T>) => Promise<T | undefined>;
|
package/defer.d.ts
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @module Deferred values
|
3
|
-
*/
|
4
|
-
/**
|
5
|
-
* Describe a defer
|
6
|
-
*/
|
7
|
-
export type Defer<T = any> = [
|
8
|
-
wait: Promise<T>,
|
9
|
-
open: (value: T | PromiseLike<T>) => void
|
10
|
-
];
|
11
|
-
/**
|
12
|
-
* Create a latch
|
13
|
-
*/
|
14
|
-
export declare const init: <T>() => Defer<T>;
|
15
|
-
/**
|
16
|
-
* Wait until a deferred is resolved
|
17
|
-
*/
|
18
|
-
export declare const wait: <T>(d: Defer<T>) => Promise<T>;
|
19
|
-
/**
|
20
|
-
* Resolve the defer
|
21
|
-
*/
|
22
|
-
export declare const resolve: (<T extends {}>(d: Defer<T>, p: T | PromiseLike<T>) => void) & ((d: Defer<void>) => void);
|
package/defer.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export let init=()=>{let r;return[new Promise((res)=>{r=res}),r]};export let wait=(d)=>d[0];export let resolve=(d,p)=>{d[1](p)};
|
package/lock.d.ts
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @module Lock utilities
|
3
|
-
*/
|
4
|
-
import type { Node as QueueNode } from './queue.js';
|
5
|
-
/**
|
6
|
-
* Describe a lock
|
7
|
-
*/
|
8
|
-
export type Lock<T = any> = [
|
9
|
-
/**
|
10
|
-
* The head of the Promise resolve queue
|
11
|
-
*/
|
12
|
-
head: QueueNode<(value?: T) => void>,
|
13
|
-
/**
|
14
|
-
* The tail of the Promise resolve queue
|
15
|
-
*/
|
16
|
-
tail: QueueNode<(value?: T) => void>
|
17
|
-
];
|
18
|
-
/**
|
19
|
-
* Release an item
|
20
|
-
* @param lock
|
21
|
-
* @param value
|
22
|
-
*/
|
23
|
-
export declare const release: <T>(lock: Lock<T>, value?: T) => void;
|
24
|
-
/**
|
25
|
-
* Return true if all items are released
|
26
|
-
* @param lock
|
27
|
-
*/
|
28
|
-
export declare const released: (lock: Lock) => boolean;
|
29
|
-
/**
|
30
|
-
* Release all items of a lock
|
31
|
-
* @param lock
|
32
|
-
*/
|
33
|
-
export declare const flush: (lock: Lock) => void;
|
package/lock.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export let release=(lock,value)=>{(lock[1]=lock[1][0])[1](value)};export let released=(lock)=>lock[1][0]===null;export let flush=(lock)=>{while(lock[1][0]!==null)(lock[1]=lock[1][0])[1]()};
|