@keeex/utils 7.2.1 → 7.4.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.
Files changed (60) hide show
  1. package/lib/async/eventqueue.d.ts +60 -0
  2. package/lib/async/eventqueue.js +129 -0
  3. package/lib/async/keycache.d.ts +1 -1
  4. package/lib/async/timecache.d.ts +1 -1
  5. package/lib/benchmark.d.ts +1 -1
  6. package/lib/benchmark.js +2 -2
  7. package/lib/bits/arraybuffer.js +2 -1
  8. package/lib/bits/triggers/base.d.ts +30 -0
  9. package/lib/bits/triggers/base.js +171 -0
  10. package/lib/bits/triggers/debounce.d.ts +30 -0
  11. package/lib/bits/triggers/debounce.js +36 -0
  12. package/lib/bits/triggers/grenade.d.ts +33 -0
  13. package/lib/bits/triggers/grenade.js +37 -0
  14. package/lib/bits/triggers/types.d.ts +43 -0
  15. package/lib/bits/triggers/types.js +30 -0
  16. package/lib/cron.d.ts +2 -2
  17. package/lib/cron.js +1 -1
  18. package/lib/marshalling/marshaller.d.ts +1 -1
  19. package/lib/promise.d.ts +21 -2
  20. package/lib/promise.js +43 -4
  21. package/lib/triggers.d.ts +21 -0
  22. package/lib/triggers.js +21 -0
  23. package/lib/types/array.d.ts +6 -6
  24. package/lib/types/array.js +3 -3
  25. package/lib/types/enum.d.ts +3 -3
  26. package/lib/types/enum.js +2 -2
  27. package/lib/types/record.d.ts +7 -7
  28. package/lib/types/record.js +3 -3
  29. package/lib/types/utils.d.ts +4 -3
  30. package/lib/types/utils.js +13 -6
  31. package/package.json +1 -1
  32. package/web/async/eventqueue.d.ts +60 -0
  33. package/web/async/eventqueue.js +126 -0
  34. package/web/async/keycache.d.ts +1 -1
  35. package/web/async/timecache.d.ts +1 -1
  36. package/web/benchmark.d.ts +1 -1
  37. package/web/benchmark.js +2 -2
  38. package/web/bits/arraybuffer.js +3 -1
  39. package/web/bits/triggers/base.d.ts +30 -0
  40. package/web/bits/triggers/base.js +173 -0
  41. package/web/bits/triggers/debounce.d.ts +30 -0
  42. package/web/bits/triggers/debounce.js +36 -0
  43. package/web/bits/triggers/grenade.d.ts +33 -0
  44. package/web/bits/triggers/grenade.js +37 -0
  45. package/web/bits/triggers/types.d.ts +43 -0
  46. package/web/bits/triggers/types.js +30 -0
  47. package/web/cron.d.ts +2 -2
  48. package/web/marshalling/marshaller.d.ts +1 -1
  49. package/web/promise.d.ts +21 -2
  50. package/web/promise.js +37 -4
  51. package/web/triggers.d.ts +21 -0
  52. package/web/triggers.js +21 -0
  53. package/web/types/array.d.ts +6 -6
  54. package/web/types/array.js +3 -3
  55. package/web/types/enum.d.ts +3 -3
  56. package/web/types/enum.js +2 -2
  57. package/web/types/record.d.ts +7 -7
  58. package/web/types/record.js +3 -3
  59. package/web/types/utils.d.ts +4 -3
  60. package/web/types/utils.js +6 -6
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @license
3
+ * @preserve
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2023 KeeeX SAS
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
+ *
15
+ */
16
+ export var TriggerState;
17
+ (function (TriggerState) {
18
+ /** Trigger was not triggered */
19
+ TriggerState["inactive"] = "inactive";
20
+ /** Trigger was triggered, fuse timer is counting down */
21
+ TriggerState["pending"] = "pending";
22
+ /** Trigger was triggered and timer expired, the handler is running */
23
+ TriggerState["running"] = "running";
24
+ /** Handler is running, and trigger was called again */
25
+ TriggerState["runningRetrigger"] = "runningTrigger";
26
+ /** Last run after stop */
27
+ TriggerState["runningStop"] = "runningStop";
28
+ /** Handler is killed, will never run except for currently triggered call */
29
+ TriggerState["stop"] = "stop";
30
+ })(TriggerState || (TriggerState = {}));
package/lib/cron.d.ts CHANGED
@@ -13,9 +13,9 @@
13
13
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
14
  *
15
15
  */
16
- import { Logger } from "@keeex/log";
16
+ import { type Logger } from "@keeex/log";
17
17
  import ScheduledTask from "./cron/scheduledtask.js";
18
- import { TaskFunction, TaskOptions, Overrun, CompleteTaskOptions } from "./cron/types.js";
18
+ import { type TaskFunction, type TaskOptions, Overrun, type CompleteTaskOptions } from "./cron/types.js";
19
19
  export { Overrun };
