ciorent 0.3.0 → 0.3.1
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 +4 -4
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/latch.js +1 -1
- package/lock.d.ts +3 -1
- package/package.json +4 -4
- package/queue.d.ts +0 -1
- package/semaphore.js +1 -1
package/README.md
CHANGED
@@ -74,8 +74,8 @@ co.spawn(8, async (id) => {
|
|
74
74
|
});
|
75
75
|
```
|
76
76
|
|
77
|
-
####
|
78
|
-
|
77
|
+
#### Yield
|
78
|
+
Continue the execution on next tick, allowing other asynchronous tasks to run.
|
79
79
|
```ts
|
80
80
|
import * as co from 'ciorent';
|
81
81
|
|
@@ -85,7 +85,7 @@ const task1 = async () => {
|
|
85
85
|
|
86
86
|
// Yield control back to the runtime, allowing it to
|
87
87
|
// schedule other tasks
|
88
|
-
await co.
|
88
|
+
await co.nextTick;
|
89
89
|
|
90
90
|
// Simulate heavy operation
|
91
91
|
for (let i = 0; i < (Math.random() + 15) * 1e6; i++)
|
@@ -159,7 +159,7 @@ const task = async (id: number) => {
|
|
159
159
|
console.log('Task', id, 'started');
|
160
160
|
|
161
161
|
// Let the main thread schedules other tasks
|
162
|
-
for (let i = 1; i <= 5; i++) await co.
|
162
|
+
for (let i = 1; i <= 5; i++) await co.nextTick;
|
163
163
|
|
164
164
|
console.log('Task', id, 'end');
|
165
165
|
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export let
|
1
|
+
export let nextTick=Promise.resolve();export let sleep=globalThis.Bun?.sleep??globalThis.process?.getBuiltinModule?.("timers/promises").setTimeout??((ms)=>new Promise((res)=>{setTimeout(res,ms)}));let sharedBuf=new Int32Array(new SharedArrayBuffer(4));export let sleepSync=globalThis.Bun?.sleepSync??((ms)=>{Atomics.wait(sharedBuf,0,0,ms)});export let sequential=async(n,task,...args)=>{for(let i=0;i<n;i++)await task(...args,i)};export let spawn=(n,task,...args)=>{let arr=new Array(n);for(let i=0;i<n;i++)arr[i]=task(...args,i);return arr};export let debounce=(f,ms)=>{let id;return(...a)=>{clearTimeout(id);id=setTimeout(f,ms,...a)}};export let throttle=(ms,limit)=>{let head=[null];let tail=head;let cur=limit;let scheduled=false;let unlock=()=>{cur=limit;if(tail===head){scheduled=false;return}do{cur--;(tail=tail[0])[1]()}while(cur>0&&tail!==head);setTimeout(unlock,ms)};return()=>{if(cur===0){return new Promise((res)=>{head=head[0]=[null,res]})}if(!scheduled){scheduled=true;setTimeout(unlock,ms)}cur--;return nextTick}};
|
package/latch.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
import{
|
1
|
+
import{nextTick 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 close=(latch)=>{if(latch[0]===endPromise){let r;latch[0]=new Promise((res)=>{r=res});latch[1]=r}};
|
package/lock.d.ts
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
/**
|
2
|
+
* @module Lock utilities
|
3
|
+
*/
|
1
4
|
import type { Node as QueueNode } from './queue.js';
|
2
5
|
/**
|
3
6
|
* Describe a lock
|
@@ -15,7 +18,6 @@ export interface Lock<T = any> {
|
|
15
18
|
/**
|
16
19
|
* Acquire an item
|
17
20
|
* @param lock
|
18
|
-
* @param value
|
19
21
|
*/
|
20
22
|
export declare const acquire: <T>(lock: Lock<T>) => Promise<T | undefined>;
|
21
23
|
/**
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "ciorent",
|
3
|
-
"version": "0.3.
|
3
|
+
"version": "0.3.1",
|
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
|
"./latch": "./latch.js",
|
23
|
-
"./lock": "./lock.js",
|
24
22
|
"./dropping-queue": "./dropping-queue.js",
|
25
|
-
"./
|
23
|
+
"./sliding-queue": "./sliding-queue.js",
|
26
24
|
"./channel": "./channel.js",
|
27
25
|
"./fiber": "./fiber.js",
|
26
|
+
"./lock": "./lock.js",
|
28
27
|
"./queue": "./queue.js",
|
29
28
|
"./topic": "./topic.js",
|
29
|
+
"./semaphore": "./semaphore.js",
|
30
30
|
".": "./index.js"
|
31
31
|
}
|
32
32
|
}
|
package/queue.d.ts
CHANGED
package/semaphore.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
import{
|
1
|
+
import{nextTick as resolvedPromise}from"./index.js";import{acquire as lockAcquire,release as lockRelease}from"./lock.js";export let init=(n)=>{let root=[null];return[root,root,n]};export let acquire=(s)=>{s[2]--;return s[2]>=0?resolvedPromise:lockAcquire(s)};export let release=(s)=>{if(s[2]<0)lockRelease(s);s[2]++};export let bind=(f,s)=>async(...a)=>{s[2]--;if(s[2]<0)await acquire(s);try{return await f(...a)}finally{if(s[2]<0)release(s);s[2]++}};
|