ciorent 0.3.1 → 0.3.3
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 +31 -24
- package/defer.d.ts +22 -0
- package/defer.js +1 -0
- package/package.json +6 -6
- package/latch.d.ts +0 -23
- package/latch.js +0 -1
package/README.md
CHANGED
@@ -79,31 +79,36 @@ Continue the execution on next tick, allowing other asynchronous tasks to run.
|
|
79
79
|
```ts
|
80
80
|
import * as co from 'ciorent';
|
81
81
|
|
82
|
+
const logTime = (label: string) => console.log(label + ':', Math.floor(performance.now()) + 'ms');
|
83
|
+
|
82
84
|
// Expensive sync task
|
83
85
|
const task1 = async () => {
|
84
86
|
let x = 0;
|
85
87
|
|
86
|
-
// Yield control back to the runtime, allowing it to
|
87
|
-
// schedule other tasks
|
88
|
-
await co.nextTick;
|
89
|
-
|
90
88
|
// Simulate heavy operation
|
91
|
-
for (let i = 0
|
89
|
+
for (let i = 0, l = (Math.random() + 15) * 1e6; i < l; i++) {
|
90
|
+
// Yield control back occasionally to the runtime, allowing
|
91
|
+
// it to schedule other tasks
|
92
|
+
if (i % 1e5 === 0)
|
93
|
+
await co.nextTick;
|
94
|
+
|
92
95
|
x += Math.random() * 32 + i * Math.round(Math.random() * 16);
|
96
|
+
}
|
93
97
|
|
94
|
-
console.log('
|
98
|
+
console.log('Task 1 result:', x);
|
95
99
|
};
|
96
100
|
|
97
101
|
// Short async task
|
98
|
-
const task2 = async () => {
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
+
const task2 = async (id: number) => {
|
103
|
+
logTime('Task 2.' + id + ' start fetching');
|
104
|
+
await fetch('http://localhost:3000').catch(() => {});
|
105
|
+
logTime('Task 2.' + id + ' done fetching');
|
102
106
|
};
|
103
107
|
|
104
|
-
// Task 2 will not get blocked by task 1
|
105
108
|
task1();
|
106
|
-
|
109
|
+
|
110
|
+
// Task 2 will not get blocked by task 1
|
111
|
+
co.spawn(5, task2);
|
107
112
|
```
|
108
113
|
|
109
114
|
### Pubsub
|
@@ -171,27 +176,29 @@ const task = async (id: number) => {
|
|
171
176
|
co.spawn(5, task);
|
172
177
|
```
|
173
178
|
|
174
|
-
###
|
175
|
-
|
179
|
+
### Defer
|
180
|
+
Wait for a value to be resolved.
|
176
181
|
|
177
182
|
```ts
|
178
|
-
import * as
|
183
|
+
import * as defer from 'ciorent/defer';
|
184
|
+
|
185
|
+
const logTime = (label: string) => console.log(label + ':', Math.floor(performance.now()) + 'ms');
|
179
186
|
|
180
|
-
const
|
187
|
+
const deferredUrl = defer.init<string>();
|
181
188
|
|
182
189
|
const task = async () => {
|
183
|
-
// Blocks until the
|
184
|
-
await
|
190
|
+
// Blocks until the defer is resolved
|
191
|
+
const url = await defer.wait(deferredUrl);
|
185
192
|
|
186
|
-
|
187
|
-
|
188
|
-
|
193
|
+
logTime('Start fetching');
|
194
|
+
await fetch(url).catch(() => {});
|
195
|
+
logTime('Done fetching');
|
189
196
|
}
|
190
197
|
|
191
198
|
const prepare = () => {
|
192
|
-
// This always run first
|
193
|
-
|
194
|
-
|
199
|
+
// This always run first as task is waiting
|
200
|
+
logTime('Run before fetch');
|
201
|
+
defer.resolve(deferredUrl, 'http://localhost:3000');
|
195
202
|
}
|
196
203
|
|
197
204
|
task();
|
package/defer.d.ts
ADDED
@@ -0,0 +1,22 @@
|
|
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
ADDED
@@ -0,0 +1 @@
|
|
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/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "ciorent",
|
3
|
-
"version": "0.3.
|
3
|
+
"version": "0.3.3",
|
4
4
|
"description": "A lightweight, low-overhead concurrency library",
|
5
5
|
"homepage": "https://ciorent.netlify.app",
|
6
6
|
"repository": {
|
@@ -18,15 +18,15 @@
|
|
18
18
|
"main": "./index.js",
|
19
19
|
"types": "./index.d.ts",
|
20
20
|
"exports": {
|
21
|
-
"./latch": "./latch.js",
|
22
|
-
"./dropping-queue": "./dropping-queue.js",
|
23
21
|
"./sliding-queue": "./sliding-queue.js",
|
22
|
+
"./queue": "./queue.js",
|
23
|
+
"./defer": "./defer.js",
|
24
|
+
"./dropping-queue": "./dropping-queue.js",
|
24
25
|
"./channel": "./channel.js",
|
26
|
+
".": "./index.js",
|
25
27
|
"./fiber": "./fiber.js",
|
26
28
|
"./lock": "./lock.js",
|
27
|
-
"./queue": "./queue.js",
|
28
|
-
"./topic": "./topic.js",
|
29
29
|
"./semaphore": "./semaphore.js",
|
30
|
-
"
|
30
|
+
"./topic": "./topic.js"
|
31
31
|
}
|
32
32
|
}
|
package/latch.d.ts
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @module Latches
|
3
|
-
*/
|
4
|
-
/**
|
5
|
-
* Describe a latch
|
6
|
-
*/
|
7
|
-
export type Latch = [pause: Promise<void>, open: () => void];
|
8
|
-
/**
|
9
|
-
* Create a latch
|
10
|
-
*/
|
11
|
-
export declare const init: () => Latch;
|
12
|
-
/**
|
13
|
-
* Pause until a latch is opened
|
14
|
-
*/
|
15
|
-
export declare const pause: (latch: Latch) => Promise<void>;
|
16
|
-
/**
|
17
|
-
* Open a latch
|
18
|
-
*/
|
19
|
-
export declare const open: (latch: Latch) => void;
|
20
|
-
/**
|
21
|
-
* Close a latch
|
22
|
-
*/
|
23
|
-
export declare const close: (latch: Latch) => void;
|
package/latch.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
import{nextTick 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 close=(latch)=>{if(latch[0]===endPromise){let r;latch[0]=new Promise((res)=>{r=res});latch[1]=r}};
|