@naturalcycles/js-lib 14.276.0 → 15.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/cfg/frontend/tsconfig.json +3 -3
- package/dist/abort.js +1 -4
- package/dist/array/array.util.d.ts +1 -1
- package/dist/array/array.util.js +47 -88
- package/dist/array/range.d.ts +2 -2
- package/dist/array/range.js +7 -12
- package/dist/bot.js +6 -10
- package/dist/browser/adminService.d.ts +1 -1
- package/dist/browser/adminService.js +10 -14
- package/dist/browser/analytics.util.js +10 -15
- package/dist/browser/i18n/fetchTranslationLoader.d.ts +3 -3
- package/dist/browser/i18n/fetchTranslationLoader.js +1 -5
- package/dist/browser/i18n/translation.service.d.ts +1 -1
- package/dist/browser/i18n/translation.service.js +5 -10
- package/dist/browser/imageFitter.js +1 -5
- package/dist/browser/script.util.js +8 -12
- package/dist/browser/topbar.js +7 -10
- package/dist/datetime/dateInterval.d.ts +2 -2
- package/dist/datetime/dateInterval.js +7 -11
- package/dist/datetime/localDate.d.ts +3 -3
- package/dist/datetime/localDate.js +40 -44
- package/dist/datetime/localTime.d.ts +3 -3
- package/dist/datetime/localTime.js +36 -40
- package/dist/datetime/timeInterval.d.ts +2 -2
- package/dist/datetime/timeInterval.js +6 -10
- package/dist/datetime/wallTime.d.ts +3 -3
- package/dist/datetime/wallTime.js +5 -9
- package/dist/decorators/asyncMemo.decorator.d.ts +3 -3
- package/dist/decorators/asyncMemo.decorator.js +12 -17
- package/dist/decorators/createPromiseDecorator.js +3 -6
- package/dist/decorators/debounce.d.ts +1 -1
- package/dist/decorators/debounce.decorator.d.ts +1 -1
- package/dist/decorators/debounce.decorator.js +5 -9
- package/dist/decorators/debounce.js +2 -6
- package/dist/decorators/decorator.util.d.ts +1 -1
- package/dist/decorators/decorator.util.js +3 -8
- package/dist/decorators/logMethod.decorator.d.ts +1 -1
- package/dist/decorators/logMethod.decorator.js +11 -14
- package/dist/decorators/memo.decorator.d.ts +3 -3
- package/dist/decorators/memo.decorator.js +11 -16
- package/dist/decorators/memo.util.d.ts +2 -2
- package/dist/decorators/memo.util.js +11 -17
- package/dist/decorators/memoFn.d.ts +3 -3
- package/dist/decorators/memoFn.js +3 -6
- package/dist/decorators/memoFnAsync.d.ts +3 -3
- package/dist/decorators/memoFnAsync.js +5 -8
- package/dist/decorators/memoSimple.decorator.d.ts +1 -1
- package/dist/decorators/memoSimple.decorator.js +6 -10
- package/dist/decorators/retry.decorator.d.ts +1 -1
- package/dist/decorators/retry.decorator.js +3 -6
- package/dist/decorators/swarmSafe.decorator.js +3 -7
- package/dist/decorators/timeout.decorator.d.ts +1 -1
- package/dist/decorators/timeout.decorator.js +7 -10
- package/dist/define.d.ts +1 -1
- package/dist/define.js +11 -19
- package/dist/deviceIdService.js +7 -11
- package/dist/enum.util.d.ts +1 -1
- package/dist/enum.util.js +20 -42
- package/dist/env/buildInfo.d.ts +1 -1
- package/dist/env/buildInfo.js +3 -6
- package/dist/env.js +2 -6
- package/dist/error/assert.d.ts +3 -3
- package/dist/error/assert.js +30 -44
- package/dist/error/error.model.d.ts +2 -2
- package/dist/error/error.model.js +1 -2
- package/dist/error/error.util.d.ts +1 -1
- package/dist/error/error.util.js +25 -44
- package/dist/error/errorMode.js +2 -5
- package/dist/error/try.d.ts +2 -2
- package/dist/error/try.js +15 -23
- package/dist/error/tryCatch.d.ts +2 -2
- package/dist/error/tryCatch.js +6 -11
- package/dist/form.util.d.ts +1 -1
- package/dist/form.util.js +2 -6
- package/dist/http/fetcher.d.ts +3 -3
- package/dist/http/fetcher.js +48 -53
- package/dist/http/fetcher.model.d.ts +5 -5
- package/dist/http/fetcher.model.js +1 -2
- package/dist/http/http.model.js +1 -4
- package/dist/index.d.ts +96 -96
- package/dist/index.js +96 -99
- package/dist/is.util.d.ts +2 -2
- package/dist/is.util.js +12 -27
- package/dist/iter/asyncIterable2.d.ts +3 -3
- package/dist/iter/asyncIterable2.js +8 -12
- package/dist/iter/iterable2.d.ts +2 -2
- package/dist/iter/iterable2.js +8 -12
- package/dist/json-schema/from-data/generateJsonSchemaFromData.d.ts +1 -1
- package/dist/json-schema/from-data/generateJsonSchemaFromData.js +4 -7
- package/dist/json-schema/jsonSchema.cnst.d.ts +1 -1
- package/dist/json-schema/jsonSchema.cnst.js +1 -4
- package/dist/json-schema/jsonSchema.model.d.ts +1 -1
- package/dist/json-schema/jsonSchema.model.js +1 -2
- package/dist/json-schema/jsonSchema.util.d.ts +2 -2
- package/dist/json-schema/jsonSchema.util.js +5 -8
- package/dist/json-schema/jsonSchemaBuilder.d.ts +2 -2
- package/dist/json-schema/jsonSchemaBuilder.js +16 -25
- package/dist/json-schema/jsonSchemas.d.ts +2 -2
- package/dist/json-schema/jsonSchemas.js +5 -8
- package/dist/log/commonLogger.js +14 -21
- package/dist/math/accumulatingAverage.js +1 -5
- package/dist/math/math.util.js +11 -19
- package/dist/math/sma.js +1 -5
- package/dist/math/stack.util.js +11 -16
- package/dist/nanoid.js +2 -6
- package/dist/number/createDeterministicRandom.js +1 -4
- package/dist/number/number.util.d.ts +1 -1
- package/dist/number/number.util.js +9 -20
- package/dist/object/deepEquals.js +3 -8
- package/dist/object/map2.js +1 -5
- package/dist/object/object.util.d.ts +2 -2
- package/dist/object/object.util.js +40 -70
- package/dist/object/set2.js +1 -5
- package/dist/object/sortObject.d.ts +1 -1
- package/dist/object/sortObject.js +1 -4
- package/dist/object/sortObjectDeep.js +1 -4
- package/dist/polyfill.js +1 -4
- package/dist/promise/abortable.d.ts +1 -1
- package/dist/promise/abortable.js +2 -7
- package/dist/promise/pDefer.js +1 -4
- package/dist/promise/pDelay.d.ts +2 -2
- package/dist/promise/pDelay.js +4 -8
- package/dist/promise/pFilter.d.ts +1 -1
- package/dist/promise/pFilter.js +1 -4
- package/dist/promise/pHang.js +1 -4
- package/dist/promise/pMap.d.ts +2 -2
- package/dist/promise/pMap.js +17 -20
- package/dist/promise/pProps.js +1 -4
- package/dist/promise/pQueue.d.ts +2 -2
- package/dist/promise/pQueue.js +7 -11
- package/dist/promise/pRetry.d.ts +1 -1
- package/dist/promise/pRetry.js +8 -12
- package/dist/promise/pState.js +1 -4
- package/dist/promise/pTimeout.d.ts +3 -3
- package/dist/promise/pTimeout.js +7 -11
- package/dist/semver.d.ts +1 -1
- package/dist/semver.js +13 -18
- package/dist/string/case.js +9 -14
- package/dist/string/escape.js +2 -6
- package/dist/string/hash.util.d.ts +1 -1
- package/dist/string/hash.util.js +4 -10
- package/dist/string/json.util.d.ts +1 -1
- package/dist/string/json.util.js +5 -10
- package/dist/string/leven.js +1 -4
- package/dist/string/lodash/unicodeWords.js +1 -4
- package/dist/string/lodash/words.js +3 -6
- package/dist/string/pupa.d.ts +1 -1
- package/dist/string/pupa.js +4 -9
- package/dist/string/readingTime.d.ts +1 -1
- package/dist/string/readingTime.js +1 -4
- package/dist/string/regex.js +1 -4
- package/dist/string/safeJsonStringify.d.ts +1 -1
- package/dist/string/safeJsonStringify.js +1 -4
- package/dist/string/slugify.js +1 -4
- package/dist/string/string.util.js +15 -32
- package/dist/string/stringify.d.ts +1 -1
- package/dist/string/stringify.js +10 -14
- package/dist/string/url.util.d.ts +1 -1
- package/dist/string/url.util.js +2 -6
- package/dist/time/time.util.d.ts +1 -1
- package/dist/time/time.util.js +3 -8
- package/dist/typeFest.js +1 -2
- package/dist/types.d.ts +7 -3
- package/dist/types.js +21 -23
- package/dist/unit/size.util.js +5 -12
- package/dist/web.d.ts +1 -1
- package/dist/web.js +1 -5
- package/dist/zod/index.d.ts +2 -2
- package/dist/zod/index.js +4 -10
- package/dist/zod/zod.shared.schemas.js +36 -39
- package/dist/zod/zod.util.js +7 -14
- package/package.json +7 -8
- package/src/array/array.util.ts +3 -3
- package/src/array/range.ts +2 -2
- package/src/bot.ts +1 -1
- package/src/browser/adminService.ts +4 -4
- package/src/browser/analytics.util.ts +1 -1
- package/src/browser/i18n/fetchTranslationLoader.ts +3 -3
- package/src/browser/i18n/translation.service.ts +2 -2
- package/src/browser/script.util.ts +2 -2
- package/src/datetime/dateInterval.ts +3 -3
- package/src/datetime/localDate.ts +5 -5
- package/src/datetime/localTime.ts +6 -6
- package/src/datetime/timeInterval.ts +3 -3
- package/src/datetime/wallTime.ts +4 -4
- package/src/decorators/asyncMemo.decorator.ts +7 -7
- package/src/decorators/createPromiseDecorator.ts +1 -1
- package/src/decorators/debounce.decorator.ts +2 -2
- package/src/decorators/debounce.ts +1 -1
- package/src/decorators/decorator.util.ts +1 -1
- package/src/decorators/logMethod.decorator.ts +4 -4
- package/src/decorators/memo.decorator.ts +7 -7
- package/src/decorators/memo.util.ts +4 -4
- package/src/decorators/memoFn.ts +4 -4
- package/src/decorators/memoFnAsync.ts +5 -5
- package/src/decorators/memoSimple.decorator.ts +4 -4
- package/src/decorators/retry.decorator.ts +2 -2
- package/src/decorators/swarmSafe.decorator.ts +2 -2
- package/src/decorators/timeout.decorator.ts +4 -4
- package/src/define.ts +3 -3
- package/src/deviceIdService.ts +3 -3
- package/src/enum.util.ts +1 -1
- package/src/env/buildInfo.ts +2 -2
- package/src/error/assert.ts +7 -7
- package/src/error/error.model.ts +2 -2
- package/src/error/error.util.ts +5 -5
- package/src/error/try.ts +5 -5
- package/src/error/tryCatch.ts +3 -3
- package/src/form.util.ts +1 -1
- package/src/http/fetcher.model.ts +5 -5
- package/src/http/fetcher.ts +20 -15
- package/src/index.ts +96 -96
- package/src/is.util.ts +2 -2
- package/src/iter/asyncIterable2.ts +3 -3
- package/src/iter/iterable2.ts +2 -2
- package/src/json-schema/from-data/generateJsonSchemaFromData.ts +2 -2
- package/src/json-schema/jsonSchema.cnst.ts +1 -1
- package/src/json-schema/jsonSchema.model.ts +1 -1
- package/src/json-schema/jsonSchema.util.ts +4 -4
- package/src/json-schema/jsonSchemaBuilder.ts +5 -5
- package/src/json-schema/jsonSchemas.ts +2 -2
- package/src/math/math.util.ts +2 -2
- package/src/math/stack.util.ts +1 -1
- package/src/number/number.util.ts +1 -1
- package/src/object/object.util.ts +3 -3
- package/src/object/sortObject.ts +1 -1
- package/src/promise/abortable.ts +1 -1
- package/src/promise/pDelay.ts +3 -3
- package/src/promise/pFilter.ts +1 -1
- package/src/promise/pMap.ts +2 -2
- package/src/promise/pQueue.ts +4 -4
- package/src/promise/pRetry.ts +2 -2
- package/src/promise/pTimeout.ts +4 -4
- package/src/semver.ts +3 -3
- package/src/string/case.ts +2 -2
- package/src/string/hash.util.ts +1 -1
- package/src/string/json.util.ts +2 -2
- package/src/string/lodash/words.ts +1 -1
- package/src/string/pupa.ts +2 -2
- package/src/string/readingTime.ts +1 -1
- package/src/string/safeJsonStringify.ts +1 -1
- package/src/string/stringify.ts +4 -4
- package/src/string/url.util.ts +1 -1
- package/src/time/time.util.ts +1 -1
- package/src/types.ts +15 -3
- package/src/web.ts +1 -1
- package/src/zod/index.ts +2 -2
- package/src/zod/zod.util.ts +1 -1
- package/dist-esm/abort.js +0 -12
- package/dist-esm/array/array.util.js +0 -458
- package/dist-esm/array/range.js +0 -34
- package/dist-esm/bot.js +0 -130
- package/dist-esm/browser/adminService.js +0 -94
- package/dist-esm/browser/analytics.util.js +0 -54
- package/dist-esm/browser/i18n/fetchTranslationLoader.js +0 -13
- package/dist-esm/browser/i18n/translation.service.js +0 -56
- package/dist-esm/browser/imageFitter.js +0 -65
- package/dist-esm/browser/script.util.js +0 -46
- package/dist-esm/browser/topbar.js +0 -135
- package/dist-esm/datetime/dateInterval.js +0 -80
- package/dist-esm/datetime/localDate.js +0 -719
- package/dist-esm/datetime/localTime.js +0 -996
- package/dist-esm/datetime/timeInterval.js +0 -88
- package/dist-esm/datetime/wallTime.js +0 -70
- package/dist-esm/decorators/asyncMemo.decorator.js +0 -111
- package/dist-esm/decorators/createPromiseDecorator.js +0 -82
- package/dist-esm/decorators/debounce.decorator.js +0 -17
- package/dist-esm/decorators/debounce.js +0 -114
- package/dist-esm/decorators/decorator.util.js +0 -31
- package/dist-esm/decorators/logMethod.decorator.js +0 -85
- package/dist-esm/decorators/memo.decorator.js +0 -80
- package/dist-esm/decorators/memo.util.js +0 -97
- package/dist-esm/decorators/memoFn.js +0 -29
- package/dist-esm/decorators/memoFnAsync.js +0 -35
- package/dist-esm/decorators/memoSimple.decorator.js +0 -55
- package/dist-esm/decorators/retry.decorator.js +0 -9
- package/dist-esm/decorators/swarmSafe.decorator.js +0 -38
- package/dist-esm/decorators/timeout.decorator.js +0 -19
- package/dist-esm/define.js +0 -109
- package/dist-esm/deviceIdService.js +0 -105
- package/dist-esm/enum.util.js +0 -157
- package/dist-esm/env/buildInfo.js +0 -19
- package/dist-esm/env.js +0 -19
- package/dist-esm/error/assert.js +0 -122
- package/dist-esm/error/error.model.js +0 -1
- package/dist-esm/error/error.util.js +0 -337
- package/dist-esm/error/errorMode.js +0 -20
- package/dist-esm/error/try.js +0 -105
- package/dist-esm/error/tryCatch.js +0 -41
- package/dist-esm/form.util.js +0 -16
- package/dist-esm/http/fetcher.js +0 -704
- package/dist-esm/http/fetcher.model.js +0 -3
- package/dist-esm/http/http.model.js +0 -1
- package/dist-esm/index.js +0 -96
- package/dist-esm/is.util.js +0 -70
- package/dist-esm/iter/asyncIterable2.js +0 -103
- package/dist-esm/iter/iterable2.js +0 -87
- package/dist-esm/json-schema/from-data/generateJsonSchemaFromData.js +0 -86
- package/dist-esm/json-schema/jsonSchema.cnst.js +0 -38
- package/dist-esm/json-schema/jsonSchema.model.js +0 -1
- package/dist-esm/json-schema/jsonSchema.util.js +0 -27
- package/dist-esm/json-schema/jsonSchemaBuilder.js +0 -352
- package/dist-esm/json-schema/jsonSchemas.js +0 -6
- package/dist-esm/log/commonLogger.js +0 -78
- package/dist-esm/math/accumulatingAverage.js +0 -32
- package/dist-esm/math/math.util.js +0 -85
- package/dist-esm/math/sma.js +0 -43
- package/dist-esm/math/stack.util.js +0 -82
- package/dist-esm/nanoid.js +0 -57
- package/dist-esm/number/createDeterministicRandom.js +0 -18
- package/dist-esm/number/number.util.js +0 -109
- package/dist-esm/object/deepEquals.js +0 -170
- package/dist-esm/object/map2.js +0 -21
- package/dist-esm/object/object.util.js +0 -435
- package/dist-esm/object/set2.js +0 -15
- package/dist-esm/object/sortObject.js +0 -21
- package/dist-esm/object/sortObjectDeep.js +0 -17
- package/dist-esm/polyfill.js +0 -6
- package/dist-esm/promise/abortable.js +0 -31
- package/dist-esm/promise/pDefer.js +0 -17
- package/dist-esm/promise/pDelay.js +0 -37
- package/dist-esm/promise/pFilter.js +0 -5
- package/dist-esm/promise/pHang.js +0 -6
- package/dist-esm/promise/pMap.js +0 -174
- package/dist-esm/promise/pProps.js +0 -20
- package/dist-esm/promise/pQueue.js +0 -101
- package/dist-esm/promise/pRetry.js +0 -64
- package/dist-esm/promise/pState.js +0 -14
- package/dist-esm/promise/pTimeout.js +0 -64
- package/dist-esm/semver.js +0 -149
- package/dist-esm/string/case.js +0 -24
- package/dist-esm/string/escape.js +0 -49
- package/dist-esm/string/hash.util.js +0 -67
- package/dist-esm/string/json.util.js +0 -46
- package/dist-esm/string/leven.js +0 -77
- package/dist-esm/string/lodash/unicodeWords.js +0 -68
- package/dist-esm/string/lodash/words.js +0 -31
- package/dist-esm/string/pupa.js +0 -54
- package/dist-esm/string/readingTime.js +0 -102
- package/dist-esm/string/regex.js +0 -6
- package/dist-esm/string/safeJsonStringify.js +0 -39
- package/dist-esm/string/slugify.js +0 -66
- package/dist-esm/string/string.util.js +0 -99
- package/dist-esm/string/stringify.js +0 -133
- package/dist-esm/string/url.util.js +0 -42
- package/dist-esm/time/time.util.js +0 -61
- package/dist-esm/typeFest.js +0 -2
- package/dist-esm/types.js +0 -59
- package/dist-esm/unit/size.util.js +0 -43
- package/dist-esm/web.js +0 -40
- package/dist-esm/zod/index.js +0 -4
- package/dist-esm/zod/zod.shared.schemas.js +0 -92
- package/dist-esm/zod/zod.util.js +0 -52
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { CommonLogger } from '../log/commonLogger';
|
|
2
|
-
import type { AnyFunction, AnyObject, MaybeParameters } from '../types';
|
|
3
|
-
import type { MemoCache, MethodDecorator } from './memo.util';
|
|
1
|
+
import type { CommonLogger } from '../log/commonLogger.js';
|
|
2
|
+
import type { AnyFunction, AnyObject, MaybeParameters } from '../types.js';
|
|
3
|
+
import type { MemoCache, MethodDecorator } from './memo.util.js';
|
|
4
4
|
export interface MemoOptions<FN> {
|
|
5
5
|
/**
|
|
6
6
|
* Provide a custom implementation of MemoCache.
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const assert_1 = require("../error/assert");
|
|
6
|
-
const types_1 = require("../types");
|
|
7
|
-
const decorator_util_1 = require("./decorator.util");
|
|
8
|
-
const memo_util_1 = require("./memo.util");
|
|
1
|
+
import { _assert, _assertTypeOf } from '../error/assert.js';
|
|
2
|
+
import { _objectAssign } from '../types.js';
|
|
3
|
+
import { _getTargetMethodSignature } from './decorator.util.js';
|
|
4
|
+
import { jsonMemoSerializer, MapMemoCache } from './memo.util.js';
|
|
9
5
|
/**
|
|
10
6
|
* Memoizes the method of the class, so it caches the output and returns the cached version if the "key"
|
|
11
7
|
* of the cache is the same. Key, by defaul, is calculated as `JSON.stringify(...args)`.
|
|
@@ -27,8 +23,8 @@ const memo_util_1 = require("./memo.util");
|
|
|
27
23
|
* https://community.risingstack.com/the-worlds-fastest-javascript-memoization-library/
|
|
28
24
|
*/
|
|
29
25
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
30
|
-
const _Memo = (opt = {}) => (target, key, descriptor) => {
|
|
31
|
-
|
|
26
|
+
export const _Memo = (opt = {}) => (target, key, descriptor) => {
|
|
27
|
+
_assertTypeOf(descriptor.value, 'function', 'Memoization can be applied only to methods');
|
|
32
28
|
const originalFn = descriptor.value;
|
|
33
29
|
// Map<ctx => MemoCache<cacheKey, result>>
|
|
34
30
|
//
|
|
@@ -38,9 +34,9 @@ const _Memo = (opt = {}) => (target, key, descriptor) => {
|
|
|
38
34
|
// UPD: tests show that normal Map also doesn't leak (to be tested further)
|
|
39
35
|
// Normal Map is needed to allow .clear()
|
|
40
36
|
const instanceCache = new Map();
|
|
41
|
-
const { logger = console, cacheFactory = () => new
|
|
37
|
+
const { logger = console, cacheFactory = () => new MapMemoCache(), cacheKeyFn = jsonMemoSerializer, } = opt;
|
|
42
38
|
const keyStr = String(key);
|
|
43
|
-
const methodSignature =
|
|
39
|
+
const methodSignature = _getTargetMethodSignature(target, keyStr);
|
|
44
40
|
descriptor.value = function (...args) {
|
|
45
41
|
const ctx = this;
|
|
46
42
|
const cacheKey = cacheKeyFn(args);
|
|
@@ -63,7 +59,7 @@ const _Memo = (opt = {}) => (target, key, descriptor) => {
|
|
|
63
59
|
}
|
|
64
60
|
return value;
|
|
65
61
|
};
|
|
66
|
-
|
|
62
|
+
_objectAssign(descriptor.value, {
|
|
67
63
|
clear: () => {
|
|
68
64
|
logger.log(`${methodSignature} @_Memo.clear()`);
|
|
69
65
|
instanceCache.forEach(memoCache => memoCache.clear());
|
|
@@ -74,12 +70,11 @@ const _Memo = (opt = {}) => (target, key, descriptor) => {
|
|
|
74
70
|
});
|
|
75
71
|
return descriptor;
|
|
76
72
|
};
|
|
77
|
-
exports._Memo = _Memo;
|
|
78
73
|
/**
|
|
79
74
|
Call it on a method that is decorated with `@_Memo` to get access to additional functions,
|
|
80
75
|
e.g `clear` to clear the cache, or get its underlying data.
|
|
81
76
|
*/
|
|
82
|
-
function _getMemo(method) {
|
|
83
|
-
|
|
77
|
+
export function _getMemo(method) {
|
|
78
|
+
_assert(typeof method?.getInstanceCache === 'function', 'method is not a Memo instance');
|
|
84
79
|
return method;
|
|
85
80
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { AnyObject, UnixTimestamp } from '../types';
|
|
2
|
-
import { MISS } from '../types';
|
|
1
|
+
import type { AnyObject, UnixTimestamp } from '../types.js';
|
|
2
|
+
import { MISS } from '../types.js';
|
|
3
3
|
export type MemoSerializer = (args: any[]) => any;
|
|
4
4
|
export declare const jsonMemoSerializer: MemoSerializer;
|
|
5
5
|
export interface MemoCacheOptions {
|
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const pDelay_1 = require("../promise/pDelay");
|
|
6
|
-
const types_1 = require("../types");
|
|
7
|
-
const jsonMemoSerializer = args => {
|
|
1
|
+
import { _isPrimitive } from '../is.util.js';
|
|
2
|
+
import { pDelay } from '../promise/pDelay.js';
|
|
3
|
+
import { MISS } from '../types.js';
|
|
4
|
+
export const jsonMemoSerializer = args => {
|
|
8
5
|
if (args.length === 0)
|
|
9
6
|
return undefined;
|
|
10
|
-
if (args.length === 1 &&
|
|
7
|
+
if (args.length === 1 && _isPrimitive(args[0]))
|
|
11
8
|
return args[0];
|
|
12
9
|
return JSON.stringify(args);
|
|
13
10
|
};
|
|
14
|
-
exports.jsonMemoSerializer = jsonMemoSerializer;
|
|
15
11
|
// SingleValueMemoCache and ObjectMemoCache are example-only, not used in production code
|
|
16
12
|
/*
|
|
17
13
|
export class SingleValueMemoCache implements MemoCache {
|
|
@@ -57,7 +53,7 @@ export class ObjectMemoCache implements MemoCache {
|
|
|
57
53
|
}
|
|
58
54
|
}
|
|
59
55
|
*/
|
|
60
|
-
class MapMemoCache {
|
|
56
|
+
export class MapMemoCache {
|
|
61
57
|
m = new Map();
|
|
62
58
|
has(k) {
|
|
63
59
|
return this.m.has(k);
|
|
@@ -72,31 +68,29 @@ class MapMemoCache {
|
|
|
72
68
|
this.m.clear();
|
|
73
69
|
}
|
|
74
70
|
}
|
|
75
|
-
exports.MapMemoCache = MapMemoCache;
|
|
76
71
|
/**
|
|
77
72
|
* Implementation of AsyncMemoCache backed by a synchronous Map.
|
|
78
73
|
* Doesn't have a practical use except testing,
|
|
79
74
|
* because the point of AsyncMemoCache is to have an **async** backed cache.
|
|
80
75
|
*/
|
|
81
|
-
class MapAsyncMemoCache {
|
|
76
|
+
export class MapAsyncMemoCache {
|
|
82
77
|
delay;
|
|
83
78
|
constructor(delay = 0) {
|
|
84
79
|
this.delay = delay;
|
|
85
80
|
}
|
|
86
81
|
m = new Map();
|
|
87
82
|
async get(k) {
|
|
88
|
-
await
|
|
83
|
+
await pDelay(this.delay);
|
|
89
84
|
if (!this.m.has(k))
|
|
90
|
-
return
|
|
85
|
+
return MISS;
|
|
91
86
|
return this.m.get(k);
|
|
92
87
|
}
|
|
93
88
|
async set(k, v) {
|
|
94
|
-
await
|
|
89
|
+
await pDelay(this.delay);
|
|
95
90
|
this.m.set(k, v);
|
|
96
91
|
}
|
|
97
92
|
async clear() {
|
|
98
|
-
await
|
|
93
|
+
await pDelay(this.delay);
|
|
99
94
|
this.m.clear();
|
|
100
95
|
}
|
|
101
96
|
}
|
|
102
|
-
exports.MapAsyncMemoCache = MapAsyncMemoCache;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { AnyFunction } from '../types';
|
|
2
|
-
import type { MemoOptions } from './memo.decorator';
|
|
3
|
-
import type { MemoCache } from './memo.util';
|
|
1
|
+
import type { AnyFunction } from '../types.js';
|
|
2
|
+
import type { MemoOptions } from './memo.decorator.js';
|
|
3
|
+
import type { MemoCache } from './memo.util.js';
|
|
4
4
|
export interface MemoizedFunction {
|
|
5
5
|
cache: MemoCache;
|
|
6
6
|
}
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._memoFn = _memoFn;
|
|
4
|
-
const memo_util_1 = require("./memo.util");
|
|
1
|
+
import { jsonMemoSerializer, MapMemoCache } from './memo.util.js';
|
|
5
2
|
/**
|
|
6
3
|
* Only supports Sync functions.
|
|
7
4
|
* To support Async functions - use _memoFnAsync.
|
|
@@ -9,8 +6,8 @@ const memo_util_1 = require("./memo.util");
|
|
|
9
6
|
*
|
|
10
7
|
* @experimental
|
|
11
8
|
*/
|
|
12
|
-
function _memoFn(fn, opt = {}) {
|
|
13
|
-
const { logger = console, cacheFactory = () => new
|
|
9
|
+
export function _memoFn(fn, opt = {}) {
|
|
10
|
+
const { logger = console, cacheFactory = () => new MapMemoCache(), cacheKeyFn = jsonMemoSerializer, } = opt;
|
|
14
11
|
const cache = cacheFactory();
|
|
15
12
|
const memoizedFn = function (...args) {
|
|
16
13
|
const ctx = this;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { AnyAsyncFunction } from '../types';
|
|
2
|
-
import type { AsyncMemoOptions } from './asyncMemo.decorator';
|
|
3
|
-
import type { AsyncMemoCache } from './memo.util';
|
|
1
|
+
import type { AnyAsyncFunction } from '../types.js';
|
|
2
|
+
import type { AsyncMemoOptions } from './asyncMemo.decorator.js';
|
|
3
|
+
import type { AsyncMemoCache } from './memo.util.js';
|
|
4
4
|
export interface MemoizedAsyncFunction {
|
|
5
5
|
cache: AsyncMemoCache;
|
|
6
6
|
}
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports._memoFnAsync = _memoFnAsync;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
const memo_util_1 = require("./memo.util");
|
|
1
|
+
import { MISS } from '../types.js';
|
|
2
|
+
import { jsonMemoSerializer, MapMemoCache } from './memo.util.js';
|
|
6
3
|
/**
|
|
7
4
|
* @experimental
|
|
8
5
|
*/
|
|
9
|
-
function _memoFnAsync(fn, opt) {
|
|
10
|
-
const { logger = console, cacheFactory = () => new
|
|
6
|
+
export function _memoFnAsync(fn, opt) {
|
|
7
|
+
const { logger = console, cacheFactory = () => new MapMemoCache(), cacheKeyFn = jsonMemoSerializer, } = opt;
|
|
11
8
|
const cache = cacheFactory();
|
|
12
9
|
const memoizedFn = async function (...args) {
|
|
13
10
|
const ctx = this;
|
|
@@ -19,7 +16,7 @@ function _memoFnAsync(fn, opt) {
|
|
|
19
16
|
catch (err) {
|
|
20
17
|
logger.error(err);
|
|
21
18
|
}
|
|
22
|
-
if (value !==
|
|
19
|
+
if (value !== MISS) {
|
|
23
20
|
return value;
|
|
24
21
|
}
|
|
25
22
|
value = await fn.apply(ctx, args);
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
// Based on:
|
|
3
2
|
// https://github.com/mgechev/memo-decorator/blob/master/index.ts
|
|
4
3
|
// http://decodize.com/blog/2012/08/27/javascript-memoization-caching-results-for-better-performance/
|
|
5
4
|
// http://inlehmansterms.net/2015/03/01/javascript-memoization/
|
|
6
5
|
// https://community.risingstack.com/the-worlds-fastest-javascript-memoization-library/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const decorator_util_1 = require("./decorator.util");
|
|
10
|
-
const memo_util_1 = require("./memo.util");
|
|
6
|
+
import { _getTargetMethodSignature } from './decorator.util.js';
|
|
7
|
+
import { jsonMemoSerializer, MapMemoCache } from './memo.util.js';
|
|
11
8
|
// memoSimple decorator is NOT exported. Only used in benchmarks currently
|
|
12
9
|
/**
|
|
13
10
|
* Memoizes the method of the class, so it caches the output and returns the cached version if the "key"
|
|
@@ -17,7 +14,7 @@ const memo_util_1 = require("./memo.util");
|
|
|
17
14
|
*
|
|
18
15
|
* Supports dropping it's cache by calling .dropCache() method of decorated function (useful in unit testing).
|
|
19
16
|
*/
|
|
20
|
-
const memoSimple = (opt = {}) => (target, key, descriptor) => {
|
|
17
|
+
export const memoSimple = (opt = {}) => (target, key, descriptor) => {
|
|
21
18
|
if (typeof descriptor.value !== 'function') {
|
|
22
19
|
throw new TypeError('Memoization can be applied only to methods');
|
|
23
20
|
}
|
|
@@ -36,13 +33,13 @@ const memoSimple = (opt = {}) => (target, key, descriptor) => {
|
|
|
36
33
|
// cache = new ObjectMemoCache()
|
|
37
34
|
}
|
|
38
35
|
*/
|
|
39
|
-
const cache = new
|
|
36
|
+
const cache = new MapMemoCache();
|
|
40
37
|
const { logger = console } = opt;
|
|
41
38
|
const keyStr = String(key);
|
|
42
|
-
const methodSignature =
|
|
39
|
+
const methodSignature = _getTargetMethodSignature(target, keyStr);
|
|
43
40
|
descriptor.value = function (...args) {
|
|
44
41
|
const ctx = this;
|
|
45
|
-
const cacheKey =
|
|
42
|
+
const cacheKey = jsonMemoSerializer(args);
|
|
46
43
|
if (cache.has(cacheKey)) {
|
|
47
44
|
return cache.get(cacheKey);
|
|
48
45
|
}
|
|
@@ -56,4 +53,3 @@ const memoSimple = (opt = {}) => (target, key, descriptor) => {
|
|
|
56
53
|
};
|
|
57
54
|
return descriptor;
|
|
58
55
|
};
|
|
59
|
-
exports.memoSimple = memoSimple;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { PRetryOptions } from '
|
|
1
|
+
import type { PRetryOptions } from '../index.js';
|
|
2
2
|
export declare function _Retry(opt?: PRetryOptions): MethodDecorator;
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._Retry = _Retry;
|
|
4
|
-
const __1 = require("..");
|
|
1
|
+
import { pRetryFn } from '../index.js';
|
|
5
2
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
6
|
-
function _Retry(opt = {}) {
|
|
3
|
+
export function _Retry(opt = {}) {
|
|
7
4
|
return (_target, _key, descriptor) => {
|
|
8
5
|
const originalFn = descriptor.value;
|
|
9
|
-
descriptor.value =
|
|
6
|
+
descriptor.value = pRetryFn(originalFn, opt);
|
|
10
7
|
return descriptor;
|
|
11
8
|
};
|
|
12
9
|
}
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._SwarmSafe = void 0;
|
|
4
|
-
const decorator_util_1 = require("./decorator.util");
|
|
1
|
+
import { _getTargetMethodSignature } from './decorator.util.js';
|
|
5
2
|
/**
|
|
6
3
|
* Prevents "swarm" of async calls to the same method.
|
|
7
4
|
* Allows max 1 in-flight promise to exist.
|
|
@@ -11,13 +8,13 @@ const decorator_util_1 = require("./decorator.util");
|
|
|
11
8
|
* So, the same Promise is returned, regardless of the arguments.
|
|
12
9
|
*/
|
|
13
10
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
14
|
-
const _SwarmSafe = () => (target, key, descriptor) => {
|
|
11
|
+
export const _SwarmSafe = () => (target, key, descriptor) => {
|
|
15
12
|
if (typeof descriptor.value !== 'function') {
|
|
16
13
|
throw new TypeError('@_SwarmSafe can be applied only to methods');
|
|
17
14
|
}
|
|
18
15
|
const originalFn = descriptor.value;
|
|
19
16
|
const keyStr = String(key);
|
|
20
|
-
const methodSignature =
|
|
17
|
+
const methodSignature = _getTargetMethodSignature(target, keyStr);
|
|
21
18
|
const instanceCache = new Map();
|
|
22
19
|
console.log('SwarmSafe constructor called', { key, methodSignature });
|
|
23
20
|
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
@@ -39,4 +36,3 @@ const _SwarmSafe = () => (target, key, descriptor) => {
|
|
|
39
36
|
return inFlightPromise;
|
|
40
37
|
};
|
|
41
38
|
};
|
|
42
|
-
exports._SwarmSafe = _SwarmSafe;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { PTimeoutOptions } from '../promise/pTimeout';
|
|
1
|
+
import type { PTimeoutOptions } from '../promise/pTimeout.js';
|
|
2
2
|
export declare function _Timeout(opt: PTimeoutOptions): MethodDecorator;
|
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const assert_1 = require("../error/assert");
|
|
5
|
-
const pTimeout_1 = require("../promise/pTimeout");
|
|
6
|
-
const decorator_util_1 = require("./decorator.util");
|
|
1
|
+
import { _assert } from '../error/assert.js';
|
|
2
|
+
import { pTimeout } from '../promise/pTimeout.js';
|
|
3
|
+
import { _getMethodSignature } from './decorator.util.js';
|
|
7
4
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
8
|
-
function _Timeout(opt) {
|
|
5
|
+
export function _Timeout(opt) {
|
|
9
6
|
return (target, key, descriptor) => {
|
|
10
|
-
|
|
7
|
+
_assert(typeof descriptor.value === 'function', '@_Timeout can be applied only to methods');
|
|
11
8
|
if (!opt.timeout)
|
|
12
9
|
return descriptor;
|
|
13
10
|
const originalFn = descriptor.value;
|
|
14
11
|
const keyStr = String(key);
|
|
15
12
|
descriptor.value = async function (...args) {
|
|
16
13
|
const ctx = this;
|
|
17
|
-
opt.name ||=
|
|
18
|
-
return await
|
|
14
|
+
opt.name ||= _getMethodSignature(ctx, keyStr);
|
|
15
|
+
return await pTimeout(() => originalFn.apply(this, args), opt);
|
|
19
16
|
};
|
|
20
17
|
return descriptor;
|
|
21
18
|
};
|
package/dist/define.d.ts
CHANGED
package/dist/define.js
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports._lazyValue = _lazyValue;
|
|
4
|
-
exports._defineLazyProperty = _defineLazyProperty;
|
|
5
|
-
exports._defineLazyProps = _defineLazyProps;
|
|
6
|
-
exports._defineProperty = _defineProperty;
|
|
7
|
-
exports._defineProps = _defineProps;
|
|
8
|
-
exports._defineNonNullishProps = _defineNonNullishProps;
|
|
9
|
-
const object_util_1 = require("./object/object.util");
|
|
10
|
-
const types_1 = require("./types");
|
|
1
|
+
import { _mapObject, _mapValues } from './object/object.util.js';
|
|
2
|
+
import { SKIP } from './types.js';
|
|
11
3
|
/**
|
|
12
4
|
* const value = lazyValue(() => expensiveComputation())
|
|
13
5
|
*
|
|
@@ -17,7 +9,7 @@ const types_1 = require("./types");
|
|
|
17
9
|
*
|
|
18
10
|
* Based on: https://github.com/sindresorhus/lazy-value
|
|
19
11
|
*/
|
|
20
|
-
function _lazyValue(fn) {
|
|
12
|
+
export function _lazyValue(fn) {
|
|
21
13
|
let isCalled = false;
|
|
22
14
|
let result;
|
|
23
15
|
return (() => {
|
|
@@ -42,7 +34,7 @@ function _lazyValue(fn) {
|
|
|
42
34
|
*
|
|
43
35
|
* Based on: https://github.com/sindresorhus/define-lazy-prop
|
|
44
36
|
*/
|
|
45
|
-
function _defineLazyProperty(obj, propertyName, fn) {
|
|
37
|
+
export function _defineLazyProperty(obj, propertyName, fn) {
|
|
46
38
|
const define = (value) => {
|
|
47
39
|
Object.defineProperty(obj, propertyName, { value, enumerable: true, writable: true });
|
|
48
40
|
};
|
|
@@ -63,7 +55,7 @@ function _defineLazyProperty(obj, propertyName, fn) {
|
|
|
63
55
|
/**
|
|
64
56
|
* Like _defineLazyProperty, but allows to define multiple props at once.
|
|
65
57
|
*/
|
|
66
|
-
function _defineLazyProps(obj, props) {
|
|
58
|
+
export function _defineLazyProps(obj, props) {
|
|
67
59
|
Object.entries(props).forEach(([k, fn]) => _defineLazyProperty(obj, k, fn));
|
|
68
60
|
return obj;
|
|
69
61
|
}
|
|
@@ -83,7 +75,7 @@ function _defineLazyProps(obj, props) {
|
|
|
83
75
|
* value: existing obj[prop] value
|
|
84
76
|
*
|
|
85
77
|
*/
|
|
86
|
-
function _defineProperty(obj, prop, pd) {
|
|
78
|
+
export function _defineProperty(obj, prop, pd) {
|
|
87
79
|
return Object.defineProperty(obj, prop, {
|
|
88
80
|
writable: true,
|
|
89
81
|
configurable: true,
|
|
@@ -96,8 +88,8 @@ function _defineProperty(obj, prop, pd) {
|
|
|
96
88
|
* Object.defineProperties with better defaults.
|
|
97
89
|
* See _defineProperty for exact defaults definition.
|
|
98
90
|
*/
|
|
99
|
-
function _defineProps(obj, props) {
|
|
100
|
-
return Object.defineProperties(obj,
|
|
91
|
+
export function _defineProps(obj, props) {
|
|
92
|
+
return Object.defineProperties(obj, _mapValues(props, (_k, pd) => ({
|
|
101
93
|
writable: true,
|
|
102
94
|
configurable: true,
|
|
103
95
|
enumerable: true,
|
|
@@ -108,10 +100,10 @@ function _defineProps(obj, props) {
|
|
|
108
100
|
/**
|
|
109
101
|
* Like _defineProps, but skips props with nullish values.
|
|
110
102
|
*/
|
|
111
|
-
function _defineNonNullishProps(obj, props) {
|
|
112
|
-
return _defineProps(obj,
|
|
103
|
+
export function _defineNonNullishProps(obj, props) {
|
|
104
|
+
return _defineProps(obj, _mapObject(props, (k, pd) => {
|
|
113
105
|
if (pd.value === null || pd.value === undefined)
|
|
114
|
-
return
|
|
106
|
+
return SKIP;
|
|
115
107
|
return [k, pd];
|
|
116
108
|
}));
|
|
117
109
|
}
|
package/dist/deviceIdService.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/// <reference lib="dom" preserve="true" />
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const nanoid_1 = require("./nanoid");
|
|
7
|
-
const hash_util_1 = require("./string/hash.util");
|
|
2
|
+
import { isServerSide } from './env.js';
|
|
3
|
+
import { nanoidBrowser } from './nanoid.js';
|
|
4
|
+
import { hashCode } from './string/hash.util.js';
|
|
8
5
|
// This is in sync with the default length in Nanoid.
|
|
9
6
|
const deviceIdLength = 21;
|
|
10
7
|
/**
|
|
@@ -24,7 +21,7 @@ const deviceIdLength = 21;
|
|
|
24
21
|
*
|
|
25
22
|
* @experimental
|
|
26
23
|
*/
|
|
27
|
-
class DeviceIdService {
|
|
24
|
+
export class DeviceIdService {
|
|
28
25
|
constructor(cfg = {}) {
|
|
29
26
|
this.cfg = {
|
|
30
27
|
localStorageKey: 'deviceId',
|
|
@@ -61,7 +58,7 @@ class DeviceIdService {
|
|
|
61
58
|
}
|
|
62
59
|
const mod = Math.trunc(rate * 1000);
|
|
63
60
|
// console.log('hash: ', hashCode(this.deviceId)) // todo
|
|
64
|
-
return
|
|
61
|
+
return hashCode(this.deviceId) % 1000 < mod;
|
|
65
62
|
}
|
|
66
63
|
/**
|
|
67
64
|
* Deletes the persisted deviceId.
|
|
@@ -82,7 +79,7 @@ class DeviceIdService {
|
|
|
82
79
|
*/
|
|
83
80
|
init() {
|
|
84
81
|
this.deviceId = null;
|
|
85
|
-
if (
|
|
82
|
+
if (isServerSide())
|
|
86
83
|
return;
|
|
87
84
|
try {
|
|
88
85
|
this.deviceId = globalThis.localStorage.getItem(this.cfg.localStorageKey);
|
|
@@ -99,7 +96,7 @@ class DeviceIdService {
|
|
|
99
96
|
}
|
|
100
97
|
if (!this.deviceId) {
|
|
101
98
|
try {
|
|
102
|
-
this.deviceId =
|
|
99
|
+
this.deviceId = nanoidBrowser(deviceIdLength);
|
|
103
100
|
this.debug(`generated new deviceId: ${this.deviceId}`);
|
|
104
101
|
globalThis.localStorage.setItem(this.cfg.localStorageKey, this.deviceId);
|
|
105
102
|
}
|
|
@@ -114,4 +111,3 @@ class DeviceIdService {
|
|
|
114
111
|
console.log('[DeviceIdService]', ...args);
|
|
115
112
|
}
|
|
116
113
|
}
|
|
117
|
-
exports.DeviceIdService = DeviceIdService;
|
package/dist/enum.util.d.ts
CHANGED