ciorent 0.0.15 → 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 CHANGED
@@ -19,44 +19,8 @@ const task = semaphore.task(
19
19
  }
20
20
  );
21
21
 
22
- // Try to run 6 tasks with 4 tasks running concurrently
23
- cio.concurrent(6, task, 4);
24
- ```
25
-
26
- ## Pubsub
27
- A fast, simple publish-subscribe API.
28
-
29
- ```ts
30
- import * as topic from 'ciorent/topic';
31
- import * as cio from 'ciorent';
32
-
33
- const messages = topic.init<number>();
34
-
35
- // A task that publish messages
36
- const publisher = async () => {
37
- for (let i = 0; i < 5; i++) {
38
- await cio.sleep(100);
39
- topic.pub(messages, i);
40
- }
41
-
42
- // Resolve all waiting promises
43
- // And clear the value queue
44
- topic.flush(messages);
45
- }
46
-
47
- // Spawn 5 tasks that recieve messages
48
- cio.concurrent(5, async (id: number) => {
49
- const sub = topic.sub(messages);
50
-
51
- while (true) {
52
- // Block until the value is sent
53
- const x = await topic.recieve(sub);
54
- if (x == null) break;
55
- console.log(`Task ${id}: ${x}`);
56
- }
57
- });
58
-
59
- publisher();
22
+ // Try to run 6 tasks concurrently
23
+ cio.concurrent(6, task);
60
24
  ```
61
25
 
62
26
  ## Channel
@@ -82,7 +46,7 @@ const run = async () => {
82
46
 
83
47
  const log = async () => {
84
48
  while (true) {
85
- // Block until x is recieved
49
+ // Wait until a value is sent
86
50
  const x = await channel.recieve(c);
87
51
  if (x == null) break;
88
52
 
@@ -97,6 +61,42 @@ log();
97
61
  console.log('Starting...');
98
62
  ```
99
63
 
64
+ ## Pubsub
65
+ A fast, simple publish-subscribe API.
66
+
67
+ ```ts
68
+ import * as topic from 'ciorent/topic';
69
+ import * as cio from 'ciorent';
70
+
71
+ const messages = topic.init<number>();
72
+
73
+ // A task that publish messages
74
+ const publisher = async () => {
75
+ for (let i = 0; i < 5; i++) {
76
+ await cio.sleep(100);
77
+ topic.pub(messages, i);
78
+ }
79
+
80
+ // Resolve all waiting promises
81
+ // And clear the value queue
82
+ topic.flush(messages);
83
+ }
84
+
85
+ // Spawn 5 tasks that recieve messages
86
+ cio.concurrent(5, async (id: number) => {
87
+ const sub = topic.sub(messages);
88
+
89
+ while (true) {
90
+ // Block until the value is sent
91
+ const x = await topic.recieve(sub);
92
+ if (x == null) break;
93
+ console.log(`Task ${id}: ${x}`);
94
+ }
95
+ });
96
+
97
+ publisher();
98
+ ```
99
+
100
100
  ## Latch
101
101
  Latch is a synchronization primitive that allows one process to wait until another completes an operation before continuing execution.
102
102
 
@@ -181,7 +181,7 @@ console.log('Hi');
181
181
  ```
182
182
 
183
183
  ### Spawning tasks
184
- Creating new tasks with controlled concurrency.
184
+ Utilities to create and run tasks.
185
185
  ```ts
186
186
  import * as cio from 'ciorent';
187
187
 
package/channel.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module Channels
3
3
  */
4
- import type { QueueNode } from './fixed-queue';
4
+ import type { QueueNode } from './fixed-queue.js';
5
5
  /**
6
6
  * Describe a channel
7
7
  */
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "A lightweight, low-overhead concurrency library",
5
5
  "homepage": "https://ciorent.netlify.app",
6
6
  "repository": {
package/semaphore.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module Semaphores
3
3
  */
4
- import type { QueueNode } from './fixed-queue';
4
+ import type { QueueNode } from './fixed-queue.js';
5
5
  /**
6
6
  * Describe a semaphore
7
7
  */
@@ -31,6 +31,10 @@ export declare const pause: (s: Semaphore) => Promise<void>;
31
31
  * Signal to the semaphore to release access
32
32
  */
33
33
  export declare const signal: (s: Semaphore) => void;
34
+ /**
35
+ * Wrap a task to bind to a custom semaphore later
36
+ */
37
+ export declare const wrap: <Args extends any[], Return extends Promise<any>>(f: (...args: Args) => Return) => (s: Semaphore, ...a: Args) => Return;
34
38
  /**
35
39
  * Create a task that acquire a semaphore and release the access when it's finished
36
40
  */
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 task=(s,f)=>async(...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)}};
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)};
@@ -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}};
package/topic.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module Pubsub
3
3
  */
4
- import type { QueueNode } from './fixed-queue';
4
+ import type { QueueNode } from './fixed-queue.js';
5
5
  /**
6
6
  * Describe a topic
7
7
  */