20
20
  export type { ScheduledTask, TaskFunction, TaskOptions, CompleteTaskOptions };
21
21
  /**
package/lib/cron.js CHANGED
@@ -14,7 +14,7 @@
14
14
  *
15
15
  */
16
16
  import ScheduledTask from "./cron/scheduledtask.js";
17
- import { Overrun } from "./cron/types.js";
17
+ import { Overrun, } from "./cron/types.js";
18
18
  const scheduledTasks = [];
19
19
  export { Overrun };
20
20
  /**
@@ -23,7 +23,7 @@ export default class Marshaller {
23
23
  private constructor();
24
24
  static readonly start: (magicNumbers?: string) => Marshaller;
25
25
  /** End building the buffer and returns the result */
26
- readonly end: () => ArrayBuffer;
26
+ readonly end: () => Uint8Array;
27
27
  /** Append an utf-8 string */
28
28
  readonly str: (value: string) => this;
29
29
  /** Append a 32-bit unsigned int */
package/lib/promise.d.ts CHANGED
@@ -13,7 +13,7 @@
13
13
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
14
  *
15
15
  */
16
- import { Awaitable } from "./types/types.js";
16
+ import { type Awaitable } from "./types/types.js";
17
17
  /**
18
18
  * Create a promise that resolves after a given delay.
19
19
  *
@@ -36,10 +36,29 @@ export type PromiseFunc<T> = () => Promise<T | void>;
36
36
  */
37
37
  export declare const firstTruthy: <T>(promiseFuncs: Array<PromiseFunc<T>>) => Promise<T>;
38
38
  type AwaitableFn<T> = () => Awaitable<T>;
39
+ type ErrorHandler = boolean | ((e: Error) => void) | {
40
+ error: (e: unknown) => void;
41
+ };
39
42
  /**
40
43
  * Drop a promise and silence any exception.
41
44
  *
42
45
  * This is to be used *only* on dropped promises to explicitly silence any warning.
46
+ *
47
+ * @param errorHandler - How to handle rejection
48
+ * If not provided or true, an error message is printed on the error output.
49
+ * If false, the error is silently ignored.
50
+ * If a function is provided, it is called with an Error object.
51
+ * If an object with an `error()` function is provided, this error function is called.
52
+ */
53
+ export declare const dropPromise: <T>(promise: Awaitable<T> | AwaitableFn<T>, errorHandler?: ErrorHandler) => void;
54
+ /**
55
+ * Wait for a promise to complete, but ignore rejection.
56
+
57
+ * @param errorHandler - How to handle rejection
58
+ * If not provided or false, the error is silently ignored.
59
+ * If true, an error message is printed on the error output.
60
+ * If a function is provided, it is called with an Error object.
61
+ * If an object with an `error()` function is provided, this error function is called.
43
62
  */
44
- export declare const dropPromise: <T>(promise: Awaitable<T> | AwaitableFn<T>) => void;
63
+ export declare const ignorePromise: <T>(promise: Awaitable<T> | AwaitableFn<T>, errorHandler?: ErrorHandler) => Promise<void>;
45
64
  export {};
package/lib/promise.js CHANGED
@@ -15,6 +15,7 @@
15
15
  */
16
16
  /* eslint-env node */
17
17
  import * as log from "@keeex/log";
18
+ import { asError } from "./error.js";
18
19
  import { timeConvert } from "./units.js";
19
20
  /**
20
21
  * Create a promise that resolves after a given delay.
@@ -69,20 +70,58 @@ export const firstTruthy = async (promiseFuncs) => {
69
70
  throw new Error("No result");
70
71
  };
71
72
  const logger = log.createLogger("dropPromise");
73
+ const handleError = (handler, outputDefault, error) => {
74
+ if (handler === undefined) {
75
+ if (outputDefault)
76
+ logger.error("Dropped promise rejected:", error);
77
+ }
78
+ else if (handler === true) {
79
+ logger.error("Dropped promise rejected:", error);
80
+ }
81
+ else if (typeof handler === "function") {
82
+ handler(asError(error));
83
+ }
84
+ else if (handler !== false) {
85
+ handler.error(error);
86
+ }
87
+ };
72
88
  /**
73
89
  * Drop a promise and silence any exception.
74
90
  *
75
91
  * This is to be used *only* on dropped promises to explicitly silence any warning.
92
+ *
93
+ * @param errorHandler - How to handle rejection
94
+ * If not provided or true, an error message is printed on the error output.
95
+ * If false, the error is silently ignored.
96
+ * If a function is provided, it is called with an Error object.
97
+ * If an object with an `error()` function is provided, this error function is called.
76
98
  */
