-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Alwatr:next' into next
- Loading branch information
Showing
51 changed files
with
1,386 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. | ||
|
||
# 1.0.0 (2024-01-03) | ||
|
||
### Bug Fixes | ||
|
||
* **async-queue:** remove wait deps ([83b2e11](https://github.com/Alwatr/nanolib/commit/83b2e115a939b90049c4af8d1cd6c4ebee282bf8)) by @njfamirm | ||
|
||
### Features | ||
|
||
* **async-queue:** A simple async queue like mutex and semaphore methodology for javascript and typescript. ([e753ff2](https://github.com/Alwatr/nanolib/commit/e753ff29cf53e0e6bcdd9661666ee60300960db3)) by @AliMD | ||
* **async-queue:** add isRunning ([2e54a0a](https://github.com/Alwatr/nanolib/commit/2e54a0a5200ccbfcc64e443728eb6d16513b4296)) by @njfamirm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Async Queue | ||
|
||
A queue that executes async tasks in order like mutex and semaphore methodology for javascript and typescript. | ||
|
||
## Installation | ||
|
||
```bash | ||
yarn add @alwatr/async-queue | ||
``` | ||
|
||
## Usage | ||
|
||
```typescript | ||
import {AsyncQueue} from '@alwatr/async-queue'; | ||
import {waitForTimeout} from '@alwatr/wait'; | ||
|
||
const queue = new AsyncQueue(); | ||
|
||
async function longTask(n) { | ||
console.log('longTask(%s)', n); | ||
await queue.push('longTaskId', async () => { | ||
console.log('longTask %s start', n); | ||
// Simulate a long task | ||
await waitForTimeout(1000); | ||
}); | ||
console.log('longTask %s end', n); | ||
} | ||
|
||
// run the tasks parallel | ||
longTask(1); | ||
longTask(2); | ||
longTask(3).then(() => console.log('longTask 3 resolved')); | ||
longTask(4); | ||
|
||
/* | ||
Output: | ||
longTask(1) | ||
longTask(2) | ||
longTask(3) | ||
longTask(4) | ||
longTask 1 start | ||
longTask 1 end | ||
longTask 2 start | ||
longTask 2 end | ||
longTask 3 start | ||
longTask 3 end | ||
longTask 3 resolved | ||
longTask 4 start | ||
longTask 4 end | ||
*/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {AsyncQueue} from '@alwatr/async-queue'; | ||
|
||
const queue = new AsyncQueue(); | ||
|
||
async function longTask(n) { | ||
console.log('longTask(%s)', n); | ||
await queue.push('longTaskId', () => { | ||
return new Promise((resolve) => { | ||
console.log('longTask %s start', n); | ||
// Simulate a long task | ||
setTimeout(resolve, 1_000); | ||
}); | ||
}); | ||
console.log('longTask %s end', n); | ||
} | ||
|
||
// run the tasks parallel | ||
longTask(1); | ||
longTask(2); | ||
longTask(3).then(() => console.log('longTask 3 resolved')); | ||
longTask(4); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
{ | ||
"name": "@alwatr/async-queue", | ||
"version": "1.0.0", | ||
"description": "A queue that executes async tasks in order like mutex and semaphore methodology for javascript and typescript.", | ||
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>", | ||
"keywords": [ | ||
"async-queue", | ||
"async", | ||
"queue", | ||
"mutex", | ||
"semaphore", | ||
"lock", | ||
"SpinLock", | ||
"spin-lock", | ||
"SemaphoreSlim", | ||
"semaphore-slim", | ||
"cross-platform", | ||
"ECMAScript", | ||
"typescript", | ||
"javascript", | ||
"node", | ||
"nodejs", | ||
"esm", | ||
"module", | ||
"utility", | ||
"util", | ||
"utils", | ||
"nanolib", | ||
"alwatr" | ||
], | ||
"type": "module", | ||
"main": "./dist/main.cjs", | ||
"module": "./dist/main.mjs", | ||
"types": "./dist/main.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/main.mjs", | ||
"require": "./dist/main.cjs", | ||
"types": "./dist/main.d.ts" | ||
} | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
"**/*.{js,mjs,cjs,map,d.ts,html,md}", | ||
"!demo/**/*" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Alwatr/nanolib", | ||
"directory": "packages/async-queue" | ||
}, | ||
"homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/async-queue#readme", | ||
"bugs": { | ||
"url": "https://github.com/Alwatr/nanolib/issues" | ||
}, | ||
"prettier": "@alwatr/prettier-config", | ||
"scripts": { | ||
"b": "yarn run build", | ||
"w": "yarn run watch", | ||
"c": "yarn run clean", | ||
"cb": "yarn run clean && yarn run build", | ||
"d": "yarn run build:es && DEBUG=1 yarn node", | ||
"build": "yarn run build:ts & yarn run build:es", | ||
"build:es": "nano-build --preset=module", | ||
"build:ts": "tsc --build", | ||
"watch": "yarn run watch:ts & yarn run watch:es", | ||
"watch:es": "yarn run build:es --watch", | ||
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput", | ||
"clean": "rm -rfv dist *.tsbuildinfo" | ||
}, | ||
"dependencies": { | ||
"@alwatr/flatomise": "workspace:^" | ||
}, | ||
"devDependencies": { | ||
"@alwatr/nano-build": "workspace:^", | ||
"@alwatr/prettier-config": "workspace:^", | ||
"@alwatr/tsconfig-base": "workspace:^", | ||
"@alwatr/type-helper": "workspace:^", | ||
"@types/node": "^20.10.6", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import {newFlatomise} from '@alwatr/flatomise'; | ||
|
||
import type {Dictionary} from '@alwatr/type-helper'; | ||
|
||
/** | ||
* A queue that executes async tasks in order like mutex and semaphore methodology | ||
* | ||
* @example | ||
* ```ts | ||
* const queue = new AsyncQueue(); | ||
* | ||
* function longTask() { | ||
* queue.push('longTaskId', async () => { | ||
* // ... | ||
* }); | ||
* } | ||
* ``` | ||
*/ | ||
export class AsyncQueue { | ||
/** | ||
* A record of task IDs and their corresponding last queued task promises. | ||
*/ | ||
private queue__: Dictionary<Promise<void>> = {}; | ||
|
||
/** | ||
* Push a async task to the queue. | ||
* | ||
* @param taskId task id | ||
* @param task async task | ||
* @returns A promise that resolves when the task is done. | ||
* | ||
* @example | ||
* ```typescript | ||
* const queue = new AsyncQueue(); | ||
* | ||
* function longTask() { | ||
* queue.push('longTaskId', async () => { | ||
* // ... | ||
* }); | ||
* ``` | ||
*/ | ||
async push(taskId: string, task: () => Promise<void>): Promise<void> { | ||
const flatomise = newFlatomise(); | ||
|
||
const previousTaskPromise = this.queue__[taskId]; | ||
this.queue__[taskId] = flatomise.promise; | ||
|
||
try { | ||
await previousTaskPromise; | ||
} | ||
catch (_e) { | ||
// ignore | ||
} | ||
|
||
setTimeout(() => { | ||
task().then(flatomise.resolve, flatomise.reject).then(() => { | ||
if (this.queue__[taskId] === flatomise.promise) { | ||
delete this.queue__[taskId]; | ||
} | ||
}); | ||
}, 0); | ||
|
||
return flatomise.promise; | ||
} | ||
|
||
/** | ||
* Check if the task running in the queue. | ||
* | ||
* @param taskId task id | ||
* @returns true if the task is running, otherwise false. | ||
*/ | ||
isRunning(taskId: string): boolean { | ||
return this.queue__[taskId] !== undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "@alwatr/tsconfig-base/tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"emitDeclarationOnly": true, | ||
"composite": true, | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"references": [{"path": "../flatomise"}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. | ||
|
||
## [1.0.1](https://github.com/Alwatr/nanolib/compare/@alwatr/exit-hook@1.0.0...@alwatr/exit-hook@1.0.1) (2024-01-03) | ||
|
||
### Bug Fixes | ||
|
||
* **exit-hook:** typo function name ([e8df52a](https://github.com/Alwatr/nanolib/commit/e8df52a8f527e653025abe6a8bc54719498db83e)) by @njfamirm | ||
|
||
# 1.0.0 (2024-01-03) | ||
|
||
### Features | ||
|
||
* **exit-hook:** new package ([a41b3a0](https://github.com/Alwatr/nanolib/commit/a41b3a01a4e6af595521e506326678eb96491a11)) by @njfamirm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Exit Hook | ||
|
||
A utility for registering exit handlers in Node.js. | ||
|
||
## Installation | ||
|
||
```bash | ||
yarn add @alwatr/exit-hook | ||
``` | ||
|
||
## Usage | ||
|
||
```typescript | ||
import {exitHook} from '@alwatr/exit-hook'; | ||
|
||
const saveAllData = () => { | ||
// save all data | ||
}; | ||
|
||
exitHook(saveAllData); | ||
``` |
Oops, something went wrong.