ciorent 0.0.6 → 0.0.8

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
@@ -41,13 +41,13 @@ console.log('Slept for about 1s');
41
41
  Go-like channels for synchronizations:
42
42
  ```ts
43
43
  import * as channel from 'ciorent/channel';
44
+ import * as cio from 'ciorent';
44
45
 
45
46
  const c = channel.init<number>();
46
- const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms));
47
47
 
48
48
  const run = async () => {
49
49
  for (let i = 0; i < 10; i++) {
50
- await sleep(10);
50
+ await cio.sleep(10);
51
51
  channel.send(c, i);
52
52
  console.log('Sent', i);
53
53
  }
@@ -95,9 +95,6 @@ const task = async () => {
95
95
 
96
96
  const prepare = () => {
97
97
  console.log('Run before fetch:', performance.now().toFixed(2));
98
-
99
- // Unblock the latch
100
- latch.open(fetchLatch);
101
98
  }
102
99
 
103
100
  const main = async () => {
@@ -105,15 +102,19 @@ const main = async () => {
105
102
  await cio.sleep(500);
106
103
  prepare();
107
104
 
105
+ // Allows all previously blocked tasks to run
106
+ latch.open(fetchLatch);
107
+
108
+ // Reclose the latch
109
+ // Tasks that aren't blocked yet will be blocked
110
+ latch.reset(fetchLatch);
111
+
108
112
  return p;
109
113
  }
110
114
 
111
115
  // Run fetch after 500ms
112
116
  await main();
113
117
 
114
- // Re-close the latch
115
- latch.close(fetchLatch);
116
-
117
118
  // Run fetch after another 500ms
118
119
  await main();
119
120
  ```
@@ -166,15 +167,14 @@ const task = semaphore.task(
166
167
  semaphore.init(2),
167
168
  async (task: number) => {
168
169
  for (let i = 1; i <= 5; i++) {
169
- await cio.pause;
170
170
  console.log('Task', task, 'iteration', i);
171
+ await cio.pause;
171
172
  }
172
173
 
173
- await cio.sleep(500);
174
174
  console.log('Task', task, 'end');
175
175
  }
176
176
  );
177
177
 
178
- // Try to run 5 tasks concurrently
179
- for (let i = 1; i <= 5; i++) task(i);
178
+ // Try to run 6 tasks with 4 tasks running concurrently
179
+ cio.concurrent(6, task, 4);
180
180
  ```
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @module
3
+ * Concurrency controls
4
+ */
5
+ /**
6
+ * Describe a concurrency controller
7
+ */
8
+ export type Controller = <T>(task: () => Promise<T>) => Promise<T>;
9
+ /**
10
+ * Create a concurrency controller, allow n tasks to run concurrently
11
+ * @param n
12
+ */
13
+ declare const _default: (n: number) => Controller;
14
+ export default _default;
package/concurrent.js ADDED
@@ -0,0 +1 @@
1
+ export default (n)=>{let pending=new Array(n);let cnt=0;return async(f)=>{if(cnt<n)return pending[cnt++]=f();await Promise.allSettled(pending);cnt=0;return pending[0]=f()}};
package/index.d.ts CHANGED
@@ -15,3 +15,16 @@ export declare const pause: Promise<void>;
15
15
  * @param ms - Sleep duration in milliseconds
16
16
  */
17
17
  export declare const sleep: (ms: number) => Promise<void>;
18
+ /**
19
+ * Spawn n tasks that runs sequentially
20
+ * @param n
21
+ * @param task - The function to run
22
+ */
23
+ export declare const sequential: (n: number, task: (id: number) => Promise<any>) => Promise<void>;
24
+ /**
25
+ * Spawn n tasks that runs concurrently
26
+ * @param n
27
+ * @param task - The function to run
28
+ * @param concurrency - The amount of task to run concurrently
29
+ */
30
+ export declare const concurrent: (n: number, task: (id: number) => Promise<any>, concurrency?: number) => Promise<any>;
package/index.js CHANGED
@@ -1 +1 @@
1
- export let pause=Promise.resolve();export let sleep=globalThis.Bun?.sleep??globalThis.process?.getBuiltinModule?.("timers/promises").setTimeout??((ms)=>new Promise((res)=>{setTimeout(res,ms)}));
1
+ export let pause=Promise.resolve();export let sleep=globalThis.Bun?.sleep??globalThis.process?.getBuiltinModule?.("timers/promises").setTimeout??((ms)=>new Promise((res)=>{setTimeout(res,ms)}));export let sequential=async(n,task)=>{for(let i=1;i<=n;i++)await task(i)};export let concurrent=async(n,task,concurrency)=>{if(concurrency==null){let arr=new Array(n);for(let i=0;i<n;i++)arr[i]=task(i+1);return Promise.all(arr)}let arr=new Array(concurrency);let pre=1;for(let block=n/concurrency>>>0;block>0;block--){for(let j=0;j<concurrency;j++)arr[j]=task(pre+j);await Promise.all(arr);pre+=concurrency}n-=pre;for(let i=0;i<=n;i++)arr[i]=task(pre+i);return Promise.all(arr)};
package/latch.d.ts CHANGED
@@ -22,6 +22,6 @@ export declare const pause: (latch: Latch) => Promise<void>;
22
22
  */
23
23
  export declare const open: (latch: Latch) => void;
24
24
  /**
25
- * Close a latch
25
+ * Re-close a latch
26
26
  */
27
- export declare const close: (latch: Latch) => void;
27
+ export declare const reset: (latch: Latch) => void;
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 close=(latch)=>{if(latch[0]===endPromise){let r;latch[0]=new Promise((res)=>{r=res});latch[1]=r}};
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}};
package/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "A low-overhead, lightweight concurrency library",
5
5
  "homepage": "https://ciorent.netlify.app",
6
6
  "repository": {
7
7
  "type": "github",
8
8
  "url": "https://github.com/re-utils/ciorent"
9
9
  },
10
- "keywords": [],
10
+ "keywords": [
11
+ "low-overhead",
12
+ "lightweight",
13
+ "concurrency"
14
+ ],
11
15
  "license": "MIT",
12
16
  "type": "module",
13
17
  "main": "./index.js",
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)=>{if(s[0]===0){let r;let p=new Promise((res)=>{r=res});s[1]=s[1][1]=[r,null];return p}s[0]--;return resolvedPromise};export let signal=(s)=>{if(s[1]===s[2])s[0]++;else(s[2]=s[2][1])[0]()};export let task=(s,f)=>async(...a)=>{await pause(s);let r=await f(...a);signal(s);return r};
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)=>{try{await pause(s);return await f(...a)}finally{signal(s)}};