ciorent 0.0.9 → 0.0.12
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 -30
- package/channel.d.ts +6 -7
- package/channel.js +1 -1
- package/concurrent.d.ts +1 -2
- package/{queue/dropping.d.ts → dropping-queue.d.ts} +4 -5
- package/dropping-queue.js +1 -0
- package/{queue/index.d.ts → fixed-queue.d.ts} +1 -2
- package/index.d.ts +1 -2
- package/latch.d.ts +1 -2
- package/package.json +7 -5
- package/semaphore.d.ts +2 -3
- package/{queue/sliding.d.ts → sliding-queue.d.ts} +3 -4
- package/sliding-queue.js +1 -0
- package/queue/dropping.js +0 -1
- package/queue/sliding.js +0 -1
- /package/{queue/index.js → fixed-queue.js} +0 -0
package/README.md
CHANGED
@@ -1,42 +1,18 @@
|
|
1
|
-
A low-overhead
|
1
|
+
A lightweight, low-overhead concurrency library
|
2
2
|
|
3
3
|
# Examples
|
4
|
-
## Semaphore
|
5
|
-
Semaphore is a concurrency primitive used to control access to a common resource by multiple processes.
|
6
|
-
|
7
|
-
```ts
|
8
|
-
import * as semaphore from 'ciorent/semaphore';
|
9
|
-
import * as cio from 'ciorent';
|
10
|
-
|
11
|
-
// Only allow 2 of these tasks to run concurrently
|
12
|
-
const task = semaphore.task(
|
13
|
-
semaphore.init(2),
|
14
|
-
async (task: number) => {
|
15
|
-
for (let i = 1; i <= 5; i++) {
|
16
|
-
console.log('Task', task, 'iteration', i);
|
17
|
-
await cio.pause;
|
18
|
-
}
|
19
|
-
|
20
|
-
console.log('Task', task, 'end');
|
21
|
-
}
|
22
|
-
);
|
23
|
-
|
24
|
-
// Try to run 6 tasks with 4 tasks running concurrently
|
25
|
-
cio.concurrent(6, task, 4);
|
26
|
-
```
|
27
|
-
|
28
4
|
## Channel
|
29
5
|
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.
|
30
6
|
|
31
7
|
```ts
|
32
8
|
import * as channel from 'ciorent/channel';
|
9
|
+
import * as cio from 'ciorent';
|
33
10
|
|
34
11
|
const c = channel.init<number>();
|
35
|
-
const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms));
|
36
12
|
|
37
13
|
const run = async () => {
|
38
14
|
for (let i = 0; i < 10; i++) {
|
39
|
-
await sleep(10);
|
15
|
+
await cio.sleep(10);
|
40
16
|
channel.send(c, i);
|
41
17
|
console.log('Sent', i);
|
42
18
|
}
|
@@ -51,8 +27,9 @@ const log = async () => {
|
|
51
27
|
// Non-blocking
|
52
28
|
const x = await channel.recieve(c);
|
53
29
|
|
54
|
-
//
|
55
|
-
|
30
|
+
// 'recieve' returns undefined if
|
31
|
+
// The channel has been closed
|
32
|
+
if (x == null) break;
|
56
33
|
|
57
34
|
console.log('Recieved', x);
|
58
35
|
};
|
@@ -65,6 +42,30 @@ log();
|
|
65
42
|
console.log('Starting...');
|
66
43
|
```
|
67
44
|
|
45
|
+
## Semaphore
|
46
|
+
Semaphore is a concurrency primitive used to control access to a common resource by multiple processes.
|
47
|
+
|
48
|
+
```ts
|
49
|
+
import * as semaphore from 'ciorent/semaphore';
|
50
|
+
import * as cio from 'ciorent';
|
51
|
+
|
52
|
+
// Only allow 2 of these tasks to run concurrently
|
53
|
+
const task = semaphore.task(
|
54
|
+
semaphore.init(2),
|
55
|
+
async (task: number) => {
|
56
|
+
for (let i = 1; i <= 5; i++) {
|
57
|
+
console.log('Task', task, 'iteration', i);
|
58
|
+
await cio.pause;
|
59
|
+
}
|
60
|
+
|
61
|
+
console.log('Task', task, 'end');
|
62
|
+
}
|
63
|
+
);
|
64
|
+
|
65
|
+
// Try to run 6 tasks with 4 tasks running concurrently
|
66
|
+
cio.concurrent(6, task, 4);
|
67
|
+
```
|
68
|
+
|
68
69
|
## Latch
|
69
70
|
Latch is a synchronization primitive that allows one process to wait until another completes an operation before continuing execution.
|
70
71
|
|
@@ -179,7 +180,7 @@ console.log('Running sequentially:');
|
|
179
180
|
cio.sequential(5, task);
|
180
181
|
|
181
182
|
// Spawn and run 5 tasks concurrently
|
182
|
-
console.log('Running concurrently:');
|
183
|
+
console.log('Running 5 tasks concurrently:');
|
183
184
|
cio.concurrent(5, task);
|
184
185
|
|
185
186
|
// Spawn and run 5 tasks, with the maximum
|
package/channel.d.ts
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
/**
|
2
|
-
* @module
|
3
|
-
* Channels
|
2
|
+
* @module Channels
|
4
3
|
*/
|
5
|
-
import type { QueueNode } from './queue';
|
4
|
+
import type { QueueNode } from './fixed-queue';
|
6
5
|
/**
|
7
6
|
* Describe a channel
|
8
7
|
*/
|
@@ -22,11 +21,11 @@ export interface Channel<T> {
|
|
22
21
|
/**
|
23
22
|
* The head of the Promise resolve queue
|
24
23
|
*/
|
25
|
-
3: QueueNode<(value
|
24
|
+
3: QueueNode<(value?: T) => void>;
|
26
25
|
/**
|
27
26
|
* The tail of the Promise resolve queue
|
28
27
|
*/
|
29
|
-
4: QueueNode<(value
|
28
|
+
4: QueueNode<(value?: T) => void>;
|
30
29
|
}
|
31
30
|
/**
|
32
31
|
* Create a channel
|
@@ -42,12 +41,12 @@ export declare const send: <T>(c: Channel<T>, t: T) => void;
|
|
42
41
|
* Recieve a message from a channel, return null if the channel is closed
|
43
42
|
* @param c
|
44
43
|
*/
|
45
|
-
export declare const recieve: <T>(c: Channel<T>) => Promise<T |
|
44
|
+
export declare const recieve: <T>(c: Channel<T>) => Promise<T | undefined>;
|
46
45
|
/**
|
47
46
|
* Recieve a message from a channel, return null if no message is currently in queue
|
48
47
|
* @param c
|
49
48
|
*/
|
50
|
-
export declare const poll: <T>(c: Channel<T>) => T |
|
49
|
+
export declare const poll: <T>(c: Channel<T>) => T | undefined;
|
51
50
|
/**
|
52
51
|
* Close a channel
|
53
52
|
* @param c
|
package/channel.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
import{pause as resolvedPromise}from".";export let init=()=>{let qu=[null,null];let resolveQu=[null,null];return[true,qu,qu,resolveQu,resolveQu]};export let send=(c,t)=>{if(c[0]){if(c[4][1]!==null)(c[4]=c[4][1])[0](t);else c[1]=c[1][1]=[t,null]}};export let recieve=(c)=>c[2][1]!==null?Promise.resolve((c[2]=c[2][1])[0]):c[0]?new Promise((res)=>{c[3]=c[3][1]=[res,null]}):resolvedPromise;export let poll=(c)=>c[2][1]!==null?(c[2]=c[2][1])[0]:undefined;export let close=(c)=>{c[0]=false;while(c[4][1]!==null)(c[4]=c[4][1])[0]()};export let active=(c)=>c[0];
|
package/concurrent.d.ts
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
/**
|
2
|
-
* @module
|
3
|
-
* Dropping queues
|
2
|
+
* @module Dropping queues
|
4
3
|
*/
|
5
|
-
import type { FixedQueue } from '
|
6
|
-
export { default as init } from '
|
7
|
-
export { pop } from './sliding';
|
4
|
+
import type { FixedQueue } from './fixed-queue';
|
5
|
+
export { default as init } from './fixed-queue';
|
6
|
+
export { pop } from './sliding-queue';
|
8
7
|
/**
|
9
8
|
* Push an item to a dropping queue
|
10
9
|
* @param q - The queue to push to
|
@@ -0,0 +1 @@
|
|
1
|
+
export{default as 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};
|
package/index.d.ts
CHANGED
package/latch.d.ts
CHANGED
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "ciorent",
|
3
|
-
"version": "0.0.
|
4
|
-
"description": "A low-overhead
|
3
|
+
"version": "0.0.12",
|
4
|
+
"description": "A lightweight, low-overhead concurrency library",
|
5
5
|
"homepage": "https://ciorent.netlify.app",
|
6
6
|
"repository": {
|
7
7
|
"type": "github",
|
@@ -10,7 +10,8 @@
|
|
10
10
|
"keywords": [
|
11
11
|
"low-overhead",
|
12
12
|
"lightweight",
|
13
|
-
"concurrency"
|
13
|
+
"concurrency",
|
14
|
+
"cross-runtime"
|
14
15
|
],
|
15
16
|
"license": "MIT",
|
16
17
|
"type": "module",
|
@@ -18,11 +19,11 @@
|
|
18
19
|
"types": "./index.d.ts",
|
19
20
|
"scripts": {
|
20
21
|
"task": "bun scripts/task.ts",
|
21
|
-
"build:test": "bun
|
22
|
+
"build:test": "bun task build && bun docs && bun test",
|
22
23
|
"build:publish": "bun build:test && bun task report-size && bun task publish",
|
23
24
|
"lint": "eslint ./src",
|
24
25
|
"lint:fix": "eslint ./src --fix",
|
25
|
-
"docs": "typedoc
|
26
|
+
"docs": "typedoc"
|
26
27
|
},
|
27
28
|
"devDependencies": {
|
28
29
|
"@stylistic/eslint-plugin": "latest",
|
@@ -35,6 +36,7 @@
|
|
35
36
|
"tsx": "latest",
|
36
37
|
"typedoc": "^0.27.9",
|
37
38
|
"typedoc-material-theme": "^1.3.0",
|
39
|
+
"typedoc-plugin-inline-sources": "^1.2.1",
|
38
40
|
"typescript": "latest",
|
39
41
|
"typescript-eslint": "latest"
|
40
42
|
}
|
package/semaphore.d.ts
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
/**
|
2
|
-
* @module
|
3
|
-
* Sliding queues
|
2
|
+
* @module Sliding queues
|
4
3
|
*/
|
5
|
-
import type { FixedQueue } from '
|
6
|
-
export { default as init } from '
|
4
|
+
import type { FixedQueue } from './fixed-queue';
|
5
|
+
export { default as init } from './fixed-queue';
|
7
6
|
/**
|
8
7
|
* Push an item to a sliding queue
|
9
8
|
* @param q - The queue to push to
|
package/sliding-queue.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export{default as 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}};
|
package/queue/dropping.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export{default as init}from".";export{pop}from"./sliding";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/queue/sliding.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export{default as init}from".";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}};
|
File without changes
|