@telperion/ts-utils 0.0.0-stage → 1.1.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.
@@ -0,0 +1,26 @@
1
+ {
2
+ "schemaVersion": 1,
3
+ "name": "ts-utils",
4
+ "version": "0.0.0",
5
+ "formats": [
6
+ "esm",
7
+ "esmFlat",
8
+ "cjs",
9
+ "dts",
10
+ "dcts"
11
+ ],
12
+ "entries": [
13
+ {
14
+ "name": "index",
15
+ "sourcePath": "src/index.ts",
16
+ "outputs": {
17
+ "esm": "index.js",
18
+ "esmFlat": "fesm/ts-utils.mjs",
19
+ "cjs": "index.cjs",
20
+ "dts": "index.d.ts",
21
+ "dcts": "index.d.cts"
22
+ }
23
+ }
24
+ ],
25
+ "generatedAt": "2026-11-18T14:29:31.773Z"
26
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Telperion Technology
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 CHANGED
@@ -1,3 +1,52 @@
1
- # Temporary Holding Version
1
+ # @telperion/ts-utils
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm version](https://badge.fury.io/js/%40telperion%2Fts-utils.svg)](https://badge.fury.io/js/%40telperion%2Fts-utils)
4
+ [![Build Status](https://github.com/telperiontech/telperion/actions/workflows/ts-utils.publish.yml/badge.svg)](https://github.com/telperiontech/telperion/actions/workflows/ts-utils.publish.yml)
5
+ [![npm downloads](https://img.shields.io/npm/dm/@telperion/ts-utils.svg)](https://www.npmjs.com/package/@telperion/ts-utils)
6
+
7
+ Decorators and helpers for TypeScript classes, built on top of [@telperion/js-utils](https://www.npmjs.com/package/@telperion/js-utils).
8
+
9
+ Full reference: [open-source.telperion.tr/ts-utils](https://open-source.telperion.tr/ts-utils)
10
+
11
+ > Formerly published as `@thalesrc/ts-utils`.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install @telperion/ts-utils
17
+ ```
18
+
19
+ ## Features
20
+
21
+ - **FifoPromise**: Method decorator that queues calls to an async method and runs them one at a time, first in first out, with optional delay, shared queues and timeout
22
+
23
+ ## Usage
24
+
25
+ ```typescript
26
+ import { FifoPromise } from '@telperion/ts-utils/fifo-promise';
27
+
28
+ class Api {
29
+ @FifoPromise({ delay: 100, timeout: 5000 })
30
+ async save(data: Data) {
31
+ await fetch('/save', { method: 'POST', body: JSON.stringify(data) });
32
+ }
33
+ }
34
+
35
+ const api = new Api();
36
+
37
+ api.save(a);
38
+ api.save(b); // starts 100ms after the first save settles
39
+ ```
40
+
41
+ Pass the same `group` object to several decorated methods to serialize them through one queue. A call that runs longer than `timeout` is rejected with a `TimeoutError`.
42
+
43
+ ## Development
44
+
45
+ ```bash
46
+ nx build ts-utils # build the library
47
+ nx test ts-utils # run the unit tests
48
+ ```
49
+
50
+ ## License
51
+
52
+ MIT
@@ -0,0 +1,88 @@
1
+ import { SmartMap } from '@telperion/js-utils/smart-map';
2
+ import { call } from '@telperion/js-utils/function/call';
3
+ import { timeout } from '@telperion/js-utils/promise/timeout';
4
+
5
+ /* eslint-disable @typescript-eslint/no-explicit-any */
6
+ const LAST_JOB_MAP = new SmartMap();
7
+ /**
8
+ * ### TimeoutError
9
+ *
10
+ * Rejection reason of a `FifoPromise` call that ran longer than its `timeout`
11
+ */
12
+ class TimeoutError extends Error {
13
+ }
14
+ /**
15
+ * ### FifoPromise
16
+ *
17
+ * Method decorator that queues calls to an async method and runs them one at a time, first in first out
18
+ *
19
+ * Each call waits for the previous one in its queue to settle (resolved or rejected), then for `delay`
20
+ * milliseconds, before it starts. Pass the same `group` to several methods to serialize them together.
21
+ * With `timeout`, a call that runs too long is rejected with a `TimeoutError` and the queue moves on;
22
+ * the original method itself is not cancelled.
23
+ *
24
+ * * * *
25
+ * Example usage:
26
+ * ```typescript
27
+ * import { FifoPromise } from "@telperion/ts-utils/fifo-promise";
28
+ *
29
+ * const STORAGE = {};
30
+ *
31
+ * class Api {
32
+ * @FifoPromise({ delay: 100, timeout: 5000 })
33
+ * async save(data: Data) {
34
+ * await fetch("/save", { method: "POST", body: JSON.stringify(data) });
35
+ * }
36
+ *
37
+ * @FifoPromise({ group: STORAGE })
38
+ * async read() { ... }
39
+ *
40
+ * @FifoPromise({ group: STORAGE })
41
+ * async write(value: string) { ... }
42
+ * }
43
+ *
44
+ * const api = new Api();
45
+ *
46
+ * api.save(a);
47
+ * api.save(b); // starts 100ms after the first save settles
48
+ * ```
49
+ * * * *
50
+ * @param options Queue delay, group and timeout
51
+ * @returns A method decorator
52
+ */
53
+ function FifoPromise({ delay = 0, group = {}, timeout: timeout$1 } = {}) {
54
+ return function (target, propertyKey, descriptor) {
55
+ const originalMethod = descriptor.value;
56
+ let lastJobObj = LAST_JOB_MAP.get(group);
57
+ if (!lastJobObj)
58
+ LAST_JOB_MAP.set(group, lastJobObj = { lastJob: Promise.resolve() });
59
+ descriptor.value = async function (...args) {
60
+ return lastJobObj.lastJob = new Promise((resolve, reject) => {
61
+ lastJobObj.lastJob.finally(() => {
62
+ let timeoutKey;
63
+ timeout(delay)
64
+ .then(() => {
65
+ const tasks = [() => originalMethod.call(this, ...args)];
66
+ if (timeout$1 != null) {
67
+ timeoutKey = Symbol();
68
+ tasks.push(() => timeout(timeout$1, undefined, timeoutKey).then(() => {
69
+ throw new TimeoutError('Fifo Promise timeout');
70
+ }));
71
+ }
72
+ return Promise.race(tasks.map(call));
73
+ })
74
+ .then(value => {
75
+ if (timeoutKey) {
76
+ timeout.cancel(timeoutKey);
77
+ }
78
+ resolve(value);
79
+ })
80
+ .catch(reject);
81
+ });
82
+ });
83
+ };
84
+ };
85
+ }
86
+
87
+ export { FifoPromise, TimeoutError };
88
+ //# sourceMappingURL=ts-utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ts-utils.mjs","sources":["../../../../libs/ts-utils/src/fifo-promise.ts"],"sourcesContent":[null],"names":["timeout","jsTimeout"],"mappings":";;;;AAAA;AAMA,MAAM,YAAY,GAAG,IAAI,QAAQ,EAAkC;AAEnE;;;;AAIG;AACG,MAAO,YAAa,SAAQ,KAAK,CAAA;AAAI;AAgB3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCG;AACG,SAAU,WAAW,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,EAAE,WAAEA,SAAO,KAAuB,EAAE,EAAA;AACnF,IAAA,OAAO,UAAU,MAAW,EAAE,WAAmB,EAAE,UAA8B,EAAA;AAC/E,QAAA,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK;QACvC,IAAI,UAAU,GAAG,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC;AAExC,QAAA,IAAI,CAAC,UAAU;AAAE,YAAA,YAAY,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;AAErF,QAAA,UAAU,CAAC,KAAK,GAAG,gBAAgB,GAAG,IAAW,EAAA;AAC/C,YAAA,OAAO,UAAU,CAAC,OAAO,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAI;AAC1D,gBAAA,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,MAAK;AAC9B,oBAAA,IAAI,UAAkB;oBAEtBC,OAAS,CAAC,KAAK;yBACZ,IAAI,CAAC,MAAK;AACT,wBAAA,MAAM,KAAK,GAAG,CAAC,MAAM,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;AAExD,wBAAA,IAAID,SAAO,IAAI,IAAI,EAAE;4BACnB,UAAU,GAAG,MAAM,EAAE;AACrB,4BAAA,KAAK,CAAC,IAAI,CAAC,MAAMC,OAAS,CAACD,SAAO,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC,IAAI,CAAC,MAAK;AACnE,gCAAA,MAAM,IAAI,YAAY,CAAC,sBAAsB,CAAC;4BAChD,CAAC,CAAC,CAAC;wBACL;wBAEA,OAAO,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AACtC,oBAAA,CAAC;yBACA,IAAI,CAAC,KAAK,IAAG;wBACZ,IAAI,UAAU,EAAE;AACd,4BAAAC,OAAS,CAAC,MAAM,CAAC,UAAU,CAAC;wBAC9B;wBAEA,OAAO,CAAC,KAAK,CAAC;AAChB,oBAAA,CAAC;yBACA,KAAK,CAAC,MAAM,CAAC;AAClB,gBAAA,CAAC,CAAC;AACJ,YAAA,CAAC,CAAC;AACJ,QAAA,CAAC;AACH,IAAA,CAAC;AACH;;;;"}
@@ -0,0 +1,91 @@
1
+ 'use strict';
2
+
3
+ var smartMap = require('@telperion/js-utils/smart-map');
4
+ var call = require('@telperion/js-utils/function/call');
5
+ var timeout = require('@telperion/js-utils/promise/timeout');
6
+
7
+ /* eslint-disable @typescript-eslint/no-explicit-any */
8
+ const LAST_JOB_MAP = new smartMap.SmartMap();
9
+ /**
10
+ * ### TimeoutError
11
+ *
12
+ * Rejection reason of a `FifoPromise` call that ran longer than its `timeout`
13
+ */
14
+ class TimeoutError extends Error {
15
+ }
16
+ /**
17
+ * ### FifoPromise
18
+ *
19
+ * Method decorator that queues calls to an async method and runs them one at a time, first in first out
20
+ *
21
+ * Each call waits for the previous one in its queue to settle (resolved or rejected), then for `delay`
22
+ * milliseconds, before it starts. Pass the same `group` to several methods to serialize them together.
23
+ * With `timeout`, a call that runs too long is rejected with a `TimeoutError` and the queue moves on;
24
+ * the original method itself is not cancelled.
25
+ *
26
+ * * * *
27
+ * Example usage:
28
+ * ```typescript
29
+ * import { FifoPromise } from "@telperion/ts-utils/fifo-promise";
30
+ *
31
+ * const STORAGE = {};
32
+ *
33
+ * class Api {
34
+ * @FifoPromise({ delay: 100, timeout: 5000 })
35
+ * async save(data: Data) {
36
+ * await fetch("/save", { method: "POST", body: JSON.stringify(data) });
37
+ * }
38
+ *
39
+ * @FifoPromise({ group: STORAGE })
40
+ * async read() { ... }
41
+ *
42
+ * @FifoPromise({ group: STORAGE })
43
+ * async write(value: string) { ... }
44
+ * }
45
+ *
46
+ * const api = new Api();
47
+ *
48
+ * api.save(a);
49
+ * api.save(b); // starts 100ms after the first save settles
50
+ * ```
51
+ * * * *
52
+ * @param options Queue delay, group and timeout
53
+ * @returns A method decorator
54
+ */
55
+ function FifoPromise({ delay = 0, group = {}, timeout: timeout$1 } = {}) {
56
+ return function (target, propertyKey, descriptor) {
57
+ const originalMethod = descriptor.value;
58
+ let lastJobObj = LAST_JOB_MAP.get(group);
59
+ if (!lastJobObj)
60
+ LAST_JOB_MAP.set(group, lastJobObj = { lastJob: Promise.resolve() });
61
+ descriptor.value = async function (...args) {
62
+ return lastJobObj.lastJob = new Promise((resolve, reject) => {
63
+ lastJobObj.lastJob.finally(() => {
64
+ let timeoutKey;
65
+ timeout.timeout(delay)
66
+ .then(() => {
67
+ const tasks = [() => originalMethod.call(this, ...args)];
68
+ if (timeout$1 != null) {
69
+ timeoutKey = Symbol();
70
+ tasks.push(() => timeout.timeout(timeout$1, undefined, timeoutKey).then(() => {
71
+ throw new TimeoutError('Fifo Promise timeout');
72
+ }));
73
+ }
74
+ return Promise.race(tasks.map(call.call));
75
+ })
76
+ .then(value => {
77
+ if (timeoutKey) {
78
+ timeout.timeout.cancel(timeoutKey);
79
+ }
80
+ resolve(value);
81
+ })
82
+ .catch(reject);
83
+ });
84
+ });
85
+ };
86
+ };
87
+ }
88
+
89
+ exports.FifoPromise = FifoPromise;
90
+ exports.TimeoutError = TimeoutError;
91
+ //# sourceMappingURL=fifo-promise.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fifo-promise.cjs","sources":["../../../libs/ts-utils/src/fifo-promise.ts"],"sourcesContent":[null],"names":["SmartMap","timeout","jsTimeout","call"],"mappings":";;;;;;AAAA;AAMA,MAAM,YAAY,GAAG,IAAIA,iBAAQ,EAAkC;AAEnE;;;;AAIG;AACG,MAAO,YAAa,SAAQ,KAAK,CAAA;AAAI;AAgB3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCG;AACG,SAAU,WAAW,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,EAAE,WAAEC,SAAO,KAAuB,EAAE,EAAA;AACnF,IAAA,OAAO,UAAU,MAAW,EAAE,WAAmB,EAAE,UAA8B,EAAA;AAC/E,QAAA,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK;QACvC,IAAI,UAAU,GAAG,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC;AAExC,QAAA,IAAI,CAAC,UAAU;AAAE,YAAA,YAAY,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;AAErF,QAAA,UAAU,CAAC,KAAK,GAAG,gBAAgB,GAAG,IAAW,EAAA;AAC/C,YAAA,OAAO,UAAU,CAAC,OAAO,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAI;AAC1D,gBAAA,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,MAAK;AAC9B,oBAAA,IAAI,UAAkB;oBAEtBC,eAAS,CAAC,KAAK;yBACZ,IAAI,CAAC,MAAK;AACT,wBAAA,MAAM,KAAK,GAAG,CAAC,MAAM,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;AAExD,wBAAA,IAAID,SAAO,IAAI,IAAI,EAAE;4BACnB,UAAU,GAAG,MAAM,EAAE;AACrB,4BAAA,KAAK,CAAC,IAAI,CAAC,MAAMC,eAAS,CAACD,SAAO,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC,IAAI,CAAC,MAAK;AACnE,gCAAA,MAAM,IAAI,YAAY,CAAC,sBAAsB,CAAC;4BAChD,CAAC,CAAC,CAAC;wBACL;wBAEA,OAAO,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAACE,SAAI,CAAC,CAAC;AACtC,oBAAA,CAAC;yBACA,IAAI,CAAC,KAAK,IAAG;wBACZ,IAAI,UAAU,EAAE;AACd,4BAAAD,eAAS,CAAC,MAAM,CAAC,UAAU,CAAC;wBAC9B;wBAEA,OAAO,CAAC,KAAK,CAAC;AAChB,oBAAA,CAAC;yBACA,KAAK,CAAC,MAAM,CAAC;AAClB,gBAAA,CAAC,CAAC;AACJ,YAAA,CAAC,CAAC;AACJ,QAAA,CAAC;AACH,IAAA,CAAC;AACH;;;;;"}
@@ -0,0 +1,88 @@
1
+ import { SmartMap } from '@telperion/js-utils/smart-map';
2
+ import { call } from '@telperion/js-utils/function/call';
3
+ import { timeout } from '@telperion/js-utils/promise/timeout';
4
+
5
+ /* eslint-disable @typescript-eslint/no-explicit-any */
6
+ const LAST_JOB_MAP = new SmartMap();
7
+ /**
8
+ * ### TimeoutError
9
+ *
10
+ * Rejection reason of a `FifoPromise` call that ran longer than its `timeout`
11
+ */
12
+ class TimeoutError extends Error {
13
+ }
14
+ /**
15
+ * ### FifoPromise
16
+ *
17
+ * Method decorator that queues calls to an async method and runs them one at a time, first in first out
18
+ *
19
+ * Each call waits for the previous one in its queue to settle (resolved or rejected), then for `delay`
20
+ * milliseconds, before it starts. Pass the same `group` to several methods to serialize them together.
21
+ * With `timeout`, a call that runs too long is rejected with a `TimeoutError` and the queue moves on;
22
+ * the original method itself is not cancelled.
23
+ *
24
+ * * * *
25
+ * Example usage:
26
+ * ```typescript
27
+ * import { FifoPromise } from "@telperion/ts-utils/fifo-promise";
28
+ *
29
+ * const STORAGE = {};
30
+ *
31
+ * class Api {
32
+ * @FifoPromise({ delay: 100, timeout: 5000 })
33
+ * async save(data: Data) {
34
+ * await fetch("/save", { method: "POST", body: JSON.stringify(data) });
35
+ * }
36
+ *
37
+ * @FifoPromise({ group: STORAGE })
38
+ * async read() { ... }
39
+ *
40
+ * @FifoPromise({ group: STORAGE })
41
+ * async write(value: string) { ... }
42
+ * }
43
+ *
44
+ * const api = new Api();
45
+ *
46
+ * api.save(a);
47
+ * api.save(b); // starts 100ms after the first save settles
48
+ * ```
49
+ * * * *
50
+ * @param options Queue delay, group and timeout
51
+ * @returns A method decorator
52
+ */
53
+ function FifoPromise({ delay = 0, group = {}, timeout: timeout$1 } = {}) {
54
+ return function (target, propertyKey, descriptor) {
55
+ const originalMethod = descriptor.value;
56
+ let lastJobObj = LAST_JOB_MAP.get(group);
57
+ if (!lastJobObj)
58
+ LAST_JOB_MAP.set(group, lastJobObj = { lastJob: Promise.resolve() });
59
+ descriptor.value = async function (...args) {
60
+ return lastJobObj.lastJob = new Promise((resolve, reject) => {
61
+ lastJobObj.lastJob.finally(() => {
62
+ let timeoutKey;
63
+ timeout(delay)
64
+ .then(() => {
65
+ const tasks = [() => originalMethod.call(this, ...args)];
66
+ if (timeout$1 != null) {
67
+ timeoutKey = Symbol();
68
+ tasks.push(() => timeout(timeout$1, undefined, timeoutKey).then(() => {
69
+ throw new TimeoutError('Fifo Promise timeout');
70
+ }));
71
+ }
72
+ return Promise.race(tasks.map(call));
73
+ })
74
+ .then(value => {
75
+ if (timeoutKey) {
76
+ timeout.cancel(timeoutKey);
77
+ }
78
+ resolve(value);
79
+ })
80
+ .catch(reject);
81
+ });
82
+ });
83
+ };
84
+ };
85
+ }
86
+
87
+ export { FifoPromise, TimeoutError };
88
+ //# sourceMappingURL=fifo-promise.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fifo-promise.js","sources":["../../../libs/ts-utils/src/fifo-promise.ts"],"sourcesContent":[null],"names":["timeout","jsTimeout"],"mappings":";;;;AAAA;AAMA,MAAM,YAAY,GAAG,IAAI,QAAQ,EAAkC;AAEnE;;;;AAIG;AACG,MAAO,YAAa,SAAQ,KAAK,CAAA;AAAI;AAgB3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCG;AACG,SAAU,WAAW,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,EAAE,WAAEA,SAAO,KAAuB,EAAE,EAAA;AACnF,IAAA,OAAO,UAAU,MAAW,EAAE,WAAmB,EAAE,UAA8B,EAAA;AAC/E,QAAA,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK;QACvC,IAAI,UAAU,GAAG,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC;AAExC,QAAA,IAAI,CAAC,UAAU;AAAE,YAAA,YAAY,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;AAErF,QAAA,UAAU,CAAC,KAAK,GAAG,gBAAgB,GAAG,IAAW,EAAA;AAC/C,YAAA,OAAO,UAAU,CAAC,OAAO,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAI;AAC1D,gBAAA,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,MAAK;AAC9B,oBAAA,IAAI,UAAkB;oBAEtBC,OAAS,CAAC,KAAK;yBACZ,IAAI,CAAC,MAAK;AACT,wBAAA,MAAM,KAAK,GAAG,CAAC,MAAM,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;AAExD,wBAAA,IAAID,SAAO,IAAI,IAAI,EAAE;4BACnB,UAAU,GAAG,MAAM,EAAE;AACrB,4BAAA,KAAK,CAAC,IAAI,CAAC,MAAMC,OAAS,CAACD,SAAO,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC,IAAI,CAAC,MAAK;AACnE,gCAAA,MAAM,IAAI,YAAY,CAAC,sBAAsB,CAAC;4BAChD,CAAC,CAAC,CAAC;wBACL;wBAEA,OAAO,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AACtC,oBAAA,CAAC;yBACA,IAAI,CAAC,KAAK,IAAG;wBACZ,IAAI,UAAU,EAAE;AACd,4BAAAC,OAAS,CAAC,MAAM,CAAC,UAAU,CAAC;wBAC9B;wBAEA,OAAO,CAAC,KAAK,CAAC;AAChB,oBAAA,CAAC;yBACA,KAAK,CAAC,MAAM,CAAC;AAClB,gBAAA,CAAC,CAAC;AACJ,YAAA,CAAC,CAAC;AACJ,QAAA,CAAC;AACH,IAAA,CAAC;AACH;;;;"}
package/index.cjs ADDED
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ var fifoPromise = require('./fifo-promise.cjs');
4
+
5
+
6
+
7
+ exports.FifoPromise = fifoPromise.FifoPromise;
8
+ exports.TimeoutError = fifoPromise.TimeoutError;
9
+ //# sourceMappingURL=index.cjs.map
package/index.cjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
package/index.d.cts ADDED
@@ -0,0 +1,63 @@
1
+ /**
2
+ * ### TimeoutError
3
+ *
4
+ * Rejection reason of a `FifoPromise` call that ran longer than its `timeout`
5
+ */
6
+ declare class TimeoutError extends Error {
7
+ }
8
+ /**
9
+ * ### FifoPromiseProps
10
+ *
11
+ * Options of the `FifoPromise` decorator
12
+ */
13
+ interface FifoPromiseProps {
14
+ /** Milliseconds to wait after the previous call settles before starting the next one. Defaults to `0` */
15
+ delay?: number;
16
+ /** Queue key: methods decorated with the same `group` share one queue. Defaults to a queue per decorated method */
17
+ group?: any;
18
+ /** Milliseconds a call may run before it is rejected with a `TimeoutError`. No limit by default */
19
+ timeout?: number;
20
+ }
21
+ /**
22
+ * ### FifoPromise
23
+ *
24
+ * Method decorator that queues calls to an async method and runs them one at a time, first in first out
25
+ *
26
+ * Each call waits for the previous one in its queue to settle (resolved or rejected), then for `delay`
27
+ * milliseconds, before it starts. Pass the same `group` to several methods to serialize them together.
28
+ * With `timeout`, a call that runs too long is rejected with a `TimeoutError` and the queue moves on;
29
+ * the original method itself is not cancelled.
30
+ *
31
+ * * * *
32
+ * Example usage:
33
+ * ```typescript
34
+ * import { FifoPromise } from "@telperion/ts-utils/fifo-promise";
35
+ *
36
+ * const STORAGE = {};
37
+ *
38
+ * class Api {
39
+ * @FifoPromise({ delay: 100, timeout: 5000 })
40
+ * async save(data: Data) {
41
+ * await fetch("/save", { method: "POST", body: JSON.stringify(data) });
42
+ * }
43
+ *
44
+ * @FifoPromise({ group: STORAGE })
45
+ * async read() { ... }
46
+ *
47
+ * @FifoPromise({ group: STORAGE })
48
+ * async write(value: string) { ... }
49
+ * }
50
+ *
51
+ * const api = new Api();
52
+ *
53
+ * api.save(a);
54
+ * api.save(b); // starts 100ms after the first save settles
55
+ * ```
56
+ * * * *
57
+ * @param options Queue delay, group and timeout
58
+ * @returns A method decorator
59
+ */
60
+ declare function FifoPromise({ delay, group, timeout }?: FifoPromiseProps): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
61
+
62
+ export { FifoPromise, TimeoutError };
63
+ export type { FifoPromiseProps };
package/index.d.ts ADDED
@@ -0,0 +1,63 @@
1
+ /**
2
+ * ### TimeoutError
3
+ *
4
+ * Rejection reason of a `FifoPromise` call that ran longer than its `timeout`
5
+ */
6
+ declare class TimeoutError extends Error {
7
+ }
8
+ /**
9
+ * ### FifoPromiseProps
10
+ *
11
+ * Options of the `FifoPromise` decorator
12
+ */
13
+ interface FifoPromiseProps {
14
+ /** Milliseconds to wait after the previous call settles before starting the next one. Defaults to `0` */
15
+ delay?: number;
16
+ /** Queue key: methods decorated with the same `group` share one queue. Defaults to a queue per decorated method */
17
+ group?: any;
18
+ /** Milliseconds a call may run before it is rejected with a `TimeoutError`. No limit by default */
19
+ timeout?: number;
20
+ }
21
+ /**
22
+ * ### FifoPromise
23
+ *
24
+ * Method decorator that queues calls to an async method and runs them one at a time, first in first out
25
+ *
26
+ * Each call waits for the previous one in its queue to settle (resolved or rejected), then for `delay`
27
+ * milliseconds, before it starts. Pass the same `group` to several methods to serialize them together.
28
+ * With `timeout`, a call that runs too long is rejected with a `TimeoutError` and the queue moves on;
29
+ * the original method itself is not cancelled.
30
+ *
31
+ * * * *
32
+ * Example usage:
33
+ * ```typescript
34
+ * import { FifoPromise } from "@telperion/ts-utils/fifo-promise";
35
+ *
36
+ * const STORAGE = {};
37
+ *
38
+ * class Api {
39
+ * @FifoPromise({ delay: 100, timeout: 5000 })
40
+ * async save(data: Data) {
41
+ * await fetch("/save", { method: "POST", body: JSON.stringify(data) });
42
+ * }
43
+ *
44
+ * @FifoPromise({ group: STORAGE })
45
+ * async read() { ... }
46
+ *
47
+ * @FifoPromise({ group: STORAGE })
48
+ * async write(value: string) { ... }
49
+ * }
50
+ *
51
+ * const api = new Api();
52
+ *
53
+ * api.save(a);
54
+ * api.save(b); // starts 100ms after the first save settles
55
+ * ```
56
+ * * * *
57
+ * @param options Queue delay, group and timeout
58
+ * @returns A method decorator
59
+ */
60
+ declare function FifoPromise({ delay, group, timeout }?: FifoPromiseProps): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
61
+
62
+ export { FifoPromise, TimeoutError };
63
+ export type { FifoPromiseProps };
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { FifoPromise, TimeoutError } from './fifo-promise.js';
2
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@telperion/ts-utils",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.1.1",
4
+ "dependencies": {
5
+ "tslib": "^2.3.0",
6
+ "@telperion/js-utils": "^3.8.7"
7
+ },
8
+ "type": "module",
9
+ "module": "./index.js",
10
+ "main": "./index.cjs",
11
+ "typings": "./index.d.ts",
12
+ "bugs": "https://github.com/telperiontech/telperion/issues",
13
+ "license": "MIT",
14
+ "homepage": "https://open-source.telperion.tr",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/telperiontech/telperion.git"
18
+ },
19
+ "author": {
20
+ "name": "Ali Şahin Özçelik",
21
+ "email": "alisahinozcelik@gmail.com",
22
+ "url": "https://alisah.in"
23
+ },
24
+ "funding": {
25
+ "type": "patreon",
26
+ "url": "https://www.patreon.com/alisahin"
27
+ },
28
+ "sideEffects": false,
29
+ "exports": {
30
+ ".": {
31
+ "types": {
32
+ "import": "./index.d.ts",
33
+ "require": "./index.d.cts"
34
+ },
35
+ "import": "./index.js",
36
+ "require": "./index.cjs",
37
+ "node": "./index.cjs",
38
+ "default": "./index.js"
39
+ }
40
+ },
41
+ "types": "./index.d.ts"
42
+ }