ciorent 0.0.13 → 0.0.14
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 +60 -55
- package/package.json +1 -1
- package/topic.d.ts +2 -2
- package/topic.js +1 -1
package/README.md
CHANGED
@@ -1,28 +1,4 @@
|
|
1
1
|
A lightweight, low-overhead concurrency library.
|
2
|
-
# Semaphore
|
3
|
-
Semaphore is a concurrency primitive used to control access to a common resource by multiple processes.
|
4
|
-
|
5
|
-
```ts
|
6
|
-
import * as semaphore from 'ciorent/semaphore';
|
7
|
-
import * as cio from 'ciorent';
|
8
|
-
|
9
|
-
// Only allow 2 of these tasks to run concurrently
|
10
|
-
const task = semaphore.task(
|
11
|
-
semaphore.init(2),
|
12
|
-
async (task: number) => {
|
13
|
-
for (let i = 1; i <= 5; i++) {
|
14
|
-
console.log('Task', task, 'iteration', i);
|
15
|
-
await cio.pause;
|
16
|
-
}
|
17
|
-
|
18
|
-
console.log('Task', task, 'end');
|
19
|
-
}
|
20
|
-
);
|
21
|
-
|
22
|
-
// Try to run 6 tasks with 4 tasks running concurrently
|
23
|
-
cio.concurrent(6, task, 4);
|
24
|
-
```
|
25
|
-
|
26
2
|
# Latch
|
27
3
|
Latch is a synchronization primitive that allows one process to wait until another completes an operation before continuing execution.
|
28
4
|
|
@@ -66,6 +42,66 @@ await main();
|
|
66
42
|
await main();
|
67
43
|
```
|
68
44
|
|
45
|
+
# Pubsub
|
46
|
+
A fast, simple publish-subscribe API.
|
47
|
+
|
48
|
+
```ts
|
49
|
+
import * as topic from 'ciorent/topic';
|
50
|
+
import * as cio from 'ciorent';
|
51
|
+
|
52
|
+
const messages = topic.init<number>();
|
53
|
+
|
54
|
+
// A task that publish messages
|
55
|
+
const publisher = async () => {
|
56
|
+
for (let i = 0; i < 5; i++) {
|
57
|
+
await cio.sleep(50);
|
58
|
+
topic.pub(messages, i);
|
59
|
+
}
|
60
|
+
|
61
|
+
// Resolve all waiting promises
|
62
|
+
// And clear the value queue
|
63
|
+
topic.flush(messages);
|
64
|
+
}
|
65
|
+
|
66
|
+
// Spawn 5 tasks that recieve messages
|
67
|
+
cio.concurrent(5, async (id: number) => {
|
68
|
+
const sub = topic.sub(messages);
|
69
|
+
|
70
|
+
while (true) {
|
71
|
+
// Block until
|
72
|
+
const x = await topic.next(sub);
|
73
|
+
if (x == null) break;
|
74
|
+
console.log(`Task ${id}: ${x}`);
|
75
|
+
}
|
76
|
+
});
|
77
|
+
|
78
|
+
publisher();
|
79
|
+
```
|
80
|
+
|
81
|
+
# Semaphore
|
82
|
+
Semaphore is a concurrency primitive used to control access to a common resource by multiple processes.
|
83
|
+
|
84
|
+
```ts
|
85
|
+
import * as semaphore from 'ciorent/semaphore';
|
86
|
+
import * as cio from 'ciorent';
|
87
|
+
|
88
|
+
// Only allow 2 of these tasks to run concurrently
|
89
|
+
const task = semaphore.task(
|
90
|
+
semaphore.init(2),
|
91
|
+
async (task: number) => {
|
92
|
+
for (let i = 1; i <= 5; i++) {
|
93
|
+
console.log('Task', task, 'iteration', i);
|
94
|
+
await cio.pause;
|
95
|
+
}
|
96
|
+
|
97
|
+
console.log('Task', task, 'end');
|
98
|
+
}
|
99
|
+
);
|
100
|
+
|
101
|
+
// Try to run 6 tasks with 4 tasks running concurrently
|
102
|
+
cio.concurrent(6, task, 4);
|
103
|
+
```
|
104
|
+
|
69
105
|
# Channel
|
70
106
|
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.
|
71
107
|
|
@@ -107,37 +143,6 @@ log();
|
|
107
143
|
console.log('Starting...');
|
108
144
|
```
|
109
145
|
|
110
|
-
# Pubsub
|
111
|
-
A simple publish-subscribe API
|
112
|
-
|
113
|
-
```ts
|
114
|
-
import * as topic from 'ciorent/topic';
|
115
|
-
import * as cio from 'ciorent';
|
116
|
-
|
117
|
-
const messages = topic.init<number>();
|
118
|
-
|
119
|
-
// A task that publish messages
|
120
|
-
const publisher = async () => {
|
121
|
-
for (let i = 0; i < 5; i++) {
|
122
|
-
await cio.sleep(50);
|
123
|
-
topic.pub(messages, i);
|
124
|
-
}
|
125
|
-
}
|
126
|
-
|
127
|
-
// Spawn 5 tasks that recieve messages
|
128
|
-
cio.concurrent(5, async (id: number) => {
|
129
|
-
const sub = topic.sub(messages);
|
130
|
-
|
131
|
-
while (true) {
|
132
|
-
const x = await topic.next(sub);
|
133
|
-
if (x == null) break;
|
134
|
-
console.log(`Task ${id}: ${x}`);
|
135
|
-
}
|
136
|
-
});
|
137
|
-
|
138
|
-
publisher();
|
139
|
-
```
|
140
|
-
|
141
146
|
# Utilities
|
142
147
|
## Pausing
|
143
148
|
Delay the execution of a function for other asynchronous tasks to run.
|
package/package.json
CHANGED
package/topic.d.ts
CHANGED
@@ -41,10 +41,10 @@ export declare const sub: <T extends {}>(t: Topic<T>) => Subscriber<T>;
|
|
41
41
|
*/
|
42
42
|
export declare const pub: <T extends {}>(t: Topic<T>, value: T) => void;
|
43
43
|
/**
|
44
|
-
* Resolve all waiting promises
|
44
|
+
* Resolve all waiting promises and clear all pending values
|
45
45
|
* @param t
|
46
46
|
*/
|
47
|
-
export declare const
|
47
|
+
export declare const flush: <T extends {}>(t: Topic<T>) => void;
|
48
48
|
/**
|
49
49
|
* Get the next value in the message queue.
|
50
50
|
*
|
package/topic.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export let init=()=>[[null,null],[],[]];export let sub=(t)=>[t,t[0]];export let pub=(t,value)=>{let head=t[0]=t[0][1]=[value,null];for(let i=0,res=t[1],subs=t[2];i<res.length;i++){res[i](value);subs[i][1]=head}t[1]=[];t[2]=[]};export let
|
1
|
+
export let init=()=>[[null,null],[],[]];export let sub=(t)=>[t,t[0]];export let pub=(t,value)=>{let head=t[0]=t[0][1]=[value,null];for(let i=0,res=t[1],subs=t[2];i<res.length;i++){res[i](value);subs[i][1]=head}t[1]=[];t[2]=[]};export let flush=(t)=>{let head=t[0]=[null,null];for(let i=0,res=t[1],subs=t[2];i<res.length;i++){res[i]();subs[i][1]=head}t[1]=[];t[2]=[]};export let poll=(t)=>t[1][1]!==null?(t[1]=t[1][1])[0]:undefined;export let next=(t)=>{if(t[1][1]!==null)return Promise.resolve((t[1]=t[1][1])[0]);let topic=t[0];topic[2].push(t);return new Promise((res)=>{topic[1].push(res)})};
|