@alwatr/async-queue 1.0.2 → 1.1.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 CHANGED
@@ -3,6 +3,13 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [1.1.0](https://github.com/Alwatr/nanolib/compare/@alwatr/async-queue@1.0.2...@alwatr/async-queue@1.1.0) (2024-01-08)
7
+
8
+ ### Features
9
+
10
+ * **AsyncQueue:** generic type for push method ([aec6710](https://github.com/Alwatr/nanolib/commit/aec6710041347452fa52bb2556e59d24bb0932a3)) by @AliMD
11
+ * **AsyncQueue:** waitForFinish ([47decb4](https://github.com/Alwatr/nanolib/commit/47decb44a21338393d0820e9a965bf27f22dfbcd)) by @AliMD
12
+
6
13
  ## [1.0.2](https://github.com/Alwatr/nanolib/compare/@alwatr/async-queue@1.0.1...@alwatr/async-queue@1.0.2) (2024-01-08)
7
14
 
8
15
  **Note:** Version bump only for package @alwatr/async-queue
package/dist/main.cjs CHANGED
@@ -1,3 +1,3 @@
1
- /* @alwatr/async-queue v1.0.2 */
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});
1
+ /* @alwatr/async-queue v1.1.0 */
2
+ "use strict";var s=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var c=(i,e,r)=>e in i?s(i,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):i[e]=r;var u=(i,e)=>{for(var r in e)s(i,r,{get:e[r],enumerable:!0})},w=(i,e,r,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of p(e))!l.call(i,o)&&o!==r&&s(i,o,{get:()=>e[o],enumerable:!(t=h(e,o))||t.enumerable});return i};var P=i=>w(s({},"__esModule",{value:!0}),i);var m=(i,e,r)=>(c(i,typeof e!="symbol"?e+"":e,r),r);var T={};u(T,{AsyncQueue:()=>n});module.exports=P(T);var a=require("@alwatr/flatomise"),n=class{constructor(){m(this,"e",{})}async push(e,r){let t=(0,a.newFlatomise)(),o=this.e[e];this.e[e]=t.promise;try{await o}catch{}return setTimeout(()=>{r().then(t.resolve,t.reject).then(()=>{this.e[e]===t.promise&&delete this.e[e]})},0),t.promise}isRunning(e){return this.e[e]!==void 0}waitForFinish(e){return this.e[e]??Promise.resolve()}};0&&(module.exports={AsyncQueue});
3
3
  //# sourceMappingURL=main.cjs.map
package/dist/main.cjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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",
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<unknown>> = {};\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<T>(taskId: string, task: () => Promise<T>): Promise<T> {\n const flatomise = newFlatomise<T>();\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()\n .then(flatomise.resolve, flatomise.reject)\n .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 * @example\n * ```typescript\n * if (queue.isRunning('longTaskId')) {\n * // ...\n * }\n * ```\n */\n isRunning(taskId: string): boolean {\n return this.queue__[taskId] !== undefined;\n }\n\n /**\n * Wait for the all tasks in the queue to finish.\n *\n * @param taskId task id\n * @returns A promise that resolves when all tasks are done.\n * @example\n * ```typescript\n * await queue.waitForFinish('longTaskId');\n * ```\n */\n waitForFinish(taskId: string): Promise<unknown> {\n return this.queue__[taskId] ?? Promise.resolve();\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,IAAwC,CAAC,GAmBjD,MAAM,KAAQC,EAAgBC,EAAoC,CAChE,IAAMC,KAAY,gBAAgB,EAE5BC,EAAsB,KAAKJ,EAAQC,CAAM,EAC/C,KAAKD,EAAQC,CAAM,EAAIE,EAAU,QAEjC,GAAI,CACF,MAAMC,CACR,MACW,CAEX,CAEA,kBAAW,IAAM,CACfF,EAAK,EACF,KAAKC,EAAU,QAASA,EAAU,MAAM,EACxC,KAAK,IAAM,CACN,KAAKH,EAAQC,CAAM,IAAME,EAAU,SACrC,OAAO,KAAKH,EAAQC,CAAM,CAE9B,CAAC,CACL,EAAG,CAAC,EAEGE,EAAU,OACnB,CAcA,UAAUF,EAAyB,CACjC,OAAO,KAAKD,EAAQC,CAAM,IAAM,MAClC,CAYA,cAAcA,EAAkC,CAC9C,OAAO,KAAKD,EAAQC,CAAM,GAAK,QAAQ,QAAQ,CACjD,CACF",
6
6
  "names": ["main_exports", "__export", "AsyncQueue", "__toCommonJS", "import_flatomise", "AsyncQueue", "__publicField", "queue__", "taskId", "task", "flatomise", "previousTaskPromise"]
7
7
  }
package/dist/main.d.ts CHANGED
@@ -34,13 +34,30 @@ export declare class AsyncQueue {
34
34
  * });
35
35
  * ```
36
36
  */
37
- push(taskId: string, task: () => Promise<void>): Promise<void>;
37
+ push<T>(taskId: string, task: () => Promise<T>): Promise<T>;
38
38
  /**
39
39
  * Check if the task running in the queue.
40
40
  *
41
41
  * @param taskId task id
42
42
  * @returns true if the task is running, otherwise false.
43
+ * @example
44
+ * ```typescript
45
+ * if (queue.isRunning('longTaskId')) {
46
+ * // ...
47
+ * }
48
+ * ```
43
49
  */
44
50
  isRunning(taskId: string): boolean;
51
+ /**
52
+ * Wait for the all tasks in the queue to finish.
53
+ *
54
+ * @param taskId task id
55
+ * @returns A promise that resolves when all tasks are done.
56
+ * @example
57
+ * ```typescript
58
+ * await queue.waitForFinish('longTaskId');
59
+ * ```
60
+ */
61
+ waitForFinish(taskId: string): Promise<unknown>;
45
62
  }
46
63
  //# sourceMappingURL=main.d.ts.map
@@ -1 +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"}
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,CAAoC;IAEnD;;;;;;;;;;;;;;;;OAgBG;IACG,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IA0BjE;;;;;;;;;;;OAWG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAIlC;;;;;;;;;OASG;IACH,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAGhD"}
package/dist/main.mjs CHANGED
@@ -1,3 +1,3 @@
1
- /* @alwatr/async-queue v1.0.2 */
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};
1
+ /* @alwatr/async-queue v1.1.0 */
2
+ var m=Object.defineProperty;var a=(r,e,i)=>e in r?m(r,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):r[e]=i;var o=(r,e,i)=>(a(r,typeof e!="symbol"?e+"":e,i),i);import{newFlatomise as h}from"@alwatr/flatomise";var s=class{constructor(){o(this,"e",{})}async push(e,i){let t=h(),n=this.e[e];this.e[e]=t.promise;try{await n}catch{}return setTimeout(()=>{i().then(t.resolve,t.reject).then(()=>{this.e[e]===t.promise&&delete this.e[e]})},0),t.promise}isRunning(e){return this.e[e]!==void 0}waitForFinish(e){return this.e[e]??Promise.resolve()}};export{s as AsyncQueue};
3
3
  //# sourceMappingURL=main.mjs.map
package/dist/main.mjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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",
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<unknown>> = {};\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<T>(taskId: string, task: () => Promise<T>): Promise<T> {\n const flatomise = newFlatomise<T>();\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()\n .then(flatomise.resolve, flatomise.reject)\n .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 * @example\n * ```typescript\n * if (queue.isRunning('longTaskId')) {\n * // ...\n * }\n * ```\n */\n isRunning(taskId: string): boolean {\n return this.queue__[taskId] !== undefined;\n }\n\n /**\n * Wait for the all tasks in the queue to finish.\n *\n * @param taskId task id\n * @returns A promise that resolves when all tasks are done.\n * @example\n * ```typescript\n * await queue.waitForFinish('longTaskId');\n * ```\n */\n waitForFinish(taskId: string): Promise<unknown> {\n return this.queue__[taskId] ?? Promise.resolve();\n }\n}\n"],
5
+ "mappings": ";wKAAA,OAAQ,gBAAAA,MAAmB,oBAkBpB,IAAMC,EAAN,KAAiB,CAAjB,cAILC,EAAA,KAAQC,IAAwC,CAAC,GAmBjD,MAAM,KAAQC,EAAgBC,EAAoC,CAChE,IAAMC,EAAYN,EAAgB,EAE5BO,EAAsB,KAAKJ,EAAQC,CAAM,EAC/C,KAAKD,EAAQC,CAAM,EAAIE,EAAU,QAEjC,GAAI,CACF,MAAMC,CACR,MACW,CAEX,CAEA,kBAAW,IAAM,CACfF,EAAK,EACF,KAAKC,EAAU,QAASA,EAAU,MAAM,EACxC,KAAK,IAAM,CACN,KAAKH,EAAQC,CAAM,IAAME,EAAU,SACrC,OAAO,KAAKH,EAAQC,CAAM,CAE9B,CAAC,CACL,EAAG,CAAC,EAEGE,EAAU,OACnB,CAcA,UAAUF,EAAyB,CACjC,OAAO,KAAKD,EAAQC,CAAM,IAAM,MAClC,CAYA,cAAcA,EAAkC,CAC9C,OAAO,KAAKD,EAAQC,CAAM,GAAK,QAAQ,QAAQ,CACjD,CACF",
6
6
  "names": ["newFlatomise", "AsyncQueue", "__publicField", "queue__", "taskId", "task", "flatomise", "previousTaskPromise"]
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alwatr/async-queue",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "A queue that executes async tasks in order like mutex and semaphore methodology for javascript and typescript.",
5
5
  "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
6
6
  "keywords": [
@@ -72,7 +72,7 @@
72
72
  "clean": "rm -rfv dist *.tsbuildinfo"
73
73
  },
74
74
  "dependencies": {
75
- "@alwatr/flatomise": "^1.0.2"
75
+ "@alwatr/flatomise": "^1.1.0"
76
76
  },
77
77
  "devDependencies": {
78
78
  "@alwatr/nano-build": "^1.3.0",
@@ -82,5 +82,5 @@
82
82
  "@types/node": "^20.10.7",
83
83
  "typescript": "^5.3.3"
84
84
  },
85
- "gitHead": "5bbfd315996b6052e35b42b2d00b9bef1249ab16"
85
+ "gitHead": "ef1fb8f170f5eed1449b56d99decdf8397844c9d"
86
86
  }