@alwatr/async-queue 1.0.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # 1.0.0 (2024-01-03)
7
+
8
+ ### Bug Fixes
9
+
10
+ * **async-queue:** remove wait deps ([83b2e11](https://github.com/Alwatr/nanolib/commit/83b2e115a939b90049c4af8d1cd6c4ebee282bf8)) by @njfamirm
11
+
12
+ ### Features
13
+
14
+ * **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
15
+ * **async-queue:** add isRunning ([2e54a0a](https://github.com/Alwatr/nanolib/commit/2e54a0a5200ccbfcc64e443728eb6d16513b4296)) by @njfamirm
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 S. Ali Mihandoost
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Async Queue
2
+
3
+ A queue that executes async tasks in order like mutex and semaphore methodology for javascript and typescript.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ yarn add @alwatr/async-queue
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import {AsyncQueue} from '@alwatr/async-queue';
15
+ import {waitForTimeout} from '@alwatr/wait';
16
+
17
+ const queue = new AsyncQueue();
18
+
19
+ async function longTask(n) {
20
+ console.log('longTask(%s)', n);
21
+ await queue.push('longTaskId', async () => {
22
+ console.log('longTask %s start', n);
23
+ // Simulate a long task
24
+ await waitForTimeout(1000);
25
+ });
26
+ console.log('longTask %s end', n);
27
+ }
28
+
29
+ // run the tasks parallel
30
+ longTask(1);
31
+ longTask(2);
32
+ longTask(3).then(() => console.log('longTask 3 resolved'));
33
+ longTask(4);
34
+
35
+ /*
36
+ Output:
37
+
38
+ longTask(1)
39
+ longTask(2)
40
+ longTask(3)
41
+ longTask(4)
42
+ longTask 1 start
43
+ longTask 1 end
44
+ longTask 2 start
45
+ longTask 2 end
46
+ longTask 3 start
47
+ longTask 3 end
48
+ longTask 3 resolved
49
+ longTask 4 start
50
+ longTask 4 end
51
+
52
+ */
53
+ ```
package/dist/main.cjs ADDED
@@ -0,0 +1,3 @@
1
+ /* @alwatr/async-queue v1.0.0 */
2
+ "use strict";var s=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var l=(e,i,t)=>i in e?s(e,i,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[i]=t;var v=(e,i)=>{for(var t in i)s(e,t,{get:i[t],enumerable:!0})},f=(e,i,t,r)=>{if(i&&typeof i=="object"||typeof i=="function")for(let o of h(i))!c.call(e,o)&&o!==t&&s(e,o,{get:()=>i[o],enumerable:!(r=a(i,o))||r.enumerable});return e};var u=e=>f(s({},"__esModule",{value:!0}),e);var m=(e,i,t)=>(l(e,typeof i!="symbol"?i+"":i,t),t);var y={};v(y,{AsyncQueue:()=>n});module.exports=u(y);var p=require("@alwatr/flatomise"),n=class{constructor(){m(this,"i",{})}async push(i,t){let r=(0,p.newFlatomise)(),o=this.i[i];this.i[i]=r.promise;try{await o}catch{}return setTimeout(()=>{t().then(r.resolve,r.reject).then(()=>{this.i[i]===r.promise&&delete this.i[i]})},0),r.promise}isRunning(i){return this.i[i]!==void 0}};0&&(module.exports={AsyncQueue});
3
+ //# sourceMappingURL=main.cjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/main.ts"],
4
+ "sourcesContent": ["import {newFlatomise} from '@alwatr/flatomise';\n\nimport type {Dictionary} from '@alwatr/type-helper';\n\n/**\n * A queue that executes async tasks in order like mutex and semaphore methodology\n *\n * @example\n * ```ts\n * const queue = new AsyncQueue();\n *\n * function longTask() {\n * queue.push('longTaskId', async () => {\n * // ...\n * });\n * }\n * ```\n */\nexport class AsyncQueue {\n /**\n * A record of task IDs and their corresponding last queued task promises.\n */\n private queue__: Dictionary<Promise<void>> = {};\n\n /**\n * Push a async task to the queue.\n *\n * @param taskId task id\n * @param task async task\n * @returns A promise that resolves when the task is done.\n *\n * @example\n * ```typescript\n * const queue = new AsyncQueue();\n *\n * function longTask() {\n * queue.push('longTaskId', async () => {\n * // ...\n * });\n * ```\n */\n async push(taskId: string, task: () => Promise<void>): Promise<void> {\n const flatomise = newFlatomise();\n\n const previousTaskPromise = this.queue__[taskId];\n this.queue__[taskId] = flatomise.promise;\n\n try {\n await previousTaskPromise;\n }\n catch (_e) {\n // ignore\n }\n\n setTimeout(() => {\n task().then(flatomise.resolve, flatomise.reject).then(() => {\n if (this.queue__[taskId] === flatomise.promise) {\n delete this.queue__[taskId];\n }\n });\n }, 0);\n\n return flatomise.promise;\n }\n\n /**\n * Check if the task running in the queue.\n *\n * @param taskId task id\n * @returns true if the task is running, otherwise false.\n */\n isRunning(taskId: string): boolean {\n return this.queue__[taskId] !== undefined;\n }\n}\n"],
5
+ "mappings": ";qjBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,gBAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAA2B,6BAkBdC,EAAN,KAAiB,CAAjB,cAILC,EAAA,KAAQC,IAAqC,CAAC,GAmB9C,MAAM,KAAKC,EAAgBC,EAA0C,CACnE,IAAMC,KAAY,gBAAa,EAEzBC,EAAsB,KAAKJ,EAAQC,CAAM,EAC/C,KAAKD,EAAQC,CAAM,EAAIE,EAAU,QAEjC,GAAI,CACF,MAAMC,CACR,MACW,CAEX,CAEA,kBAAW,IAAM,CACfF,EAAK,EAAE,KAAKC,EAAU,QAASA,EAAU,MAAM,EAAE,KAAK,IAAM,CACtD,KAAKH,EAAQC,CAAM,IAAME,EAAU,SACrC,OAAO,KAAKH,EAAQC,CAAM,CAE9B,CAAC,CACH,EAAG,CAAC,EAEGE,EAAU,OACnB,CAQA,UAAUF,EAAyB,CACjC,OAAO,KAAKD,EAAQC,CAAM,IAAM,MAClC,CACF",
6
+ "names": ["main_exports", "__export", "AsyncQueue", "__toCommonJS", "import_flatomise", "AsyncQueue", "__publicField", "queue__", "taskId", "task", "flatomise", "previousTaskPromise"]
7
+ }
package/dist/main.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ /**
2
+ * A queue that executes async tasks in order like mutex and semaphore methodology
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * const queue = new AsyncQueue();
7
+ *
8
+ * function longTask() {
9
+ * queue.push('longTaskId', async () => {
10
+ * // ...
11
+ * });
12
+ * }
13
+ * ```
14
+ */
15
+ export declare class AsyncQueue {
16
+ /**
17
+ * A record of task IDs and their corresponding last queued task promises.
18
+ */
19
+ private queue__;
20
+ /**
21
+ * Push a async task to the queue.
22
+ *
23
+ * @param taskId task id
24
+ * @param task async task
25
+ * @returns A promise that resolves when the task is done.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * const queue = new AsyncQueue();
30
+ *
31
+ * function longTask() {
32
+ * queue.push('longTaskId', async () => {
33
+ * // ...
34
+ * });
35
+ * ```
36
+ */
37
+ push(taskId: string, task: () => Promise<void>): Promise<void>;
38
+ /**
39
+ * Check if the task running in the queue.
40
+ *
41
+ * @param taskId task id
42
+ * @returns true if the task is running, otherwise false.
43
+ */
44
+ isRunning(taskId: string): boolean;
45
+ }
46
+ //# sourceMappingURL=main.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;GAaG;AACH,qBAAa,UAAU;IACrB;;OAEG;IACH,OAAO,CAAC,OAAO,CAAiC;IAEhD;;;;;;;;;;;;;;;;OAgBG;IACG,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAwBpE;;;;;OAKG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;CAGnC"}
package/dist/main.mjs ADDED
@@ -0,0 +1,3 @@
1
+ /* @alwatr/async-queue v1.0.0 */
2
+ var m=Object.defineProperty;var p=(t,i,e)=>i in t?m(t,i,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[i]=e;var o=(t,i,e)=>(p(t,typeof i!="symbol"?i+"":i,e),e);import{newFlatomise as a}from"@alwatr/flatomise";var s=class{constructor(){o(this,"i",{})}async push(i,e){let r=a(),n=this.i[i];this.i[i]=r.promise;try{await n}catch{}return setTimeout(()=>{e().then(r.resolve,r.reject).then(()=>{this.i[i]===r.promise&&delete this.i[i]})},0),r.promise}isRunning(i){return this.i[i]!==void 0}};export{s as AsyncQueue};
3
+ //# sourceMappingURL=main.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/main.ts"],
4
+ "sourcesContent": ["import {newFlatomise} from '@alwatr/flatomise';\n\nimport type {Dictionary} from '@alwatr/type-helper';\n\n/**\n * A queue that executes async tasks in order like mutex and semaphore methodology\n *\n * @example\n * ```ts\n * const queue = new AsyncQueue();\n *\n * function longTask() {\n * queue.push('longTaskId', async () => {\n * // ...\n * });\n * }\n * ```\n */\nexport class AsyncQueue {\n /**\n * A record of task IDs and their corresponding last queued task promises.\n */\n private queue__: Dictionary<Promise<void>> = {};\n\n /**\n * Push a async task to the queue.\n *\n * @param taskId task id\n * @param task async task\n * @returns A promise that resolves when the task is done.\n *\n * @example\n * ```typescript\n * const queue = new AsyncQueue();\n *\n * function longTask() {\n * queue.push('longTaskId', async () => {\n * // ...\n * });\n * ```\n */\n async push(taskId: string, task: () => Promise<void>): Promise<void> {\n const flatomise = newFlatomise();\n\n const previousTaskPromise = this.queue__[taskId];\n this.queue__[taskId] = flatomise.promise;\n\n try {\n await previousTaskPromise;\n }\n catch (_e) {\n // ignore\n }\n\n setTimeout(() => {\n task().then(flatomise.resolve, flatomise.reject).then(() => {\n if (this.queue__[taskId] === flatomise.promise) {\n delete this.queue__[taskId];\n }\n });\n }, 0);\n\n return flatomise.promise;\n }\n\n /**\n * Check if the task running in the queue.\n *\n * @param taskId task id\n * @returns true if the task is running, otherwise false.\n */\n isRunning(taskId: string): boolean {\n return this.queue__[taskId] !== undefined;\n }\n}\n"],
5
+ "mappings": ";wKAAA,OAAQ,gBAAAA,MAAmB,oBAkBpB,IAAMC,EAAN,KAAiB,CAAjB,cAILC,EAAA,KAAQC,IAAqC,CAAC,GAmB9C,MAAM,KAAKC,EAAgBC,EAA0C,CACnE,IAAMC,EAAYN,EAAa,EAEzBO,EAAsB,KAAKJ,EAAQC,CAAM,EAC/C,KAAKD,EAAQC,CAAM,EAAIE,EAAU,QAEjC,GAAI,CACF,MAAMC,CACR,MACW,CAEX,CAEA,kBAAW,IAAM,CACfF,EAAK,EAAE,KAAKC,EAAU,QAASA,EAAU,MAAM,EAAE,KAAK,IAAM,CACtD,KAAKH,EAAQC,CAAM,IAAME,EAAU,SACrC,OAAO,KAAKH,EAAQC,CAAM,CAE9B,CAAC,CACH,EAAG,CAAC,EAEGE,EAAU,OACnB,CAQA,UAAUF,EAAyB,CACjC,OAAO,KAAKD,EAAQC,CAAM,IAAM,MAClC,CACF",
6
+ "names": ["newFlatomise", "AsyncQueue", "__publicField", "queue__", "taskId", "task", "flatomise", "previousTaskPromise"]
7
+ }
package/package.json ADDED
@@ -0,0 +1,86 @@
1
+ {
2
+ "name": "@alwatr/async-queue",
3
+ "version": "1.0.0",
4
+ "description": "A queue that executes async tasks in order like mutex and semaphore methodology for javascript and typescript.",
5
+ "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
6
+ "keywords": [
7
+ "async-queue",
8
+ "async",
9
+ "queue",
10
+ "mutex",
11
+ "semaphore",
12
+ "lock",
13
+ "SpinLock",
14
+ "spin-lock",
15
+ "SemaphoreSlim",
16
+ "semaphore-slim",
17
+ "cross-platform",
18
+ "ECMAScript",
19
+ "typescript",
20
+ "javascript",
21
+ "node",
22
+ "nodejs",
23
+ "esm",
24
+ "module",
25
+ "utility",
26
+ "util",
27
+ "utils",
28
+ "nanolib",
29
+ "alwatr"
30
+ ],
31
+ "type": "module",
32
+ "main": "./dist/main.cjs",
33
+ "module": "./dist/main.mjs",
34
+ "types": "./dist/main.d.ts",
35
+ "exports": {
36
+ ".": {
37
+ "import": "./dist/main.mjs",
38
+ "require": "./dist/main.cjs",
39
+ "types": "./dist/main.d.ts"
40
+ }
41
+ },
42
+ "license": "MIT",
43
+ "files": [
44
+ "**/*.{js,mjs,cjs,map,d.ts,html,md}",
45
+ "!demo/**/*"
46
+ ],
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "repository": {
51
+ "type": "git",
52
+ "url": "https://github.com/Alwatr/nanolib",
53
+ "directory": "packages/async-queue"
54
+ },
55
+ "homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/async-queue#readme",
56
+ "bugs": {
57
+ "url": "https://github.com/Alwatr/nanolib/issues"
58
+ },
59
+ "prettier": "@alwatr/prettier-config",
60
+ "scripts": {
61
+ "b": "yarn run build",
62
+ "w": "yarn run watch",
63
+ "c": "yarn run clean",
64
+ "cb": "yarn run clean && yarn run build",
65
+ "d": "yarn run build:es && ALWATR_DEBUG=1 yarn node",
66
+ "build": "yarn run build:ts & yarn run build:es",
67
+ "build:es": "nano-build --preset=module",
68
+ "build:ts": "tsc --build",
69
+ "watch": "yarn run watch:ts & yarn run watch:es",
70
+ "watch:es": "yarn run build:es --watch",
71
+ "watch:ts": "yarn run build:ts --watch --preserveWatchOutput",
72
+ "clean": "rm -rfv dist *.tsbuildinfo"
73
+ },
74
+ "dependencies": {
75
+ "@alwatr/flatomise": "^1.0.0"
76
+ },
77
+ "devDependencies": {
78
+ "@alwatr/nano-build": "^1.2.3",
79
+ "@alwatr/prettier-config": "^1.0.4",
80
+ "@alwatr/tsconfig-base": "^1.1.0",
81
+ "@alwatr/type-helper": "^1.0.2",
82
+ "@types/node": "^20.10.6",
83
+ "typescript": "^5.3.3"
84
+ },
85
+ "gitHead": "154de55566d7f39bba46d269585bae838499f0bf"
86
+ }