ciorent 0.3.5 → 0.3.7

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
@@ -32,7 +32,7 @@ Cross-runtime synchronous and asynchronous sleep functions.
32
32
  ```ts
33
33
  import * as co from 'ciorent';
34
34
 
35
- const logTime = (label: string) => console.log(label + ':', Math.floor(performance.now()) + 'ms');
35
+ const logTime = (label: string) => console.log(`${label}: ${Math.floor(performance.now())}ms`);;
36
36
 
37
37
  logTime('Start');
38
38
 
@@ -70,7 +70,7 @@ const throttle = co.throttle(500, 2);
70
70
 
71
71
  co.spawn(8, async (id) => {
72
72
  await throttle();
73
- console.log(id + ': ' + Math.floor(performance.now()) + 'ms');
73
+ console.log(`${id}: ${Math.floor(performance.now())}ms`);
74
74
  });
75
75
  ```
76
76
 
@@ -79,7 +79,7 @@ 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');
82
+ const logTime = (label: string) => console.log(`${label}: ${Math.floor(performance.now())}ms`);
83
83
 
84
84
  // Expensive sync task
85
85
  const task1 = async () => {
@@ -138,7 +138,7 @@ co.spawn(3, async (id: number) => {
138
138
 
139
139
  while (true) {
140
140
  // Block until the value is sent
141
- const x = await topic.recieve(sub);
141
+ const x = await topic.dispatch(sub);
142
142
  if (x == null) break;
143
143
  console.log(`Task ${id} recieved: ${x}`);
144
144
  }
@@ -182,7 +182,7 @@ Wait for a value to be resolved.
182
182
  ```ts
183
183
  import * as defer from 'ciorent/defer';
184
184
 
185
- const logTime = (label: string) => console.log(label + ':', Math.floor(performance.now()) + 'ms');
185
+ const logTime = (label: string) => console.log(`${label}: ${Math.floor(performance.now())}ms`);
186
186
 
187
187
  const deferredUrl = defer.init<string>();
188
188
 
@@ -212,24 +212,24 @@ Channel is a synchronization primitive via message passing. A message may be sen
212
212
  import * as channel from 'ciorent/channel';
213
213
  import * as co from 'ciorent';
214
214
 
215
- const c = channel.init<number>();
215
+ const chan = channel.init<number>();
216
216
 
217
217
  const run = async () => {
218
218
  for (let i = 0; i < 5; i++) {
219
219
  await co.sleep(100);
220
- channel.send(c, i);
220
+ channel.send(chan, i);
221
221
  console.log('Sent', i);
222
222
  }
223
223
 
224
224
  // Resolve all waiting promises with `undefined`
225
225
  // This is a way to tell the reciever to not listen to more data
226
- channel.flush(c);
226
+ channel.flush(chan);
227
227
  };
228
228
 
229
229
  const log = async () => {
230
230
  while (true) {
231
231
  // Wait until a value is sent to the channel
232
- const x = await channel.recieve(c);
232
+ const x = await channel.recieve(chan);
233
233
  if (x == null) break;
234
234
 
235
235
  console.log('Recieved', x);
@@ -250,7 +250,7 @@ Virtual threads with more controlled execution.
250
250
  import * as co from 'ciorent';
251
251
  import * as fiber from 'ciorent/fiber';
252
252
 
253
- const logTime = (label: string) => console.log(label + ':', Math.floor(performance.now()) + 'ms');
253
+ const logTime = (label: string) => console.log(`${label}: ${Math.floor(performance.now())}ms`);
254
254
 
255
255
  const f1 = fiber.fn(function* () {
256
256
  // Wait for a promise
package/fiber.js CHANGED
@@ -1 +1 @@
1
- import{sleep}from"./index.js";export let paused=(t)=>t[1]===0;export let resumed=(t)=>t[1]===1;export let completed=(t)=>t[1]===2;export let interrupted=(t)=>t[1]===3;let invoke=async(g,thread)=>{await 0;try{let t=g.next();while(!t.done){let v=await t.value;if(thread[1]===0)thread[1]=await new Promise((res)=>{thread[2]=res});if(thread[1]===3)return;t=g.next(v)}thread[1]=2;return t.value}finally{if(thread[1]!==2)thread[1]=3;thread[3].forEach(interrupt)}};export let fn=(f)=>f;export let spawn=(f,...args)=>{let t=[null,1,null,[]];t[0]=invoke(f(t,...args),t);return t};export let pause=(t)=>{if(t[1]===1)t[1]=0};export let resume=(t)=>{if(t[1]===0)t[2]?.(1)};export let interrupt=(t)=>{if(t[1]!==2){if(t[1]===0)t[2]?.(3);else t[1]=3}};export let timeout=async(t,ms)=>{await sleep(ms);interrupt(t)};export function*join(t){return yield t[0]}export let done=(t)=>t[0];export let mount=(child,parent)=>{parent[3].push(child)};export let control=(t,signal)=>{signal.addEventListener("abort",()=>{interrupt(t)})};export function*unwrap(t){return yield t}
1
+ import{sleep}from"./index.js";export let paused=(t)=>t[1]===0;export let resumed=(t)=>t[1]===1;export let completed=(t)=>t[1]===2;export let interrupted=(t)=>t[1]===3;let invoke=async(g,thread)=>{await 0;try{let t=g.next();while(!t.done){let v=await t.value;if(thread[1]===0)thread[1]=await new Promise((res)=>{thread[2]=res});if(thread[1]===3)return;t=g.next(v)}thread[1]=2;return t.value}finally{if(thread[1]!==2)thread[1]=3;thread[3].forEach(interrupt)}};export let fn=(f)=>f;export let spawn=(f,...args)=>{let t=[null,1,null,[]];t[0]=invoke(f(t,...args),t);return t};export let pause=(t)=>{if(t[1]===1)t[1]=0};export let resume=(t)=>{if(t[1]===0){if(t[2]===null)t[1]=1;else t[2](1)}};export let interrupt=(t)=>{if(t[1]!==2){if(t[1]===0&&t[2]!==null)t[2](3);else t[1]=3}};export let timeout=async(t,ms)=>{await sleep(ms);interrupt(t)};export function*join(t){return yield t[0]}export let done=(t)=>t[0];export let mount=(child,parent)=>{parent[3].push(child)};export let control=(t,signal)=>{signal.addEventListener("abort",()=>{interrupt(t)})};export function*unwrap(t){return yield t}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ciorent",
3
- "version": "0.3.5",
3
+ "version": "0.3.7",
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
- "./sliding-queue": "./sliding-queue.js",
22
21
  "./queue": "./queue.js",
23
22
  "./channel": "./channel.js",
24
- "./fiber": "./fiber.js",
25
- "./defer": "./defer.js",
26
- ".": "./index.js",
27
- "./dropping-queue": "./dropping-queue.js",
23
+ "./sliding-queue": "./sliding-queue.js",
28
24
  "./topic": "./topic.js",
25
+ "./lock": "./lock.js",
26
+ ".": "./index.js",
29
27
  "./semaphore": "./semaphore.js",
30
- "./lock": "./lock.js"
28
+ "./defer": "./defer.js",
29
+ "./dropping-queue": "./dropping-queue.js",
30
+ "./fiber": "./fiber.js"
31
31
  }
32
32
  }
package/topic.d.ts CHANGED
@@ -54,4 +54,4 @@ export declare const poll: <T extends {}>(t: Subscriber<T>) => T | undefined;
54
54
  * Returns a promise that resolves when the message queue is not empty
55
55
  * @param t
56
56
  */
57
- export declare const recieve: <T extends {}>(t: Subscriber<T>) => Promise<T | undefined>;
57
+ export declare const dispatch: <T extends {}>(t: Subscriber<T>) => Promise<T | undefined>;
package/topic.js CHANGED
@@ -1 +1 @@
1
- export let init=()=>[[null],[],[]];export let subscribe=(t)=>[t,t[0]];export let publish=(t,value)=>{let head=t[0]=t[0][0]=[null,value];for(let i=0,res=t[1];i<res.length;i++)res[i](head);t[1]=[]};export let flush=(t)=>{let head=t[0]=t[0][0]=[null,undefined];for(let i=0,res=t[1];i<res.length;i++)res[i](head);t[1]=[]};export let poll=(t)=>t[1][0]!==null?(t[1]=t[1][0])[1]:undefined;export let recieve=async(t)=>t[1][0]!==null?(t[1]=t[1][0])[1]:(t[1]=await new Promise((res)=>{t[0][1].push(res)}))[1];
1
+ export let init=()=>{let t=[[null],[],(res)=>{t[1].push(res)}];return t};export let subscribe=(t)=>[t,t[0]];export let publish=(t,value)=>{let head=t[0]=t[0][0]=[null,value];for(let i=0,res=t[1];i<res.length;i++)res[i](head);t[1]=[]};export let flush=(t)=>{let head=t[0]=t[0][0]=[null,undefined];for(let i=0,res=t[1];i<res.length;i++)res[i](head);t[1]=[]};export let poll=(t)=>t[1][0]!==null?(t[1]=t[1][0])[1]:undefined;export let dispatch=async(t)=>t[1][0]!==null?(t[1]=t[1][0])[1]:(t[1]=await new Promise(t[0][2]))[1];