77
- export const dropPromise = (promise) => {
99
+ export const dropPromise = (promise, errorHandler) => {
78
100
  if (typeof promise === "function") {
79
101
  dropPromise(promise());
80
102
  return;
81
103
  }
82
104
  if (promise instanceof Promise) {
83
105
  // eslint-disable-next-line promise/prefer-await-to-then
84
- promise.catch((e) => {
85
- logger.error("Dropped promise rejected:", e);
86
- });
106
+ promise.catch((e) => handleError(errorHandler, true, e));
107
+ }
108
+ };
109
+ /**
110
+ * Wait for a promise to complete, but ignore rejection.
111
+
112
+ * @param errorHandler - How to handle rejection
113
+ * If not provided or false, the error is silently ignored.
114
+ * If true, an error message is printed on the error output.
115
+ * If a function is provided, it is called with an Error object.
116
+ * If an object with an `error()` function is provided, this error function is called.
117
+ */
118
+ export const ignorePromise = async (promise, errorHandler) => {
119
+ try {
120
+ if (typeof promise === "function")
121
+ return await ignorePromise(promise());
122
+ await promise;
123
+ }
124
+ catch (e) {
125
+ handleError(errorHandler, false, e);
87
126
  }
88
127
  };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @license
3
+ * @preserve
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2023 KeeeX SAS
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
+ *
15
+ */
16
+ import { type Logger } from "@keeex/log";
17
+ import type * as types from "./bits/triggers/types.js";
18
+ /** Return a simple trigger that is run after the initial delay is passed after a trigger call */
19
+ export declare const simpleTrigger: (handler: types.TriggerHandler, delayMs: number, logger?: Logger) => types.Trigger;
20
+ /** Return a debouncing trigger that will rearm as long as it is triggered during the grace delay */
21
+ export declare const debouncingTrigger: (handler: types.TriggerHandler, delayMs: number, logger?: Logger) => types.Trigger;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @license
3
+ * @preserve
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2023 KeeeX SAS
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
+ *
15
+ */
16
+ import { DebounceTrigger } from "./bits/triggers/debounce.js";
17
+ import { GrenadeTrigger } from "./bits/triggers/grenade.js";
18
+ /** Return a simple trigger that is run after the initial delay is passed after a trigger call */
19
+ export const simpleTrigger = (handler, delayMs, logger) => new GrenadeTrigger(handler, delayMs, logger);
20
+ /** Return a debouncing trigger that will rearm as long as it is triggered during the grace delay */
21
+ export const debouncingTrigger = (handler, delayMs, logger) => new DebounceTrigger(handler, delayMs, logger);
@@ -13,7 +13,7 @@
13
13
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
14
  *
15
15
  */
16
- import { PredicateOptions, TypePredicate } from "./types.js";
16
+ import * as types from "./types.js";
17
17
  /** Shorthand for an object that can be either T or an array of T */
18
18
  export type Arrayable<T> = Array<T> | T;
19
19
  /**
@@ -21,14 +21,14 @@ export type Arrayable<T> = Array<T> | T;
21
21
  *
22
22
  * @public
23
23
  */
24
- export declare const isStringArray: TypePredicate<Array<string>>;
24
+ export declare const isStringArray: types.TypePredicate<Array<string>>;
25
25
  /**
26
26
  * Check that a given object is an array where each value have the given type.
27
27
  *
28
28
  * @public
29
29
  */
30
- export declare const isArrayOfType: <T>(obj: unknown, predicate: TypePredicate<T>, options?: PredicateOptions) => obj is Array<T>;
31
- export declare const isArrayableOfType: <T>(obj: unknown, predicate: TypePredicate<T>, options?: PredicateOptions) => obj is Arrayable<T>;
30
+ export declare const isArrayOfType: <T>(obj: unknown, predicate: types.TypePredicate<T>, options?: types.PredicateOptions) => obj is Array<T>;
31
+ export declare const isArrayableOfType: <T>(obj: unknown, predicate: types.TypePredicate<T>, options?: types.PredicateOptions) => obj is Arrayable<T>;
32
32
  /** @public */
33
- export declare const makeArrayOfTypePredicate: <ValueType>(valuePredicate: TypePredicate<ValueType>) => TypePredicate<Array<ValueType>>;
34
- export declare const makeArrayableOfTypePredicate: <ValueType>(valuePredicate: TypePredicate<ValueType>) => TypePredicate<Arrayable<ValueType>>;
33
+ export declare const makeArrayOfTypePredicate: <ValueType>(valuePredicate: types.TypePredicate<ValueType>) => types.TypePredicate<Array<ValueType>>;
34
+ export declare const makeArrayableOfTypePredicate: <ValueType>(valuePredicate: types.TypePredicate<ValueType>) => types.TypePredicate<Arrayable<ValueType>>;
@@ -14,7 +14,7 @@
14
14
  *
15
15
  */
16
16
  import { PredicateError } from "./predicateerror.js";
17
- import { getPredicateOptions } from "./types.js";
17
+ import * as types from "./types.js";
18
18
  // #endregion
19
19
  /**
20
20
  * Check that a given object is an array of string
@@ -22,7 +22,7 @@ import { getPredicateOptions } from "./types.js";
22
22
  * @public
23
23
  */
24
24
  export const isStringArray = (obj, options = false) => {
25
- const opt = getPredicateOptions(options);
25
+ const opt = types.getPredicateOptions(options);
26
26
  if (!Array.isArray(obj)) {
27
27
  PredicateError.expected(opt.raise, undefined, "an array");
28
28
  return false;
@@ -41,7 +41,7 @@ export const isStringArray = (obj, options = false) => {
41
41
  * @public
42
42
  */
43
43
  export const isArrayOfType = (obj, predicate, options = false) => {
44
- const opt = getPredicateOptions(options);
44
+ const opt = types.getPredicateOptions(options);
45
45
  if (!Array.isArray(obj)) {
46
46
  PredicateError.expected(opt.raise, undefined, "an array");
47
47
  return false;
@@ -13,7 +13,7 @@
13
13
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
14
  *
15
15
  */
16
- import { PredicateOptions, TypePredicate } from "./types.js";
16
+ import * as types from "./types.js";
17
17
  /**
18
18
  * Return an enum-type value whose key matches the provided key string.
19
19
  *
@@ -25,6 +25,6 @@ export declare const keyToEnum: <EnumType>(key: string, enumType: Record<string,
25
25
  *
26
26
  * @public
27
27
  */
28
- export declare const isEnumType: <EnumType>(obj: unknown, enumType: unknown, options?: PredicateOptions) => obj is EnumType;
28
+ export declare const isEnumType: <EnumType>(obj: unknown, enumType: unknown, options?: types.PredicateOptions) => obj is EnumType;
29
29
  /** @public */
30
- export declare const makeEnumTypePredicate: <EnumType>(enumType: unknown) => TypePredicate<EnumType>;
30
+ export declare const makeEnumTypePredicate: <EnumType>(enumType: unknown) => types.TypePredicate<EnumType>;
package/lib/types/enum.js CHANGED
@@ -14,7 +14,7 @@
14
14
  *
15
15
  */
16
16
  import { PredicateError } from "./predicateerror.js";
17
- import { getPredicateOptions } from "./types.js";
17
+ import * as types from "./types.js";
18
18
  /**
19
19
  * Return an enum-type value whose key matches the provided key string.
20
20
  *
@@ -34,7 +34,7 @@ export const keyToEnum = (key, enumType) => {
34
34
  * @public
35
35
  */
36
36
  export const isEnumType = (obj, enumType, options = false) => {
37
- const opt = getPredicateOptions(options);
37
+ const opt = types.getPredicateOptions(options);
38
38
  const res = Object.values(enumType).includes(obj);
39
39
  if (!res)
40
40
  PredicateError.expected(opt.raise, undefined, "value from enum");
@@ -13,7 +13,7 @@
13
13
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
14
  *
15
15
  */
16
- import { PredicateOptions, TypePredicate } from "./types.js";
16
+ import * as types from "./types.js";
17
17
  interface ProfileValue<T = unknown> {
18
18
  value: T;
19
19
  optional?: boolean;
@@ -25,12 +25,12 @@ interface InstanceOfValue {
25
25
  shortcut?: boolean;
26
26
  }
27
27
  interface PredicateValue<T = unknown> {
28
- predicate: TypePredicate<T>;
28
+ predicate: types.TypePredicate<T>;
29
29
  optional?: boolean;
30
30
  shortcut?: boolean;
31
31
  }
32
32
  type AdvancedProfileValue = ProfileValue | InstanceOfValue | PredicateValue;
33
- type ProfileEntryType = string | TypePredicate | AdvancedProfileValue;
33
+ type ProfileEntryType = string | types.TypePredicate | AdvancedProfileValue;
34
34
  /**
35
35
  * Simple profile to check.
36
36
  *
@@ -49,19 +49,19 @@ export type Profile<T> = Record<keyof T, ProfileEntryType | Array<ProfileEntryTy
49
49
  *
50
50
  * @public
51
51
  */
52
- export declare const isObjectProfile: <T>(obj: unknown, profile: Profile<T>, options?: PredicateOptions) => obj is T;
52
+ export declare const isObjectProfile: <T>(obj: unknown, profile: Profile<T>, options?: types.PredicateOptions) => obj is T;
53
53
  /**
54
54
  * Create a predicate function based on the given profile.
55
55
  *
56
56
  * @public
57
57
  */
58
- export declare const makeProfilePredicate: <TargetType>(profile: Profile<TargetType>) => TypePredicate<TargetType>;
58
+ export declare const makeProfilePredicate: <TargetType>(profile: Profile<TargetType>) => types.TypePredicate<TargetType>;
59
59
  /**
60
60
  * Check if all of an object properties match a given type.
61
61
  *
62
62
  * @public
63
63
  */
64
- export declare const isKeyValueOfType: <ValueType>(obj: unknown, typePredicate: TypePredicate<ValueType>, options?: PredicateOptions) => obj is Record<string, ValueType>;
64
+ export declare const isKeyValueOfType: <ValueType>(obj: unknown, typePredicate: types.TypePredicate<ValueType>, options?: types.PredicateOptions) => obj is Record<string, ValueType>;
65
65
  /** @public */
66
- export declare const makeKeyValueOfTypePredicate: <ValueType>(typePredicate: TypePredicate<ValueType>) => TypePredicate<Record<string, ValueType>>;
66
+ export declare const makeKeyValueOfTypePredicate: <ValueType>(typePredicate: types.TypePredicate<ValueType>) => types.TypePredicate<Record<string, ValueType>>;
67
67
  export {};
@@ -15,7 +15,7 @@
15
15
  */
16
16
  import { asError } from "../error.js";
17
17
  import { PredicateError } from "./predicateerror.js";
18
- import { getPredicateOptions, } from "./types.js";
18
+ import * as types from "./types.js";
19
19
  /**
20
20
  * Extract modifiers from primitive type name
21
21
  *
@@ -180,7 +180,7 @@ const checkArrayTypeDef = (valueType, rec, keyName, options) => {
180
180
  * @public
181
181
  */
182
182
  export const isObjectProfile = (obj, profile, options = false) => {
183
- const opt = getPredicateOptions(options);
183
+ const opt = types.getPredicateOptions(options);
184
184
  if (typeof obj !== "object" || obj === null) {
185
185
  PredicateError.predicateFailed(opt.raise, "root");
186
186
  return false;
@@ -210,7 +210,7 @@ export const makeProfilePredicate = (profile) => (obj, options = false) => isObj
210
210
  * @public
211
211
  */
212
212
  export const isKeyValueOfType = (obj, typePredicate, options = false) => {
213
- const opt = getPredicateOptions(options);
213
+ const opt = types.getPredicateOptions(options);
214
214
  if (typeof obj !== "object") {
215
215
  PredicateError.predicateFailed(opt.raise, undefined);
216
216
  return false;
@@ -13,8 +13,9 @@
13
13
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
14
  *
15
15
  */
16
- import ms from "ms";
16
+ import * as types from "./types.js";
17
+ import type ms from "ms";
17
18
  /** Check that the argument match the string portion of `ms()` */
18
- export declare const isMsStringValue: import("./types.js").TypePredicate<ms.StringValue>;
19
+ export declare const isMsStringValue: types.TypePredicate<ms.StringValue>;
19
20
  /** Check that a value is suitable for input for `ms()` */
20
- export declare const isMsInput: import("./types.js").TypePredicate<number | ms.StringValue>;
21
+ export declare const isMsInput: types.TypePredicate<number | ms.StringValue>;
@@ -14,7 +14,7 @@
14
14
  *
15
15
  */
16
16
  import { isNumber } from "./primitive.js";
17
- import { makeOrPredicate, makeTemplatePredicate } from "./types.js";
17
+ import * as types from "./types.js";
18
18
  /** All `ms()` units, extracted from type definition */
19
19
  const msBaseUnits = [
20
20
  "Years",
@@ -55,14 +55,21 @@ const msUnitAnyCase = Array.from(new Set([
55
55
  ...msBaseUnits.map((c) => c.toUpperCase()),
56
56
  ...msBaseUnits.map((c) => c.toLowerCase()),
57
57
  ]));
58
- const isMsStrInput = makeTemplatePredicate("${number}");
59
- const isMsStrUnitPredicate = makeTemplatePredicate("${number}${msUnitAnyCase}", { msUnitAnyCase });
60
- const isMsSpaceUnitPredicate = makeTemplatePredicate("${number} ${msUnitAnyCase}", { msUnitAnyCase });
58
+ const isMsStrInput = types.makeTemplatePredicate("${number}");
59
+ const isMsStrUnitPredicate = types.makeTemplatePredicate("${number}${msUnitAnyCase}", {
60
+ msUnitAnyCase,
61
+ });
62
+ const isMsSpaceUnitPredicate = types.makeTemplatePredicate("${number} ${msUnitAnyCase}", {
63
+ msUnitAnyCase,
64
+ });
61
65
  /** Check that the argument match the string portion of `ms()` */
62
- export const isMsStringValue = makeOrPredicate([
66
+ export const isMsStringValue = types.makeOrPredicate([
63
67
  isMsStrInput,
64
68
  isMsStrUnitPredicate,
65
69
  isMsSpaceUnitPredicate,
66
70
  ]);
67
71
  /** Check that a value is suitable for input for `ms()` */
68
- export const isMsInput = makeOrPredicate([isNumber, isMsStringValue]);
72
+ export const isMsInput = types.makeOrPredicate([
73
+ isNumber,
74
+ isMsStringValue,
75
+ ]);
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@keeex/utils","version":"7.2.1","description":"Various utility functions for pure JavaScript","scripts":{},"author":"KeeeX SAS","contributors":[{"name":"Gabriel Paul \"Cley Faye\" Risterucci","email":"gabriel@keeex.net"}],"homepage":"https://keeex.me/oss","keywords":["utility"],"type":"module","license":"MIT","dependencies":{"@keeex/bubble_babble":"^3.0.1","@keeex/log":"^1.7.1","base64-arraybuffer":"^1.0.2","cron-parser":"^5.2.0","ms":"^2.1.3","text-encoding-shim":"^1.0.5"},"exports":{"./units.js":{"node":"./lib/units.js","browser":"./web/units.js","default":"./lib/units.js"},"./uint8array.js":{"node":"./lib/uint8array.js","browser":"./web/uint8array.js","default":"./lib/uint8array.js"},"./string.js":{"node":"./lib/string.js","browser":"./web/string.js","default":"./lib/string.js"},"./starttime.js":{"node":"./lib/starttime.js","browser":"./web/starttime.js","default":"./lib/starttime.js"},"./promise.js":{"node":"./lib/promise.js","browser":"./web/promise.js","default":"./lib/promise.js"},"./path.js":{"node":"./lib/path.js","browser":"./web/path.js","default":"./lib/path.js"},"./number.js":{"node":"./lib/number.js","browser":"./web/number.js","default":"./lib/number.js"},"./linebuffer.js":{"node":"./lib/linebuffer.js","browser":"./web/linebuffer.js","default":"./lib/linebuffer.js"},"./json.js":{"node":"./lib/json.js","browser":"./web/json.js","default":"./lib/json.js"},"./idx.js":{"node":"./lib/idx.js","browser":"./web/idx.js","default":"./lib/idx.js"},"./hex.js":{"node":"./lib/hex.js","browser":"./web/hex.js","default":"./lib/hex.js"},"./global.js":{"node":"./lib/global.js","browser":"./web/global.js","default":"./lib/global.js"},"./error.js":{"node":"./lib/error.js","browser":"./web/error.js","default":"./lib/error.js"},"./dict.js":{"node":"./lib/dict.js","browser":"./web/dict.js","default":"./lib/dict.js"},"./cron.js":{"node":"./lib/cron.js","browser":"./web/cron.js","default":"./lib/cron.js"},"./consts.js":{"node":"./lib/consts.js","browser":"./web/consts.js","default":"./lib/consts.js"},"./bytebuffer.js":{"node":"./lib/bytebuffer.js","browser":"./web/bytebuffer.js","default":"./lib/bytebuffer.js"},"./benchmark.js":{"node":"./lib/benchmark.js","browser":"./web/benchmark.js","default":"./lib/benchmark.js"},"./base64.js":{"node":"./lib/base64.js","browser":"./web/base64.js","default":"./lib/base64.js"},"./base58.js":{"node":"./lib/base58.js","browser":"./web/base58.js","default":"./lib/base58.js"},"./arraybuffer.js":{"node":"./lib/arraybuffer.js","browser":"./web/arraybuffer.js","default":"./lib/arraybuffer.js"},"./array.js":{"node":"./lib/array.js","browser":"./web/array.js","default":"./lib/array.js"},"./types/utils.js":{"node":"./lib/types/utils.js","browser":"./web/types/utils.js","default":"./lib/types/utils.js"},"./types/types.js":{"node":"./lib/types/types.js","browser":"./web/types/types.js","default":"./lib/types/types.js"},"./types/record.js":{"node":"./lib/types/record.js","browser":"./web/types/record.js","default":"./lib/types/record.js"},"./types/primitive.js":{"node":"./lib/types/primitive.js","browser":"./web/types/primitive.js","default":"./lib/types/primitive.js"},"./types/predicateerror.js":{"node":"./lib/types/predicateerror.js","browser":"./web/types/predicateerror.js","default":"./lib/types/predicateerror.js"},"./types/enum.js":{"node":"./lib/types/enum.js","browser":"./web/types/enum.js","default":"./lib/types/enum.js"},"./types/array.js":{"node":"./lib/types/array.js","browser":"./web/types/array.js","default":"./lib/types/array.js"},"./marshalling/unmarshaller.js":{"node":"./lib/marshalling/unmarshaller.js","browser":"./web/marshalling/unmarshaller.js","default":"./lib/marshalling/unmarshaller.js"},"./marshalling/marshaller.js":{"node":"./lib/marshalling/marshaller.js","browser":"./web/marshalling/marshaller.js","default":"./lib/marshalling/marshaller.js"},"./async/timecache.js":{"node":"./lib/async/timecache.js","browser":"./web/async/timecache.js","default":"./lib/async/timecache.js"},"./async/queues.js":{"node":"./lib/async/queues.js","browser":"./web/async/queues.js","default":"./lib/async/queues.js"},"./async/keycache.js":{"node":"./lib/async/keycache.js","browser":"./web/async/keycache.js","default":"./lib/async/keycache.js"},"./async/deferredpromise.js":{"node":"./lib/async/deferredpromise.js","browser":"./web/async/deferredpromise.js","default":"./lib/async/deferredpromise.js"},"./async/asynctrigger.js":{"node":"./lib/async/asynctrigger.js","browser":"./web/async/asynctrigger.js","default":"./lib/async/asynctrigger.js"}},"files":["/lib","/web"]}
1
+ {"name":"@keeex/utils","version":"7.4.0","description":"Various utility functions for pure JavaScript","scripts":{},"author":"KeeeX SAS","contributors":[{"name":"Gabriel Paul \"Cley Faye\" Risterucci","email":"gabriel@keeex.net"}],"homepage":"https://keeex.me/oss","keywords":["utility"],"type":"module","license":"MIT","dependencies":{"@keeex/bubble_babble":"^3.0.1","@keeex/log":"^1.7.1","base64-arraybuffer":"^1.0.2","cron-parser":"^5.3.0","ms":"^2.1.3","text-encoding-shim":"^1.0.5"},"exports":{"./array.js":{"node":"./lib/array.js","browser":"./web/array.js","react-native":"./web/array.js","default":"./lib/array.js"},"./arraybuffer.js":{"node":"./lib/arraybuffer.js","browser":"./web/arraybuffer.js","react-native":"./web/arraybuffer.js","default":"./lib/arraybuffer.js"},"./async/asynctrigger.js":{"node":"./lib/async/asynctrigger.js","browser":"./web/async/asynctrigger.js","react-native":"./web/async/asynctrigger.js","default":"./lib/async/asynctrigger.js"},"./async/deferredpromise.js":{"node":"./lib/async/deferredpromise.js","browser":"./web/async/deferredpromise.js","react-native":"./web/async/deferredpromise.js","default":"./lib/async/deferredpromise.js"},"./async/eventqueue.js":{"node":"./lib/async/eventqueue.js","browser":"./web/async/eventqueue.js","react-native":"./web/async/eventqueue.js","default":"./lib/async/eventqueue.js"},"./async/keycache.js":{"node":"./lib/async/keycache.js","browser":"./web/async/keycache.js","react-native":"./web/async/keycache.js","default":"./lib/async/keycache.js"},"./async/queues.js":{"node":"./lib/async/queues.js","browser":"./web/async/queues.js","react-native":"./web/async/queues.js","default":"./lib/async/queues.js"},"./async/timecache.js":{"node":"./lib/async/timecache.js","browser":"./web/async/timecache.js","react-native":"./web/async/timecache.js","default":"./lib/async/timecache.js"},"./base58.js":{"node":"./lib/base58.js","browser":"./web/base58.js","react-native":"./web/base58.js","default":"./lib/base58.js"},"./base64.js":{"node":"./lib/base64.js","browser":"./web/base64.js","react-native":"./web/base64.js","default":"./lib/base64.js"},"./benchmark.js":{"node":"./lib/benchmark.js","browser":"./web/benchmark.js","react-native":"./web/benchmark.js","default":"./lib/benchmark.js"},"./bytebuffer.js":{"node":"./lib/bytebuffer.js","browser":"./web/bytebuffer.js","react-native":"./web/bytebuffer.js","default":"./lib/bytebuffer.js"},"./consts.js":{"node":"./lib/consts.js","browser":"./web/consts.js","react-native":"./web/consts.js","default":"./lib/consts.js"},"./cron.js":{"node":"./lib/cron.js","browser":"./web/cron.js","react-native":"./web/cron.js","default":"./lib/cron.js"},"./dict.js":{"node":"./lib/dict.js","browser":"./web/dict.js","react-native":"./web/dict.js","default":"./lib/dict.js"},"./error.js":{"node":"./lib/error.js","browser":"./web/error.js","react-native":"./web/error.js","default":"./lib/error.js"},"./global.js":{"node":"./lib/global.js","browser":"./web/global.js","react-native":"./web/global.js","default":"./lib/global.js"},"./hex.js":{"node":"./lib/hex.js","browser":"./web/hex.js","react-native":"./web/hex.js","default":"./lib/hex.js"},"./idx.js":{"node":"./lib/idx.js","browser":"./web/idx.js","react-native":"./web/idx.js","default":"./lib/idx.js"},"./json.js":{"node":"./lib/json.js","browser":"./web/json.js","react-native":"./web/json.js","default":"./lib/json.js"},"./linebuffer.js":{"node":"./lib/linebuffer.js","browser":"./web/linebuffer.js","react-native":"./web/linebuffer.js","default":"./lib/linebuffer.js"},"./marshalling/marshaller.js":{"node":"./lib/marshalling/marshaller.js","browser":"./web/marshalling/marshaller.js","react-native":"./web/marshalling/marshaller.js","default":"./lib/marshalling/marshaller.js"},"./marshalling/unmarshaller.js":{"node":"./lib/marshalling/unmarshaller.js","browser":"./web/marshalling/unmarshaller.js","react-native":"./web/marshalling/unmarshaller.js","default":"./lib/marshalling/unmarshaller.js"},"./number.js":{"node":"./lib/number.js","browser":"./web/number.js","react-native":"./web/number.js","default":"./lib/number.js"},"./path.js":{"node":"./lib/path.js","browser":"./web/path.js","react-native":"./web/path.js","default":"./lib/path.js"},"./promise.js":{"node":"./lib/promise.js","browser":"./web/promise.js","react-native":"./web/promise.js","default":"./lib/promise.js"},"./starttime.js":{"node":"./lib/starttime.js","browser":"./web/starttime.js","react-native":"./web/starttime.js","default":"./lib/starttime.js"},"./string.js":{"node":"./lib/string.js","browser":"./web/string.js","react-native":"./web/string.js","default":"./lib/string.js"},"./triggers.js":{"node":"./lib/triggers.js","browser":"./web/triggers.js","react-native":"./web/triggers.js","default":"./lib/triggers.js"},"./types/array.js":{"node":"./lib/types/array.js","browser":"./web/types/array.js","react-native":"./web/types/array.js","default":"./lib/types/array.js"},"./types/enum.js":{"node":"./lib/types/enum.js","browser":"./web/types/enum.js","react-native":"./web/types/enum.js","default":"./lib/types/enum.js"},"./types/predicateerror.js":{"node":"./lib/types/predicateerror.js","browser":"./web/types/predicateerror.js","react-native":"./web/types/predicateerror.js","default":"./lib/types/predicateerror.js"},"./types/primitive.js":{"node":"./lib/types/primitive.js","browser":"./web/types/primitive.js","react-native":"./web/types/primitive.js","default":"./lib/types/primitive.js"},"./types/record.js":{"node":"./lib/types/record.js","browser":"./web/types/record.js","react-native":"./web/types/record.js","default":"./lib/types/record.js"},"./types/types.js":{"node":"./lib/types/types.js","browser":"./web/types/types.js","react-native":"./web/types/types.js","default":"./lib/types/types.js"},"./types/utils.js":{"node":"./lib/types/utils.js","browser":"./web/types/utils.js","react-native":"./web/types/utils.js","default":"./lib/types/utils.js"},"./uint8array.js":{"node":"./lib/uint8array.js","browser":"./web/uint8array.js","react-native":"./web/uint8array.js","default":"./lib/uint8array.js"},"./units.js":{"node":"./lib/units.js","browser":"./web/units.js","react-native":"./web/units.js","default":"./lib/units.js"}},"files":["/lib","/web"]}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @license
3
+ * @preserve
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2023 KeeeX SAS
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
+ *
15
+ */
16
+ import * as kxLog from "@keeex/log";
17
+ import { type Awaitable } from "../types/types.js";
18
+ export type EventHandler<DataType> = DataType extends undefined ? () => Awaitable<void> : (data: DataType) => Awaitable<void>;
19
+ /** Function used to merge multiple events into one */
20
+ export type MergeFunc<DataType> = (pendingData: Array<DataType>) => Awaitable<DataType>;
21
+ /**
22
+ * Provides asynchronous handling of a sequential event queue.
23
+ *
24
+ * This should work as a regular sequential event queue, except that the handling of these events
25
+ * can be done using asynchronous functions.
26
+ * All handlers will be called on all input events in the order they came in, but there is no
27
+ * guarantee on the *order* of the event handlers when there is more than one.
28
+ *
29
+ * More events can be triggered while some events are processed, in which case they'll cause more
30
+ * calls to the handlers as needed.
31
+ */
32
+ export declare class EventQueue<DataType> {
33
+ #private;
34
+ constructor(merger?: MergeFunc<DataType>, logger?: kxLog.Logger);
35
+ addHandler: (handler: EventHandler<DataType>) => void;
36
+ removeHandler: (handler: EventHandler<DataType>) => void;
37
+ /**
38
+ * Cancel all pending events.
39
+ *
40
+ * Currently pending events that are not being processed are dropped.
41
+ * This does not prevent more events from being sent, and will not stop current processing.
42
+ */
43
+ cancelAll: () => void;
44
+ /**
45
+ * Disable all further event processing.
46
+ *
47
+ * This will not stop current processing.
48
+ */
49
+ close: (cancelAll?: boolean) => void;
50
+ /**
51
+ * Wait for all handlers to complete.
52
+ *
53
+ * If there are currently events being processed, this will wait until they are all processed.
54
+ * If more events show up while waiting, it will wait for those events to be processed too.
55
+ * If there are no events currently being processed, it will resolve immediately.
56
+ */
57
+ wait: () => Promise<void>;
58
+ /** Trigger an event to be handled */
59
+ trigger: (data: DataType) => void;
60
+ }