Skip to content

Commit

Permalink
Merge branch 'Alwatr:next' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
adltalab authored Jan 3, 2024
2 parents 0da3689 + 2f30ef1 commit 4eec9af
Show file tree
Hide file tree
Showing 51 changed files with 1,386 additions and 225 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Here is a brief overview of the included libraries:
8. [`nano-build`](./packages/nano-build/README.md): This is a tool for building/bundling ECMAScript, TypeScript, and JavaScript libraries. It's user-friendly, requires no setup, follows best practices, has no dependencies, and uses esbuild for improved performance.
9. [`type-helper`](./packages/type-helper/README.md): Collection of useful typescript type helpers.
10. [`wait`](./packages/wait/README.md): Comprehensive toolkit for managing asynchronous operations.
<!-- 9. [`async-queue`](./packages/async-queue/README.md): This is a utility for managing asynchronous operations in a queue. -->
11. [`exit-hook`](./packages/exit-hook/README.md): A utility for registering exit handlers in Node.js.
12. [`flatomise`](./packages/flatomise/README.md): A utility for creating promises that can be externally resolved or rejected.
13. [`async-queue`](./packages/async-queue/README.md): A queue that executes async tasks in order like mutex and semaphore methodology for javascript and typescript.

For more detailed information and guidelines on how to use each package, please refer to to each package's README.
1 change: 1 addition & 0 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"changelogPreset": "conventional-changelog-angular",
"changelogIncludeCommitsClientLogin": " by @%l",
"ignoreChanges": ["*.md"],
"skipBumpOnlyReleases": true,
"message": "release:",
"private": false,
"forceGitTag": true,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"@lerna-lite/publish": "^3.1.0",
"@lerna-lite/run": "^3.1.0",
"@lerna-lite/version": "^3.1.0",
"@typescript-eslint/eslint-plugin": "^6.16.0",
"@typescript-eslint/parser": "^6.16.0",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"eslint": "^8.56.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
Expand Down
15 changes: 15 additions & 0 deletions packages/async-queue/CHANGELOG.md
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
53 changes: 53 additions & 0 deletions packages/async-queue/README.md
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
*/
```
21 changes: 21 additions & 0 deletions packages/async-queue/demo/simple-queue.mjs
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);
85 changes: 85 additions & 0 deletions packages/async-queue/package.json
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"
}
}
75 changes: 75 additions & 0 deletions packages/async-queue/src/main.ts
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;
}
}
11 changes: 11 additions & 0 deletions packages/async-queue/tsconfig.json
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"}]
}
4 changes: 4 additions & 0 deletions packages/deep-clone/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.0.12](https://github.com/Alwatr/nanolib/compare/@alwatr/deep-clone@1.0.11...@alwatr/deep-clone@1.0.12) (2024-01-03)

**Note:** Version bump only for package @alwatr/deep-clone

## [1.0.11](https://github.com/Alwatr/nanolib/compare/@alwatr/deep-clone@1.0.10...@alwatr/deep-clone@1.0.11) (2023-12-26)

**Note:** Version bump only for package @alwatr/deep-clone
Expand Down
4 changes: 2 additions & 2 deletions packages/deep-clone/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alwatr/deep-clone",
"version": "1.0.11",
"version": "1.0.12",
"description": "Clone deeply nested objects and arrays in JavaScript.",
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
"keywords": [
Expand Down Expand Up @@ -57,7 +57,7 @@
"w": "yarn run watch",
"c": "yarn run clean",
"cb": "yarn run clean && yarn run build",
"d": "yarn run build:es && ALWATR_DEBUG=1 yarn node",
"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",
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.0.7](https://github.com/Alwatr/nanolib/compare/@alwatr/eslint-config@1.0.6...@alwatr/eslint-config@1.0.7) (2024-01-03)

**Note:** Version bump only for package @alwatr/eslint-config

## [1.0.6](https://github.com/Alwatr/nanolib/compare/@alwatr/eslint-config@1.0.5...@alwatr/eslint-config@1.0.6) (2023-12-26)

**Note:** Version bump only for package @alwatr/eslint-config
Expand Down
8 changes: 4 additions & 4 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alwatr/eslint-config",
"version": "1.0.6",
"version": "1.0.7",
"description": "Alwatr ECMAScript Style Guide as a ESLint shareable configurations.",
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
"keywords": [
Expand Down Expand Up @@ -45,13 +45,13 @@
},
"prettier": "@alwatr/prettier-config",
"dependencies": {
"esbuild": "^0.19.10"
"esbuild": "^0.19.11"
},
"devDependencies": {
"@alwatr/prettier-config": "workspace:^",
"@alwatr/tsconfig-base": "workspace:^",
"@typescript-eslint/eslint-plugin": "^6.16.0",
"@typescript-eslint/parser": "^6.16.0",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"eslint": "^8.56.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
Expand Down
16 changes: 16 additions & 0 deletions packages/exit-hook/CHANGELOG.md
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
21 changes: 21 additions & 0 deletions packages/exit-hook/README.md
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);
```
Loading

0 comments on commit 4eec9af

Please sign in to comment.