ciorent 0.0.15 → 0.0.16
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 +47 -47
- package/package.json +1 -1
- package/semaphore.d.ts +4 -0
- package/semaphore.js +1 -1
package/README.md
CHANGED
@@ -19,8 +19,51 @@ const task = semaphore.task(
|
|
19
19
|
}
|
20
20
|
);
|
21
21
|
|
22
|
-
// Try to run 6 tasks
|
23
|
-
cio.concurrent(6, task
|
22
|
+
// Try to run 6 tasks concurrently
|
23
|
+
cio.concurrent(6, task);
|
24
|
+
```
|
25
|
+
|
26
|
+
## Latch
|
27
|
+
Latch is a synchronization primitive that allows one process to wait until another completes an operation before continuing execution.
|
28
|
+
|
29
|
+
```ts
|
30
|
+
import * as latch from 'ciorent/latch';
|
31
|
+
import * as cio from 'ciorent';
|
32
|
+
|
33
|
+
const fetchLatch = latch.init();
|
34
|
+
|
35
|
+
const task = async () => {
|
36
|
+
// Blocks until the latch is open
|
37
|
+
await latch.pause(fetchLatch);
|
38
|
+
|
39
|
+
const res = await fetch('http://example.com');
|
40
|
+
console.log('Fetch status:', res.status);
|
41
|
+
}
|
42
|
+
|
43
|
+
const prepare = () => {
|
44
|
+
console.log('Run before fetch:', performance.now().toFixed(2));
|
45
|
+
}
|
46
|
+
|
47
|
+
const main = async () => {
|
48
|
+
const p = task();
|
49
|
+
await cio.sleep(500);
|
50
|
+
prepare();
|
51
|
+
|
52
|
+
// Allows all previously blocked tasks to run
|
53
|
+
latch.open(fetchLatch);
|
54
|
+
|
55
|
+
// Reclose the latch
|
56
|
+
// Tasks that aren't blocked yet will be blocked
|
57
|
+
latch.reset(fetchLatch);
|
58
|
+
|
59
|
+
return p;
|
60
|
+
}
|
61
|
+
|
62
|
+
// Run fetch after 500ms
|
63
|
+
await main();
|
64
|
+
|
65
|
+
// Run fetch after another 500ms
|
66
|
+
await main();
|
24
67
|
```
|
25
68
|
|
26
69
|
## Pubsub
|
@@ -82,7 +125,7 @@ const run = async () => {
|
|
82
125
|
|
83
126
|
const log = async () => {
|
84
127
|
while (true) {
|
85
|
-
//
|
128
|
+
// Wait until a value is sent
|
86
129
|
const x = await channel.recieve(c);
|
87
130
|
if (x == null) break;
|
88
131
|
|
@@ -97,49 +140,6 @@ log();
|
|
97
140
|
console.log('Starting...');
|
98
141
|
```
|
99
142
|
|
100
|
-
## Latch
|
101
|
-
Latch is a synchronization primitive that allows one process to wait until another completes an operation before continuing execution.
|
102
|
-
|
103
|
-
```ts
|
104
|
-
import * as latch from 'ciorent/latch';
|
105
|
-
import * as cio from 'ciorent';
|
106
|
-
|
107
|
-
const fetchLatch = latch.init();
|
108
|
-
|
109
|
-
const task = async () => {
|
110
|
-
// Blocks until the latch is open
|
111
|
-
await latch.pause(fetchLatch);
|
112
|
-
|
113
|
-
const res = await fetch('http://example.com');
|
114
|
-
console.log('Fetch status:', res.status);
|
115
|
-
}
|
116
|
-
|
117
|
-
const prepare = () => {
|
118
|
-
console.log('Run before fetch:', performance.now().toFixed(2));
|
119
|
-
}
|
120
|
-
|
121
|
-
const main = async () => {
|
122
|
-
const p = task();
|
123
|
-
await cio.sleep(500);
|
124
|
-
prepare();
|
125
|
-
|
126
|
-
// Allows all previously blocked tasks to run
|
127
|
-
latch.open(fetchLatch);
|
128
|
-
|
129
|
-
// Reclose the latch
|
130
|
-
// Tasks that aren't blocked yet will be blocked
|
131
|
-
latch.reset(fetchLatch);
|
132
|
-
|
133
|
-
return p;
|
134
|
-
}
|
135
|
-
|
136
|
-
// Run fetch after 500ms
|
137
|
-
await main();
|
138
|
-
|
139
|
-
// Run fetch after another 500ms
|
140
|
-
await main();
|
141
|
-
```
|
142
|
-
|
143
143
|
## Utilities
|
144
144
|
### Pausing
|
145
145
|
Delay the execution of a function for other asynchronous tasks to run.
|
@@ -181,7 +181,7 @@ console.log('Hi');
|
|
181
181
|
```
|
182
182
|
|
183
183
|
### Spawning tasks
|
184
|
-
|
184
|
+
Utilities to create and run tasks.
|
185
185
|
```ts
|
186
186
|
import * as cio from 'ciorent';
|
187
187
|
|
package/package.json
CHANGED
package/semaphore.d.ts
CHANGED
@@ -31,6 +31,10 @@ export declare const pause: (s: Semaphore) => Promise<void>;
|
|
31
31
|
* Signal to the semaphore to release access
|
32
32
|
*/
|
33
33
|
export declare const signal: (s: Semaphore) => void;
|
34
|
+
/**
|
35
|
+
* Wrap a task to bind to a custom semaphore later
|
36
|
+
*/
|
37
|
+
export declare const wrap: <Args extends any[], Return extends Promise<any>>(f: (...args: Args) => Return) => (s: Semaphore, ...a: Args) => Return;
|
34
38
|
/**
|
35
39
|
* Create a task that acquire a semaphore and release the access when it's finished
|
36
40
|
*/
|
package/semaphore.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
import{pause as resolvedPromise}from".";export let init=(n)=>{let root=[null,null];return[n,root,root]};export let pause=(s)=>{s[0]--;if(s[0]<0){let r;let p=new Promise((res)=>{r=res});s[1]=s[1][1]=[r,null];return p}return resolvedPromise};export let signal=(s)=>{if(s[0]<0)(s[2]=s[2][1])[0]();s[0]++};export let
|
1
|
+
import{pause as resolvedPromise}from".";export let init=(n)=>{let root=[null,null];return[n,root,root]};export let pause=(s)=>{s[0]--;if(s[0]<0){let r;let p=new Promise((res)=>{r=res});s[1]=s[1][1]=[r,null];return p}return resolvedPromise};export let signal=(s)=>{if(s[0]<0)(s[2]=s[2][1])[0]();s[0]++};export let wrap=(f)=>async(s,...a)=>{s[0]--;if(s[0]<0){let r;let p=new Promise((res)=>{r=res});s[1]=s[1][1]=[r,null];await p}try{return await f(...a)}finally{signal(s)}};export let task=(s,f)=>{f=wrap(f);return(...a)=>f(s,...a)};
|