corexxx 1.0.94 → 1.0.96
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/dist/dcache.d.ts +3 -2
- package/dist/dcache.js +34 -4
- package/dist/dcache.js.map +1 -1
- package/package.json +1 -1
- package/src/dcache.ts +28 -4
package/dist/dcache.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export declare namespace dcache {
|
|
2
|
-
function memoizeAsync(func: Function, timeoutms?: number): (...args: any) => Promise<any>;
|
|
3
|
-
function memoize(func: Function, timeoutms?: number): (...args: any) => any;
|
|
2
|
+
function memoizeAsync(func: Function, timeoutms?: number, customKey?: string): (...args: any) => Promise<any>;
|
|
3
|
+
function memoize(func: Function, timeoutms?: number, customKey?: string): (...args: any) => any;
|
|
4
|
+
function ensureOneCallAsync(func: Function): (...args: any) => Promise<void>;
|
|
4
5
|
}
|
package/dist/dcache.js
CHANGED
|
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.dcache = void 0;
|
|
13
|
+
const dlog_1 = require("./dlog");
|
|
13
14
|
var dcache;
|
|
14
15
|
(function (dcache) {
|
|
15
16
|
/*
|
|
@@ -24,11 +25,11 @@ var dcache;
|
|
|
24
25
|
pow(3, 5); // creates the index "[3,5]" and returns 243
|
|
25
26
|
*/
|
|
26
27
|
// make sure you have used await
|
|
27
|
-
function memoizeAsync(func, timeoutms) {
|
|
28
|
+
function memoizeAsync(func, timeoutms, customKey) {
|
|
28
29
|
const cache = {};
|
|
29
30
|
return function memoized(...args) {
|
|
30
31
|
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
-
const key = JSON.stringify(args);
|
|
32
|
+
const key = customKey || JSON.stringify(args);
|
|
32
33
|
if (!cache[key]) {
|
|
33
34
|
cache[key] = yield func(...args);
|
|
34
35
|
if (timeoutms) {
|
|
@@ -37,16 +38,19 @@ var dcache;
|
|
|
37
38
|
}, timeoutms);
|
|
38
39
|
}
|
|
39
40
|
}
|
|
41
|
+
else {
|
|
42
|
+
dlog_1.dlog.d("perf:avoid call as cache founds");
|
|
43
|
+
}
|
|
40
44
|
return cache[key];
|
|
41
45
|
});
|
|
42
46
|
};
|
|
43
47
|
}
|
|
44
48
|
dcache.memoizeAsync = memoizeAsync;
|
|
45
49
|
// just call
|
|
46
|
-
function memoize(func, timeoutms) {
|
|
50
|
+
function memoize(func, timeoutms, customKey) {
|
|
47
51
|
const cache = {};
|
|
48
52
|
return function memoized(...args) {
|
|
49
|
-
const key = JSON.stringify(args);
|
|
53
|
+
const key = customKey || JSON.stringify(args);
|
|
50
54
|
if (!cache[key]) {
|
|
51
55
|
cache[key] = func(...args);
|
|
52
56
|
if (timeoutms) {
|
|
@@ -55,9 +59,35 @@ var dcache;
|
|
|
55
59
|
}, timeoutms);
|
|
56
60
|
}
|
|
57
61
|
}
|
|
62
|
+
else {
|
|
63
|
+
dlog_1.dlog.d("perf:avoid call as cache founds");
|
|
64
|
+
}
|
|
58
65
|
return cache[key];
|
|
59
66
|
};
|
|
60
67
|
}
|
|
61
68
|
dcache.memoize = memoize;
|
|
69
|
+
function ensureOneCallAsync(func) {
|
|
70
|
+
const cache = {};
|
|
71
|
+
return function memoized(...args) {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
const key = JSON.stringify(args);
|
|
74
|
+
if (!cache[key]) {
|
|
75
|
+
cache[key] = true;
|
|
76
|
+
try {
|
|
77
|
+
yield func(...args);
|
|
78
|
+
cache[key] = false;
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
cache[key] = false;
|
|
82
|
+
throw e;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
dlog_1.dlog.d("Perf:avoid call as another call in progress");
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
dcache.ensureOneCallAsync = ensureOneCallAsync;
|
|
62
92
|
})(dcache = exports.dcache || (exports.dcache = {}));
|
|
63
93
|
//# sourceMappingURL=dcache.js.map
|
package/dist/dcache.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dcache.js","sourceRoot":"","sources":["../src/dcache.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,IAAiB,MAAM,
|
|
1
|
+
{"version":3,"file":"dcache.js","sourceRoot":"","sources":["../src/dcache.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,iCAA8B;AAC9B,IAAiB,MAAM,CAoEtB;AApED,WAAiB,MAAM;IACrB;;;;;;;;;;QAUI;IACJ,gCAAgC;IAChC,SAAgB,YAAY,CAAC,IAAc,EAAE,SAAkB,EAAE,SAAkB;QACjF,MAAM,KAAK,GAAY,EAAE,CAAC;QAC1B,OAAO,SAAe,QAAQ,CAAC,GAAG,IAAS;;gBACzC,MAAM,GAAG,GAAG,SAAS,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;gBAC9C,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;oBACf,KAAK,CAAC,GAAG,CAAC,GAAG,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;oBACjC,IAAI,SAAS,EAAE;wBACb,UAAU,CAAC,GAAG,EAAE;4BACd,KAAK,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;wBACzB,CAAC,EAAE,SAAS,CAAC,CAAC;qBACf;iBACF;qBAAM;oBACL,WAAI,CAAC,CAAC,CAAC,iCAAiC,CAAC,CAAC;iBAC3C;gBACD,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;YACpB,CAAC;SAAA,CAAC;IACJ,CAAC;IAhBe,mBAAY,eAgB3B,CAAA;IAED,YAAY;IACZ,SAAgB,OAAO,CAAC,IAAc,EAAE,SAAkB,EAAE,SAAkB;QAC5E,MAA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CAAC,EApEgB,MAAM,GAAN,cAAM,KAAN,cAAM,QAoEtB"}
|
package/package.json
CHANGED
package/src/dcache.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TObject } from ".";
|
|
2
|
+
import { dlog } from "./dlog";
|
|
2
3
|
export namespace dcache {
|
|
3
4
|
/*
|
|
4
5
|
https://www.sitepoint.com/implementing-memoization-in-javascript/
|
|
@@ -12,10 +13,10 @@ export namespace dcache {
|
|
|
12
13
|
pow(3, 5); // creates the index "[3,5]" and returns 243
|
|
13
14
|
*/
|
|
14
15
|
// make sure you have used await
|
|
15
|
-
export function memoizeAsync(func: Function, timeoutms?: number) {
|
|
16
|
+
export function memoizeAsync(func: Function, timeoutms?: number, customKey?: string) {
|
|
16
17
|
const cache: TObject = {};
|
|
17
18
|
return async function memoized(...args: any) {
|
|
18
|
-
const key = JSON.stringify(args);
|
|
19
|
+
const key = customKey || JSON.stringify(args);
|
|
19
20
|
if (!cache[key]) {
|
|
20
21
|
cache[key] = await func(...args);
|
|
21
22
|
if (timeoutms) {
|
|
@@ -23,16 +24,18 @@ export namespace dcache {
|
|
|
23
24
|
cache[key] = undefined;
|
|
24
25
|
}, timeoutms);
|
|
25
26
|
}
|
|
27
|
+
} else {
|
|
28
|
+
dlog.d("perf:avoid call as cache founds");
|
|
26
29
|
}
|
|
27
30
|
return cache[key];
|
|
28
31
|
};
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
// just call
|
|
32
|
-
export function memoize(func: Function, timeoutms?: number) {
|
|
35
|
+
export function memoize(func: Function, timeoutms?: number, customKey?: string) {
|
|
33
36
|
const cache: TObject = {};
|
|
34
37
|
return function memoized(...args: any) {
|
|
35
|
-
const key = JSON.stringify(args);
|
|
38
|
+
const key = customKey || JSON.stringify(args);
|
|
36
39
|
if (!cache[key]) {
|
|
37
40
|
cache[key] = func(...args);
|
|
38
41
|
if (timeoutms) {
|
|
@@ -40,8 +43,29 @@ export namespace dcache {
|
|
|
40
43
|
cache[key] = undefined;
|
|
41
44
|
}, timeoutms);
|
|
42
45
|
}
|
|
46
|
+
} else {
|
|
47
|
+
dlog.d("perf:avoid call as cache founds");
|
|
43
48
|
}
|
|
44
49
|
return cache[key];
|
|
45
50
|
};
|
|
46
51
|
}
|
|
52
|
+
|
|
53
|
+
export function ensureOneCallAsync(func: Function) {
|
|
54
|
+
const cache: TObject = {};
|
|
55
|
+
return async function memoized(...args: any) {
|
|
56
|
+
const key = JSON.stringify(args);
|
|
57
|
+
if (!cache[key]) {
|
|
58
|
+
cache[key] = true;
|
|
59
|
+
try {
|
|
60
|
+
await func(...args);
|
|
61
|
+
cache[key] = false;
|
|
62
|
+
} catch (e) {
|
|
63
|
+
cache[key] = false;
|
|
64
|
+
throw e;
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
dlog.d("Perf:avoid call as another call in progress");
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
47
71
|
}
|