ciorent 0.0.16 → 0.0.17
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 +55 -55
- package/channel.d.ts +1 -1
- package/dropping-queue.d.ts +3 -3
- package/dropping-queue.js +1 -1
- package/latch.js +1 -1
- package/package.json +1 -1
- package/semaphore.d.ts +1 -1
- package/semaphore.js +1 -1
- package/sliding-queue.d.ts +2 -2
- package/sliding-queue.js +1 -1
- package/topic.d.ts +1 -1
package/README.md
CHANGED
@@ -23,47 +23,42 @@ const task = semaphore.task(
|
|
23
23
|
cio.concurrent(6, task);
|
24
24
|
```
|
25
25
|
|
26
|
-
##
|
27
|
-
|
26
|
+
## Channel
|
27
|
+
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.
|
28
28
|
|
29
29
|
```ts
|
30
|
-
import * as
|
30
|
+
import * as channel from 'ciorent/channel';
|
31
31
|
import * as cio from 'ciorent';
|
32
32
|
|
33
|
-
const
|
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
|
-
}
|
33
|
+
const c = channel.init<number>();
|
46
34
|
|
47
|
-
const
|
48
|
-
|
49
|
-
|
50
|
-
|
35
|
+
const run = async () => {
|
36
|
+
for (let i = 0; i < 10; i++) {
|
37
|
+
await cio.sleep(10);
|
38
|
+
channel.send(c, i);
|
39
|
+
console.log('Sent', i);
|
40
|
+
}
|
51
41
|
|
52
|
-
//
|
53
|
-
|
42
|
+
// Resolve all waiting promises with `undefined`
|
43
|
+
// This is a way to tell the reciever to not listen to more data
|
44
|
+
channel.flush(c);
|
45
|
+
};
|
54
46
|
|
55
|
-
|
56
|
-
|
57
|
-
|
47
|
+
const log = async () => {
|
48
|
+
while (true) {
|
49
|
+
// Wait until a value is sent
|
50
|
+
const x = await channel.recieve(c);
|
51
|
+
if (x == null) break;
|
58
52
|
|
59
|
-
|
53
|
+
console.log('Recieved', x);
|
54
|
+
};
|
60
55
|
}
|
61
56
|
|
62
|
-
|
63
|
-
|
57
|
+
run();
|
58
|
+
log();
|
64
59
|
|
65
|
-
//
|
66
|
-
|
60
|
+
// This runs first
|
61
|
+
console.log('Starting...');
|
67
62
|
```
|
68
63
|
|
69
64
|
## Pubsub
|
@@ -102,42 +97,47 @@ cio.concurrent(5, async (id: number) => {
|
|
102
97
|
publisher();
|
103
98
|
```
|
104
99
|
|
105
|
-
##
|
106
|
-
|
100
|
+
## Latch
|
101
|
+
Latch is a synchronization primitive that allows one process to wait until another completes an operation before continuing execution.
|
107
102
|
|
108
103
|
```ts
|
109
|
-
import * as
|
104
|
+
import * as latch from 'ciorent/latch';
|
110
105
|
import * as cio from 'ciorent';
|
111
106
|
|
112
|
-
const
|
107
|
+
const fetchLatch = latch.init();
|
113
108
|
|
114
|
-
const
|
115
|
-
|
116
|
-
|
117
|
-
channel.send(c, i);
|
118
|
-
console.log('Sent', i);
|
119
|
-
}
|
109
|
+
const task = async () => {
|
110
|
+
// Blocks until the latch is open
|
111
|
+
await latch.pause(fetchLatch);
|
120
112
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
};
|
113
|
+
const res = await fetch('http://example.com');
|
114
|
+
console.log('Fetch status:', res.status);
|
115
|
+
}
|
125
116
|
|
126
|
-
const
|
127
|
-
|
128
|
-
|
129
|
-
const x = await channel.recieve(c);
|
130
|
-
if (x == null) break;
|
117
|
+
const prepare = () => {
|
118
|
+
console.log('Run before fetch:', performance.now().toFixed(2));
|
119
|
+
}
|
131
120
|
|
132
|
-
|
133
|
-
|
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
134
|
}
|
135
135
|
|
136
|
-
|
137
|
-
|
136
|
+
// Run fetch after 500ms
|
137
|
+
await main();
|
138
138
|
|
139
|
-
//
|
140
|
-
|
139
|
+
// Run fetch after another 500ms
|
140
|
+
await main();
|
141
141
|
```
|
142
142
|
|
143
143
|
## Utilities
|
package/channel.d.ts
CHANGED
package/dropping-queue.d.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
/**
|
2
2
|
* @module Dropping queues
|
3
3
|
*/
|
4
|
-
import type { FixedQueue } from './fixed-queue';
|
5
|
-
export { init } from './fixed-queue';
|
6
|
-
export { pop } from './sliding-queue';
|
4
|
+
import type { FixedQueue } from './fixed-queue.js';
|
5
|
+
export { init } from './fixed-queue.js';
|
6
|
+
export { pop } from './sliding-queue.js';
|
7
7
|
/**
|
8
8
|
* Push an item to a dropping queue
|
9
9
|
* @param q - The queue to push to
|
package/dropping-queue.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export{init}from"./fixed-queue";export{pop}from"./sliding-queue";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};
|
1
|
+
export{init}from"./fixed-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/latch.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
import{pause as endPromise}from".";export let init=()=>{let r;return[new Promise((res)=>{r=res}),r]};export let pause=(latch)=>latch[0];export let open=(latch)=>{latch[1]();latch[0]=endPromise};export let reset=(latch)=>{if(latch[0]===endPromise){let r;latch[0]=new Promise((res)=>{r=res});latch[1]=r}};
|
1
|
+
import{pause as endPromise}from"./index.js";export let init=()=>{let r;return[new Promise((res)=>{r=res}),r]};export let pause=(latch)=>latch[0];export let open=(latch)=>{latch[1]();latch[0]=endPromise};export let reset=(latch)=>{if(latch[0]===endPromise){let r;latch[0]=new Promise((res)=>{r=res});latch[1]=r}};
|
package/package.json
CHANGED
package/semaphore.d.ts
CHANGED
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 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)};
|
1
|
+
import{pause as resolvedPromise}from"./index.js";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)};
|
package/sliding-queue.d.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
/**
|
2
2
|
* @module Sliding queues
|
3
3
|
*/
|
4
|
-
import type { FixedQueue } from './fixed-queue';
|
5
|
-
export { init } from './fixed-queue';
|
4
|
+
import type { FixedQueue } from './fixed-queue.js';
|
5
|
+
export { init } from './fixed-queue.js';
|
6
6
|
/**
|
7
7
|
* Push an item to a sliding queue
|
8
8
|
* @param q - The queue to push to
|
package/sliding-queue.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export{init}from"./fixed-queue";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}};
|
1
|
+
export{init}from"./fixed-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}};
|