ciorent 0.0.3 → 0.0.4
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 +58 -8
- package/latch.d.ts +20 -2
- package/latch.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
# Ciorent
|
2
1
|
A low-overhead, cross-runtime concurrency library.
|
3
2
|
|
4
|
-
|
3
|
+
# Usage
|
5
4
|
Pausing to prioritize an asynchronous task:
|
6
5
|
```ts
|
7
6
|
import * as cio from 'ciorent';
|
@@ -77,11 +76,54 @@ log();
|
|
77
76
|
console.log('Starting...');
|
78
77
|
```
|
79
78
|
|
80
|
-
|
79
|
+
## Latch
|
80
|
+
Latch is a synchronization tool that works like a gate, pausing tasks until the latch is opened.
|
81
|
+
|
82
|
+
```ts
|
83
|
+
import * as latch from 'ciorent/latch';
|
84
|
+
import * as cio from 'ciorent';
|
85
|
+
|
86
|
+
const fetchLatch = latch.init();
|
87
|
+
|
88
|
+
const task = async () => {
|
89
|
+
// Blocks until the latch is open
|
90
|
+
await latch.pause(fetchLatch);
|
91
|
+
|
92
|
+
const res = await fetch('http://example.com');
|
93
|
+
console.log('Fetch status:', res.status);
|
94
|
+
}
|
95
|
+
|
96
|
+
const prepare = () => {
|
97
|
+
console.log('Run before fetch:', performance.now().toFixed(2));
|
98
|
+
|
99
|
+
// Unblock the latch
|
100
|
+
latch.open(fetchLatch);
|
101
|
+
}
|
102
|
+
|
103
|
+
const main = async () => {
|
104
|
+
const p = task();
|
105
|
+
await cio.sleep(500);
|
106
|
+
prepare();
|
107
|
+
|
108
|
+
return p;
|
109
|
+
}
|
110
|
+
|
111
|
+
// Run fetch after 500ms
|
112
|
+
await main();
|
113
|
+
|
114
|
+
// Re-close the latch
|
115
|
+
latch.close(fetchLatch);
|
116
|
+
|
117
|
+
// Run fetch after another 500ms
|
118
|
+
await main();
|
119
|
+
```
|
120
|
+
|
121
|
+
If you don't need to close the latch again:
|
81
122
|
```ts
|
82
|
-
import latch from 'ciorent/latch';
|
123
|
+
import * as latch from 'ciorent/latch';
|
124
|
+
import * as cio from 'ciorent';
|
83
125
|
|
84
|
-
const [
|
126
|
+
const [pauseFetch, startFetch] = latch.init();
|
85
127
|
|
86
128
|
const task = async () => {
|
87
129
|
// Blocks until the latch is open
|
@@ -92,12 +134,20 @@ const task = async () => {
|
|
92
134
|
}
|
93
135
|
|
94
136
|
const prepare = () => {
|
95
|
-
console.log('Run before fetch');
|
137
|
+
console.log('Run before fetch:', performance.now().toFixed(2));
|
96
138
|
|
97
139
|
// Unblock the latch
|
98
140
|
startFetch();
|
99
141
|
}
|
100
142
|
|
101
|
-
|
102
|
-
|
143
|
+
const main = async () => {
|
144
|
+
const p = task();
|
145
|
+
await cio.sleep(500);
|
146
|
+
prepare();
|
147
|
+
|
148
|
+
return p;
|
149
|
+
}
|
150
|
+
|
151
|
+
// Run fetch after 500ms
|
152
|
+
await main();
|
103
153
|
```
|
package/latch.d.ts
CHANGED
@@ -2,8 +2,26 @@
|
|
2
2
|
* @module
|
3
3
|
* Latches
|
4
4
|
*/
|
5
|
+
/**
|
6
|
+
* Describe a latch
|
7
|
+
*/
|
8
|
+
export interface Latch {
|
9
|
+
0: Promise<void>;
|
10
|
+
1: () => void;
|
11
|
+
}
|
5
12
|
/**
|
6
13
|
* Create a latch
|
7
14
|
*/
|
8
|
-
declare const
|
9
|
-
|
15
|
+
export declare const init: () => Latch;
|
16
|
+
/**
|
17
|
+
* Pause until a latch is opened
|
18
|
+
*/
|
19
|
+
export declare const pause: (latch: Latch) => Promise<void>;
|
20
|
+
/**
|
21
|
+
* Open a latch
|
22
|
+
*/
|
23
|
+
export declare const open: (latch: Latch) => void;
|
24
|
+
/**
|
25
|
+
* Close a latch
|
26
|
+
*/
|
27
|
+
export declare const close: (latch: Latch) => void;
|
package/latch.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export
|
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 close=(latch)=>{if(latch[0]===endPromise){let r;latch[0]=new Promise((res)=>{r=res});latch[1]=r}};
|