ciorent 0.3.7 → 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 +34 -79
- package/dropping-queue.d.ts +2 -2
- package/fiber.d.ts +6 -18
- package/index.js +1 -1
- package/package.json +5 -7
- package/queue.d.ts +15 -23
- package/semaphore.d.ts +5 -7
- package/semaphore.js +1 -1
- package/sliding-queue.d.ts +3 -3
- package/stream.d.ts +30 -0
- package/stream.js +1 -0
- package/topic.d.ts +25 -38
- package/topic.js +1 -1
- package/channel.d.ts +0 -39
- package/channel.js +0 -1
- 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
@@ -32,7 +32,7 @@ Cross-runtime synchronous and asynchronous sleep functions.
|
|
32
32
|
```ts
|
33
33
|
import * as co from 'ciorent';
|
34
34
|
|
35
|
-
const logTime = (label: string) => console.log(`${label}: ${Math.floor(performance.now())}ms`)
|
35
|
+
const logTime = (label: string) => console.log(`${label}: ${Math.floor(performance.now())}ms`);
|
36
36
|
|
37
37
|
logTime('Start');
|
38
38
|
|
@@ -112,39 +112,33 @@ co.spawn(5, task2);
|
|
112
112
|
```
|
113
113
|
|
114
114
|
### Pubsub
|
115
|
-
|
115
|
+
Pubsub allows broadcasting messages to topics that can be recieved by subscribers.
|
116
116
|
|
117
117
|
```ts
|
118
118
|
import * as topic from 'ciorent/topic';
|
119
119
|
import * as co from 'ciorent';
|
120
120
|
|
121
|
-
const
|
122
|
-
|
123
|
-
// A task that publish messages
|
124
|
-
const publisher = async () => {
|
125
|
-
for (let i = 0; i < 3; i++) {
|
126
|
-
await co.sleep(100);
|
127
|
-
topic.publish(messages, i);
|
128
|
-
}
|
121
|
+
const numbers = topic.init<number>();
|
129
122
|
|
130
|
-
|
131
|
-
|
132
|
-
topic.
|
133
|
-
}
|
134
|
-
|
135
|
-
// Spawn 3 tasks that recieve messages
|
136
|
-
co.spawn(3, async (id: number) => {
|
137
|
-
const sub = topic.subscribe(messages);
|
123
|
+
// Spawn 3 tasks that subscribe to the topic
|
124
|
+
co.spawn(3, async (id) => {
|
125
|
+
const subscriber = topic.subscribe(numbers);
|
138
126
|
|
139
127
|
while (true) {
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
console.log(`Task ${id} recieved: ${x}`);
|
128
|
+
const msg = await topic.dispatch(subscriber);
|
129
|
+
if (msg == null) return;
|
130
|
+
console.log('Task', id, 'recieved:', msg);
|
144
131
|
}
|
145
132
|
});
|
146
133
|
|
147
|
-
|
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);
|
148
142
|
```
|
149
143
|
|
150
144
|
### Semaphore
|
@@ -176,71 +170,32 @@ const task = async (id: number) => {
|
|
176
170
|
co.spawn(5, task);
|
177
171
|
```
|
178
172
|
|
179
|
-
###
|
180
|
-
|
173
|
+
### Stream
|
174
|
+
Send and recieve data asynchronously through streams.
|
181
175
|
|
182
176
|
```ts
|
183
|
-
import * as
|
184
|
-
|
185
|
-
const logTime = (label: string) => console.log(`${label}: ${Math.floor(performance.now())}ms`);
|
186
|
-
|
187
|
-
const deferredUrl = defer.init<string>();
|
188
|
-
|
189
|
-
const task = async () => {
|
190
|
-
// Blocks until the defer is resolved
|
191
|
-
const url = await defer.wait(deferredUrl);
|
192
|
-
|
193
|
-
logTime('Start fetching');
|
194
|
-
await fetch(url).catch(() => {});
|
195
|
-
logTime('Done fetching');
|
196
|
-
}
|
197
|
-
|
198
|
-
const prepare = () => {
|
199
|
-
// This always run first as task is waiting
|
200
|
-
logTime('Run before fetch');
|
201
|
-
defer.resolve(deferredUrl, 'http://localhost:3000');
|
202
|
-
}
|
203
|
-
|
204
|
-
task();
|
205
|
-
prepare();
|
206
|
-
```
|
207
|
-
|
208
|
-
### Channel
|
209
|
-
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.
|
210
|
-
|
211
|
-
```ts
|
212
|
-
import * as channel from 'ciorent/channel';
|
177
|
+
import * as stream from 'ciorent/stream';
|
213
178
|
import * as co from 'ciorent';
|
214
179
|
|
215
|
-
const
|
180
|
+
const numbers = stream.init<number>();
|
216
181
|
|
217
|
-
|
218
|
-
|
219
|
-
await co.sleep(100);
|
220
|
-
channel.send(chan, i);
|
221
|
-
console.log('Sent', i);
|
222
|
-
}
|
223
|
-
|
224
|
-
// Resolve all waiting promises with `undefined`
|
225
|
-
// This is a way to tell the reciever to not listen to more data
|
226
|
-
channel.flush(chan);
|
227
|
-
};
|
228
|
-
|
229
|
-
const log = async () => {
|
182
|
+
// Spawn 3 tasks that read from the stream
|
183
|
+
co.spawn(3, async (id) => {
|
230
184
|
while (true) {
|
231
|
-
|
232
|
-
|
233
|
-
|
185
|
+
const msg = await stream.read(numbers);
|
186
|
+
if (msg == null) return;
|
187
|
+
console.log('Task', id, 'recieved:', msg);
|
188
|
+
}
|
189
|
+
});
|
234
190
|
|
235
|
-
|
236
|
-
|
191
|
+
// Write messages to the stream
|
192
|
+
for (let i = 0; i < 3; i++) {
|
193
|
+
stream.write(numbers, i);
|
194
|
+
await co.nextTick;
|
237
195
|
}
|
238
196
|
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
// This runs first
|
243
|
-
console.log('Starting...');
|
197
|
+
// Send undefined to every stream
|
198
|
+
stream.flush(numbers);
|
244
199
|
```
|
245
200
|
|
246
201
|
### Fibers
|
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
@@ -4,24 +4,12 @@
|
|
4
4
|
/**
|
5
5
|
* Describe a fiber process
|
6
6
|
*/
|
7
|
-
export
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
* Fiber status
|
14
|
-
*/
|
15
|
-
1: 0 | 1 | 2 | 3;
|
16
|
-
/**
|
17
|
-
* Callback to continue running the fiber
|
18
|
-
*/
|
19
|
-
2: null | ((state: 1 | 3) => void);
|
20
|
-
/**
|
21
|
-
* Bounded fibers
|
22
|
-
*/
|
23
|
-
3: Process[];
|
24
|
-
}
|
7
|
+
export type Process<TReturn = unknown> = [
|
8
|
+
proc: Promise<TReturn | undefined>,
|
9
|
+
status: 0 | 1 | 2 | 3,
|
10
|
+
resume: null | ((state: 1 | 3) => void),
|
11
|
+
children: Process[]
|
12
|
+
];
|
25
13
|
/**
|
26
14
|
* Describe a fiber runtime
|
27
15
|
*/
|
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": {
|
@@ -19,14 +19,12 @@
|
|
19
19
|
"types": "./index.d.ts",
|
20
20
|
"exports": {
|
21
21
|
"./queue": "./queue.js",
|
22
|
-
"./channel": "./channel.js",
|
23
22
|
"./sliding-queue": "./sliding-queue.js",
|
24
|
-
"./topic": "./topic.js",
|
25
|
-
"./lock": "./lock.js",
|
26
23
|
".": "./index.js",
|
27
24
|
"./semaphore": "./semaphore.js",
|
28
|
-
"./
|
29
|
-
"./
|
30
|
-
"./
|
25
|
+
"./topic": "./topic.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,33 +4,25 @@
|
|
4
4
|
/**
|
5
5
|
* Describe a fixed-sized queue
|
6
6
|
*/
|
7
|
-
export
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
* Queue capacity
|
14
|
-
*/
|
15
|
-
readonly 1: number;
|
16
|
-
/**
|
17
|
-
* Head pointer
|
18
|
-
*/
|
19
|
-
2: number;
|
20
|
-
/**
|
21
|
-
* Tail pointer
|
22
|
-
*/
|
23
|
-
3: number;
|
24
|
-
}
|
7
|
+
export type FixedQueue<T extends {} = {}> = [
|
8
|
+
buffer: (T | undefined | null)[],
|
9
|
+
capacity: number,
|
10
|
+
head: number,
|
11
|
+
tail: number
|
12
|
+
];
|
25
13
|
/**
|
26
14
|
* Describe a queue node (singly linked list node)
|
27
15
|
*/
|
28
|
-
export
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
+
];
|
32
24
|
/**
|
33
25
|
* Create a fixed queue
|
34
26
|
* @param n - The queue size
|
35
27
|
*/
|
36
|
-
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
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
3: number;
|
13
|
-
}
|
8
|
+
export type Semaphore = [
|
9
|
+
...UnboundedQueue<() => void>,
|
10
|
+
remain: number
|
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
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
/**
|
2
|
+
* @module Streams
|
3
|
+
*/
|
4
|
+
import type { UnboundedQueue } from './queue.js';
|
5
|
+
/**
|
6
|
+
* Describe a stream
|
7
|
+
*/
|
8
|
+
export type Stream<T extends {} = {}> = [
|
9
|
+
...UnboundedQueue<T | ((val?: T) => void)>,
|
10
|
+
queueing: boolean
|
11
|
+
];
|
12
|
+
/**
|
13
|
+
* Create a stream
|
14
|
+
*/
|
15
|
+
export declare const init: <T extends {} = {}>() => Stream<T>;
|
16
|
+
/**
|
17
|
+
* Write a value to the stream
|
18
|
+
* @param s
|
19
|
+
* @param v
|
20
|
+
*/
|
21
|
+
export declare const write: <T extends {} = {}>(s: Stream<T>, v: T) => void;
|
22
|
+
/**
|
23
|
+
* Read a value from the stream
|
24
|
+
* @param s
|
25
|
+
*/
|
26
|
+
export declare const read: <T extends {} = {}>(s: Stream<T>) => Promise<T | undefined>;
|
27
|
+
/**
|
28
|
+
* Release all pending read with undefined
|
29
|
+
*/
|
30
|
+
export declare const flush: (s: Stream) => void;
|
package/stream.js
ADDED
@@ -0,0 +1 @@
|
|
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,57 +1,44 @@
|
|
1
1
|
/**
|
2
2
|
* @module Pubsub
|
3
3
|
*/
|
4
|
-
import type {
|
4
|
+
import type { QueueNode } from "./queue.js";
|
5
5
|
/**
|
6
6
|
* Describe a topic
|
7
7
|
*/
|
8
|
-
export
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
/**
|
14
|
-
* The waiting subscriber resolves
|
15
|
-
*/
|
16
|
-
1: ((res: QueueNode<T>) => void)[];
|
17
|
-
}
|
8
|
+
export type Topic<T extends {} = {}> = [
|
9
|
+
head: QueueNode<T>,
|
10
|
+
resolve: (() => void) | null,
|
11
|
+
pending: Promise<void> | null
|
12
|
+
];
|
18
13
|
/**
|
19
|
-
*
|
14
|
+
* Describe a subscriber
|
20
15
|
*/
|
21
|
-
export
|
16
|
+
export type Subscriber<T extends {} = {}> = [
|
17
|
+
topic: Topic<T>,
|
18
|
+
tail: QueueNode<T>
|
19
|
+
];
|
22
20
|
/**
|
23
|
-
*
|
21
|
+
* Create a topic
|
24
22
|
*/
|
25
|
-
export
|
26
|
-
0: Topic<T>;
|
27
|
-
1: QueueNode<T>;
|
28
|
-
}
|
23
|
+
export declare const init: <T extends {} = {}>() => Topic<T>;
|
29
24
|
/**
|
30
|
-
*
|
31
|
-
* @param t
|
25
|
+
* Publish a message to the topic
|
26
|
+
* @param t - The topic
|
27
|
+
* @param m - The message to publish
|
32
28
|
*/
|
33
|
-
export declare const
|
29
|
+
export declare const publish: <T extends {} = {}>(t: Topic<T>, m: T) => void;
|
34
30
|
/**
|
35
|
-
*
|
36
|
-
* @param t
|
31
|
+
* Resolve all pending dispatch
|
37
32
|
*/
|
38
|
-
export declare const
|
33
|
+
export declare const flush: (t: Topic) => void;
|
39
34
|
/**
|
40
|
-
*
|
41
|
-
* @param t
|
42
|
-
*/
|
43
|
-
export declare const flush: <T extends {}>(t: Topic<T>) => void;
|
44
|
-
/**
|
45
|
-
* Get the next value in the message queue.
|
46
|
-
*
|
47
|
-
* Returns `undefined` if the message queue is empty
|
35
|
+
* Subscribe to a topic
|
48
36
|
* @param t
|
37
|
+
* @returns A subscriber object
|
49
38
|
*/
|
50
|
-
export declare const
|
39
|
+
export declare const subscribe: <T extends {}>(t: Topic<T>) => Subscriber<T>;
|
51
40
|
/**
|
52
|
-
*
|
53
|
-
*
|
54
|
-
* Returns a promise that resolves when the message queue is not empty
|
55
|
-
* @param t
|
41
|
+
* Wait for messages from the topic
|
42
|
+
* @param s
|
56
43
|
*/
|
57
|
-
export declare const dispatch: <T extends {}>(
|
44
|
+
export declare const dispatch: <T extends {}>(s: Subscriber<T>) => Promise<T | undefined>;
|
package/topic.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export let init=()=>{let t=[[null],
|
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]};
|
package/channel.d.ts
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @module Channels
|
3
|
-
*/
|
4
|
-
import type { Node as QueueNode } from './queue.js';
|
5
|
-
import { type Lock } from './lock.js';
|
6
|
-
/**
|
7
|
-
* Describe a channel
|
8
|
-
*/
|
9
|
-
export interface Channel<T = any> extends Lock<T> {
|
10
|
-
/**
|
11
|
-
* The head of the value queue
|
12
|
-
*/
|
13
|
-
3: QueueNode<T>;
|
14
|
-
/**
|
15
|
-
* The tail of the value queue
|
16
|
-
*/
|
17
|
-
4: QueueNode<T>;
|
18
|
-
}
|
19
|
-
/**
|
20
|
-
* Create a channel
|
21
|
-
*/
|
22
|
-
export declare const init: <T extends {}>() => Channel<T>;
|
23
|
-
/**
|
24
|
-
* Send a message to a channel
|
25
|
-
* @param c - The channel to send to
|
26
|
-
* @param t - The message to send
|
27
|
-
*/
|
28
|
-
export declare const send: <T>(c: Channel<T>, t: T) => void;
|
29
|
-
/**
|
30
|
-
* Recieve a message from a channel, return null if the channel is closed
|
31
|
-
* @param c
|
32
|
-
*/
|
33
|
-
export declare const recieve: <T>(c: Channel<T>) => Promise<T | undefined>;
|
34
|
-
/**
|
35
|
-
* Recieve a message from a channel, return undefined if no message is currently in queue
|
36
|
-
* @param c
|
37
|
-
*/
|
38
|
-
export declare const poll: <T>(c: Channel<T>) => T | undefined;
|
39
|
-
export { flush } from './lock.js';
|
package/channel.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
import{release as lockRelease,released as lockReleased}from"./lock.js";export let init=()=>{let resolveQu=[null];let qu=[null];let chan=[resolveQu,resolveQu,(res)=>{chan[0]=chan[0][0]=[null,res]},qu,qu];return chan};export let send=(c,t)=>{if(lockReleased(c))c[3]=c[3][0]=[null,t];else lockRelease(c,t)};export let recieve=(c)=>c[4][0]!==null?Promise.resolve((c[4]=c[4][0])[1]):new Promise(c[2]);export let poll=(c)=>c[4][0]!==null?(c[4]=c[4][0])[1]:undefined;export{flush}from"./lock.js";
|
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 interface Lock<T = any> {
|
9
|
-
/**
|
10
|
-
* The head of the Promise resolve queue
|
11
|
-
*/
|
12
|
-
0: QueueNode<(value?: T) => void>;
|
13
|
-
/**
|
14
|
-
* The tail of the Promise resolve queue
|
15
|
-
*/
|
16
|
-
1: 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]()};
|