ciorent 0.0.1 → 0.0.2

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.
Files changed (2) hide show
  1. package/README.md +65 -0
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1,2 +1,67 @@
1
1
  # Ciorent
2
2
  A concurrency library.
3
+
4
+ ## Example usage
5
+ Pausing to prioritize an asynchronous task:
6
+ ```ts
7
+ import * as cio from 'ciorent';
8
+
9
+ // Expensive sync task
10
+ const task1 = async () => {
11
+ let x = 0;
12
+ for (let i = 0; i < (Math.random() + 15) * 1e7; i++) {
13
+ // Frequent pausing
14
+ if (i % 2e6 === 0)
15
+ await cio.pause;
16
+
17
+ x += Math.random() * 32 + i * Math.round(Math.random() * 16);
18
+ }
19
+ console.log('Finish task 1:', x);
20
+ };
21
+
22
+ // Short async task
23
+ const task2 = async () => {
24
+ console.log('Fetch start', performance.now().toFixed(2) + 'ms');
25
+ const txt = await fetch('http://example.com');
26
+ console.log('Fetch status', txt.status);
27
+ };
28
+
29
+ // Task 2 will not get blocked by task 1
30
+ task1();
31
+ task2();
32
+ ```
33
+
34
+ Go-like channels for synchronizations:
35
+ ```ts
36
+ import * as channel from 'ciorent/channel';
37
+
38
+ const c = channel.init<number>();
39
+ const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms));
40
+
41
+ const run = async () => {
42
+ for (let i = 0; i < 10; i++) {
43
+ await sleep(10);
44
+ channel.send(c, i);
45
+ console.log('Sent', i);
46
+ }
47
+
48
+ // Send the closed signal to the reciever
49
+ // Or else channel.recieve will block forever
50
+ channel.close(c);
51
+ };
52
+
53
+ const log = async () => {
54
+ do {
55
+ // Non-blocking
56
+ const x = await channel.recieve(c);
57
+ if (x === channel.closed) break;
58
+ console.log('Recieved', x);
59
+ } while (true);
60
+ }
61
+
62
+ run();
63
+ log();
64
+
65
+ // This runs first
66
+ console.log('Starting...');
67
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "A concurrency library",
5
5
  "keywords": [],
6
6
  "license": "MIT",
@@ -21,9 +21,9 @@
21
21
  "eslint": "latest",
22
22
  "eslint-plugin-jsdoc": "latest",
23
23
  "mitata": "latest",
24
+ "terser": "^5.39.0",
24
25
  "tsx": "latest",
25
26
  "typescript": "latest",
26
- "typescript-eslint": "latest",
27
- "uglify-js": "latest"
27
+ "typescript-eslint": "latest"
28
28
  }
29
29
  }