proxy-facades 1.4.0 → 1.5.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/dist/commonjs/retsync.d.ts +8 -2
- package/dist/commonjs/retsync.d.ts.map +1 -1
- package/dist/commonjs/retsync.js +31 -7
- package/dist/commonjs/retsync.js.map +1 -1
- package/dist/esm/retsync.d.ts +8 -2
- package/dist/esm/retsync.d.ts.map +1 -1
- package/dist/esm/retsync.js +30 -7
- package/dist/esm/retsync.js.map +1 -1
- package/package.json +1 -1
- package/retsync.ts +33 -8
|
@@ -4,14 +4,19 @@ type Retsync2promiseOptions = {
|
|
|
4
4
|
*/
|
|
5
5
|
checkSaved?: boolean;
|
|
6
6
|
};
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Internal / used in frameworks.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Retsync2promiseCall {
|
|
8
11
|
}
|
|
9
12
|
/**
|
|
10
|
-
* Global state.
|
|
13
|
+
* Global state. Internal / shared with frameworks.
|
|
11
14
|
*/
|
|
12
15
|
export declare const _global: {
|
|
13
16
|
resolvedPromiseValues: WeakMap<Promise<any>, unknown>;
|
|
14
17
|
retsync2promiseCall?: Retsync2promiseCall;
|
|
18
|
+
globalObj: {};
|
|
19
|
+
resourcePromises: import("./Util").DefaultMap<unknown, Map<string | number | undefined, Promise<unknown>>>;
|
|
15
20
|
};
|
|
16
21
|
/**
|
|
17
22
|
* Let's you run retsync code and wait, till it is finished.
|
|
@@ -35,6 +40,7 @@ export declare function promise2retsync<T>(savedPromise: Promise<T>): T;
|
|
|
35
40
|
* @param loaderFn
|
|
36
41
|
* @param idObj object to associate this call to. undefined means globally and the idKey primitive value is the only key.
|
|
37
42
|
* @param idKey Additional primitive key under idObj.
|
|
43
|
+
* @see cleanResource
|
|
38
44
|
*/
|
|
39
45
|
export declare function asyncResource2retsync<T>(loaderFn: () => Promise<T>, idObj: object | undefined, idKey?: (string | number)): T;
|
|
40
46
|
export declare class RetsyncWaitsForPromiseException extends Error {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retsync.d.ts","sourceRoot":"","sources":["../../retsync.ts"],"names":[],"mappings":"AASA,KAAK,sBAAsB,GAAG;IAC1B;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACvB,CAAA;AAED,
|
|
1
|
+
{"version":3,"file":"retsync.d.ts","sourceRoot":"","sources":["../../retsync.ts"],"names":[],"mappings":"AASA,KAAK,sBAAsB,GAAG;IAC1B;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACvB,CAAA;AAED;;GAEG;AACH,qBAAa,mBAAmB;CAE/B;AAED;;GAEG;AACH,eAAO,MAAM,OAAO;;0BAEM,mBAAmB;;;CAG5C,CAAA;AAED;;;;GAIG;AACH,wBAAsB,eAAe,CAAC,CAAC,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,OAAO,GAAE,sBAA2B,GAAG,OAAO,CAAC,CAAC,CAAC,CA0DhH;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAM9D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAK,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC,MAAM,GAAC,MAAM,CAAC,GAAG,CAAC,CAyBzH;AAqBD,qBAAa,+BAAgC,SAAQ,KAAK;IACtD,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IACtB;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;gBAET,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC;CAIpC;AAED,wBAAgB,6BAA6B,SAI5C;;AAID;;GAEG"}
|
package/dist/commonjs/retsync.js
CHANGED
|
@@ -5,20 +5,26 @@
|
|
|
5
5
|
// Semantics: Retsync code must be repeatable. It can change state, as long as that leads to the same result when repeated.
|
|
6
6
|
// It does not mean strictly deterministic (may be for while in the same sync block??) because resources that are fetched, can change over time. Therefore some inner user's retsync code might subscribe to change events and invalidate asyncResource2retsync's cached promises when there are such changes.
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.RetsyncWaitsForPromiseException = exports._global = void 0;
|
|
8
|
+
exports.RetsyncWaitsForPromiseException = exports._global = exports.Retsync2promiseCall = void 0;
|
|
9
9
|
exports.retsync2promise = retsync2promise;
|
|
10
10
|
exports.promise2retsync = promise2retsync;
|
|
11
11
|
exports.asyncResource2retsync = asyncResource2retsync;
|
|
12
12
|
exports.checkThatCallerHandlesRetsync = checkThatCallerHandlesRetsync;
|
|
13
13
|
const Util_1 = require("./Util");
|
|
14
|
+
/**
|
|
15
|
+
* Internal / used in frameworks.
|
|
16
|
+
*/
|
|
14
17
|
class Retsync2promiseCall {
|
|
15
18
|
}
|
|
19
|
+
exports.Retsync2promiseCall = Retsync2promiseCall;
|
|
16
20
|
/**
|
|
17
|
-
* Global state.
|
|
21
|
+
* Global state. Internal / shared with frameworks.
|
|
18
22
|
*/
|
|
19
23
|
exports._global = new class {
|
|
20
24
|
constructor() {
|
|
21
25
|
this.resolvedPromiseValues = new WeakMap();
|
|
26
|
+
this.globalObj = {};
|
|
27
|
+
this.resourcePromises = (0, Util_1.newDefaultWeakMap)((key) => new Map());
|
|
22
28
|
}
|
|
23
29
|
};
|
|
24
30
|
/**
|
|
@@ -45,7 +51,7 @@ async function retsync2promise(repeatableFn, options = {}) {
|
|
|
45
51
|
return runRepeatableFn();
|
|
46
52
|
}
|
|
47
53
|
catch (e) {
|
|
48
|
-
if (e instanceof RetsyncWaitsForPromiseException) {
|
|
54
|
+
if (e != null && e instanceof RetsyncWaitsForPromiseException) {
|
|
49
55
|
if (e.checkSaved || (e.checkSaved === undefined && options.checkSaved !== false)) {
|
|
50
56
|
const optionHint = `Hint: See also: Retsync2promiseOptions#checkSaved`;
|
|
51
57
|
// Check if repeatableFn is behaving in repeatable symantics and saves the promise
|
|
@@ -93,8 +99,6 @@ function promise2retsync(savedPromise) {
|
|
|
93
99
|
}
|
|
94
100
|
throw new RetsyncWaitsForPromiseException(savedPromise);
|
|
95
101
|
}
|
|
96
|
-
const globalObj = {};
|
|
97
|
-
const resourcePromises = (0, Util_1.newDefaultWeakMap)((key) => new Map());
|
|
98
102
|
/**
|
|
99
103
|
* Makes async code usable in retsync code.
|
|
100
104
|
* <p>
|
|
@@ -106,10 +110,15 @@ const resourcePromises = (0, Util_1.newDefaultWeakMap)((key) => new Map());
|
|
|
106
110
|
* @param loaderFn
|
|
107
111
|
* @param idObj object to associate this call to. undefined means globally and the idKey primitive value is the only key.
|
|
108
112
|
* @param idKey Additional primitive key under idObj.
|
|
113
|
+
* @see cleanResource
|
|
109
114
|
*/
|
|
110
115
|
function asyncResource2retsync(loaderFn, idObj, idKey) {
|
|
111
|
-
|
|
112
|
-
|
|
116
|
+
// Validity check:
|
|
117
|
+
if (!idObj && !idKey) {
|
|
118
|
+
throw new Error("Either idObj or idKey must be specified");
|
|
119
|
+
}
|
|
120
|
+
idObj = idObj || exports._global.globalObj;
|
|
121
|
+
const promisesForIdObj = exports._global.resourcePromises.get(idObj);
|
|
113
122
|
let promise = promisesForIdObj.get(idKey);
|
|
114
123
|
if (!promise) {
|
|
115
124
|
promise = loaderFn();
|
|
@@ -126,6 +135,21 @@ function asyncResource2retsync(loaderFn, idObj, idKey) {
|
|
|
126
135
|
throw e;
|
|
127
136
|
}
|
|
128
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Cleans the promise and therefore the result behind the given obj+key like used in {@link asyncResource2retsync}.
|
|
140
|
+
* Call this i.e. on the event that the resource has change and you want to "invalidate the cached value", so it will be fetched fresh next time.
|
|
141
|
+
* @param idObj
|
|
142
|
+
* @param idKey
|
|
143
|
+
*/
|
|
144
|
+
function cleanResource(idObj, idKey) {
|
|
145
|
+
// Validity check:
|
|
146
|
+
if (!idObj && !idKey) {
|
|
147
|
+
throw new Error("Either idObj or idKey must be specified");
|
|
148
|
+
}
|
|
149
|
+
idObj = idObj || exports._global.globalObj;
|
|
150
|
+
const promisesForIdObj = exports._global.resourcePromises.get(idObj);
|
|
151
|
+
promisesForIdObj.delete(idKey);
|
|
152
|
+
}
|
|
129
153
|
class RetsyncWaitsForPromiseException extends Error {
|
|
130
154
|
constructor(promise) {
|
|
131
155
|
super("Some retsync style code (see call stack / caller of promise2retsync) wants to await an async operation. To make this possible, you need to wrapt it at some ancestor caller level with retsync2promise. I.e. 'const result = await retsync2promise(() => {...your **retryable*** - synchronous code...}});");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retsync.js","sourceRoot":"","sources":["../../retsync.ts"],"names":[],"mappings":";AAAA,wCAAwC;AACxC,0JAA0J;AAC1J,2CAA2C;AAC3C,2HAA2H;AAC3H,8SAA8S;;;
|
|
1
|
+
{"version":3,"file":"retsync.js","sourceRoot":"","sources":["../../retsync.ts"],"names":[],"mappings":";AAAA,wCAAwC;AACxC,0JAA0J;AAC1J,2CAA2C;AAC3C,2HAA2H;AAC3H,8SAA8S;;;AAkC9S,0CA0DC;AAMD,0CAMC;AAeD,sDAyBC;AAkCD,sEAIC;AAnLD,iCAAyC;AASzC;;GAEG;AACH,MAAa,mBAAmB;CAE/B;AAFD,kDAEC;AAED;;GAEG;AACU,QAAA,OAAO,GAAG,IAAI;IAAA;QACvB,0BAAqB,GAAG,IAAI,OAAO,EAAyB,CAAC;QAE7D,cAAS,GAAG,EAAE,CAAC;QACf,qBAAgB,GAAG,IAAA,wBAAiB,EAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,GAAG,EAAiD,CAAC,CAAA;IAC3G,CAAC;CAAA,CAAA;AAED;;;;GAIG;AACI,KAAK,UAAU,eAAe,CAAI,YAAqB,EAAE,UAAkC,EAAE;IAChG;;OAEG;IACH,SAAS,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AC,eAAO,CAAC,mBAAmB,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,mQAAmQ,CAAC,CAAC;IACzR,CAAC;AACL,CAAC;AAID;;GAEG;AACH,oEAAoE;AACpE,uIAAuI;AACvI,GAAG"}
|
package/dist/esm/retsync.d.ts
CHANGED
|
@@ -4,14 +4,19 @@ type Retsync2promiseOptions = {
|
|
|
4
4
|
*/
|
|
5
5
|
checkSaved?: boolean;
|
|
6
6
|
};
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Internal / used in frameworks.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Retsync2promiseCall {
|
|
8
11
|
}
|
|
9
12
|
/**
|
|
10
|
-
* Global state.
|
|
13
|
+
* Global state. Internal / shared with frameworks.
|
|
11
14
|
*/
|
|
12
15
|
export declare const _global: {
|
|
13
16
|
resolvedPromiseValues: WeakMap<Promise<any>, unknown>;
|
|
14
17
|
retsync2promiseCall?: Retsync2promiseCall;
|
|
18
|
+
globalObj: {};
|
|
19
|
+
resourcePromises: import("./Util").DefaultMap<unknown, Map<string | number | undefined, Promise<unknown>>>;
|
|
15
20
|
};
|
|
16
21
|
/**
|
|
17
22
|
* Let's you run retsync code and wait, till it is finished.
|
|
@@ -35,6 +40,7 @@ export declare function promise2retsync<T>(savedPromise: Promise<T>): T;
|
|
|
35
40
|
* @param loaderFn
|
|
36
41
|
* @param idObj object to associate this call to. undefined means globally and the idKey primitive value is the only key.
|
|
37
42
|
* @param idKey Additional primitive key under idObj.
|
|
43
|
+
* @see cleanResource
|
|
38
44
|
*/
|
|
39
45
|
export declare function asyncResource2retsync<T>(loaderFn: () => Promise<T>, idObj: object | undefined, idKey?: (string | number)): T;
|
|
40
46
|
export declare class RetsyncWaitsForPromiseException extends Error {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retsync.d.ts","sourceRoot":"","sources":["../../retsync.ts"],"names":[],"mappings":"AASA,KAAK,sBAAsB,GAAG;IAC1B;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACvB,CAAA;AAED,
|
|
1
|
+
{"version":3,"file":"retsync.d.ts","sourceRoot":"","sources":["../../retsync.ts"],"names":[],"mappings":"AASA,KAAK,sBAAsB,GAAG;IAC1B;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACvB,CAAA;AAED;;GAEG;AACH,qBAAa,mBAAmB;CAE/B;AAED;;GAEG;AACH,eAAO,MAAM,OAAO;;0BAEM,mBAAmB;;;CAG5C,CAAA;AAED;;;;GAIG;AACH,wBAAsB,eAAe,CAAC,CAAC,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,OAAO,GAAE,sBAA2B,GAAG,OAAO,CAAC,CAAC,CAAC,CA0DhH;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAM9D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAK,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC,MAAM,GAAC,MAAM,CAAC,GAAG,CAAC,CAyBzH;AAqBD,qBAAa,+BAAgC,SAAQ,KAAK;IACtD,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IACtB;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;gBAET,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC;CAIpC;AAED,wBAAgB,6BAA6B,SAI5C;;AAID;;GAEG"}
|
package/dist/esm/retsync.js
CHANGED
|
@@ -4,14 +4,19 @@
|
|
|
4
4
|
// Semantics: Retsync code must be repeatable. It can change state, as long as that leads to the same result when repeated.
|
|
5
5
|
// It does not mean strictly deterministic (may be for while in the same sync block??) because resources that are fetched, can change over time. Therefore some inner user's retsync code might subscribe to change events and invalidate asyncResource2retsync's cached promises when there are such changes.
|
|
6
6
|
import { newDefaultWeakMap } from "./Util";
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Internal / used in frameworks.
|
|
9
|
+
*/
|
|
10
|
+
export class Retsync2promiseCall {
|
|
8
11
|
}
|
|
9
12
|
/**
|
|
10
|
-
* Global state.
|
|
13
|
+
* Global state. Internal / shared with frameworks.
|
|
11
14
|
*/
|
|
12
15
|
export const _global = new class {
|
|
13
16
|
constructor() {
|
|
14
17
|
this.resolvedPromiseValues = new WeakMap();
|
|
18
|
+
this.globalObj = {};
|
|
19
|
+
this.resourcePromises = newDefaultWeakMap((key) => new Map());
|
|
15
20
|
}
|
|
16
21
|
};
|
|
17
22
|
/**
|
|
@@ -38,7 +43,7 @@ export async function retsync2promise(repeatableFn, options = {}) {
|
|
|
38
43
|
return runRepeatableFn();
|
|
39
44
|
}
|
|
40
45
|
catch (e) {
|
|
41
|
-
if (e instanceof RetsyncWaitsForPromiseException) {
|
|
46
|
+
if (e != null && e instanceof RetsyncWaitsForPromiseException) {
|
|
42
47
|
if (e.checkSaved || (e.checkSaved === undefined && options.checkSaved !== false)) {
|
|
43
48
|
const optionHint = `Hint: See also: Retsync2promiseOptions#checkSaved`;
|
|
44
49
|
// Check if repeatableFn is behaving in repeatable symantics and saves the promise
|
|
@@ -86,8 +91,6 @@ export function promise2retsync(savedPromise) {
|
|
|
86
91
|
}
|
|
87
92
|
throw new RetsyncWaitsForPromiseException(savedPromise);
|
|
88
93
|
}
|
|
89
|
-
const globalObj = {};
|
|
90
|
-
const resourcePromises = newDefaultWeakMap((key) => new Map());
|
|
91
94
|
/**
|
|
92
95
|
* Makes async code usable in retsync code.
|
|
93
96
|
* <p>
|
|
@@ -99,10 +102,15 @@ const resourcePromises = newDefaultWeakMap((key) => new Map());
|
|
|
99
102
|
* @param loaderFn
|
|
100
103
|
* @param idObj object to associate this call to. undefined means globally and the idKey primitive value is the only key.
|
|
101
104
|
* @param idKey Additional primitive key under idObj.
|
|
105
|
+
* @see cleanResource
|
|
102
106
|
*/
|
|
103
107
|
export function asyncResource2retsync(loaderFn, idObj, idKey) {
|
|
104
|
-
|
|
105
|
-
|
|
108
|
+
// Validity check:
|
|
109
|
+
if (!idObj && !idKey) {
|
|
110
|
+
throw new Error("Either idObj or idKey must be specified");
|
|
111
|
+
}
|
|
112
|
+
idObj = idObj || _global.globalObj;
|
|
113
|
+
const promisesForIdObj = _global.resourcePromises.get(idObj);
|
|
106
114
|
let promise = promisesForIdObj.get(idKey);
|
|
107
115
|
if (!promise) {
|
|
108
116
|
promise = loaderFn();
|
|
@@ -119,6 +127,21 @@ export function asyncResource2retsync(loaderFn, idObj, idKey) {
|
|
|
119
127
|
throw e;
|
|
120
128
|
}
|
|
121
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Cleans the promise and therefore the result behind the given obj+key like used in {@link asyncResource2retsync}.
|
|
132
|
+
* Call this i.e. on the event that the resource has change and you want to "invalidate the cached value", so it will be fetched fresh next time.
|
|
133
|
+
* @param idObj
|
|
134
|
+
* @param idKey
|
|
135
|
+
*/
|
|
136
|
+
function cleanResource(idObj, idKey) {
|
|
137
|
+
// Validity check:
|
|
138
|
+
if (!idObj && !idKey) {
|
|
139
|
+
throw new Error("Either idObj or idKey must be specified");
|
|
140
|
+
}
|
|
141
|
+
idObj = idObj || _global.globalObj;
|
|
142
|
+
const promisesForIdObj = _global.resourcePromises.get(idObj);
|
|
143
|
+
promisesForIdObj.delete(idKey);
|
|
144
|
+
}
|
|
122
145
|
export class RetsyncWaitsForPromiseException extends Error {
|
|
123
146
|
constructor(promise) {
|
|
124
147
|
super("Some retsync style code (see call stack / caller of promise2retsync) wants to await an async operation. To make this possible, you need to wrapt it at some ancestor caller level with retsync2promise. I.e. 'const result = await retsync2promise(() => {...your **retryable*** - synchronous code...}});");
|
package/dist/esm/retsync.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retsync.js","sourceRoot":"","sources":["../../retsync.ts"],"names":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,OAAO,CAAC,EAAE,CAAC;QACP,6DAA6D;QAC7D,IAAG,CAAC,YAAY,+BAA+B,EAAE,CAAC;YAC9C,CAAC,CAAC,UAAU,GAAG,KAAK,CAAC;QACzB,CAAC;QACD,MAAM,CAAC,CAAC;IACZ,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAC,KAAyB,EAAE,KAAuB;IACrE,kBAAkB;IAClB,IAAG,CAAC,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;IAC/D,CAAC;IAED,KAAK,GAAG,KAAK,IAAI,OAAO,CAAC,SAAS,CAAC;IACnC,MAAM,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC7D,gBAAgB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACnC,CAAC;AAID,MAAM,OAAO,+BAAgC,SAAQ,KAAK;IAOtD,YAAY,OAAqB;QAC7B,KAAK,CAAC,4SAA4S,CAAC,CAAC;QACpT,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;CACJ;AAED,MAAM,UAAU,6BAA6B;IACzC,IAAG,CAAC,OAAO,CAAC,mBAAmB,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,mQAAmQ,CAAC,CAAC;IACzR,CAAC;AACL,CAAC;AAID;;GAEG;AACH,oEAAoE;AACpE,uIAAuI;AACvI,GAAG"}
|
package/package.json
CHANGED
package/retsync.ts
CHANGED
|
@@ -14,16 +14,21 @@ type Retsync2promiseOptions = {
|
|
|
14
14
|
checkSaved?: boolean
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Internal / used in frameworks.
|
|
19
|
+
*/
|
|
20
|
+
export class Retsync2promiseCall {
|
|
18
21
|
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
/**
|
|
22
|
-
* Global state.
|
|
25
|
+
* Global state. Internal / shared with frameworks.
|
|
23
26
|
*/
|
|
24
27
|
export const _global = new class {
|
|
25
28
|
resolvedPromiseValues = new WeakMap<Promise<any>, unknown>();
|
|
26
29
|
retsync2promiseCall?: Retsync2promiseCall;
|
|
30
|
+
globalObj = {};
|
|
31
|
+
resourcePromises = newDefaultWeakMap((key) => new Map<string | number | undefined, Promise<unknown>>())
|
|
27
32
|
}
|
|
28
33
|
|
|
29
34
|
/**
|
|
@@ -50,7 +55,7 @@ export async function retsync2promise<T>(repeatableFn: () => T, options: Retsync
|
|
|
50
55
|
try {
|
|
51
56
|
return runRepeatableFn();
|
|
52
57
|
} catch (e) {
|
|
53
|
-
if (e instanceof RetsyncWaitsForPromiseException) {
|
|
58
|
+
if (e != null && e instanceof RetsyncWaitsForPromiseException) {
|
|
54
59
|
if (e.checkSaved || (e.checkSaved === undefined && options.checkSaved !== false)) {
|
|
55
60
|
const optionHint = `Hint: See also: Retsync2promiseOptions#checkSaved`
|
|
56
61
|
// Check if repeatableFn is behaving in repeatable symantics and saves the promise
|
|
@@ -103,9 +108,6 @@ export function promise2retsync<T>(savedPromise: Promise<T>): T {
|
|
|
103
108
|
throw new RetsyncWaitsForPromiseException(savedPromise)
|
|
104
109
|
}
|
|
105
110
|
|
|
106
|
-
const globalObj = {};
|
|
107
|
-
const resourcePromises = newDefaultWeakMap((key) => new Map<string | number | undefined, Promise<unknown>>())
|
|
108
|
-
|
|
109
111
|
/**
|
|
110
112
|
* Makes async code usable in retsync code.
|
|
111
113
|
* <p>
|
|
@@ -117,11 +119,17 @@ const resourcePromises = newDefaultWeakMap((key) => new Map<string | number | un
|
|
|
117
119
|
* @param loaderFn
|
|
118
120
|
* @param idObj object to associate this call to. undefined means globally and the idKey primitive value is the only key.
|
|
119
121
|
* @param idKey Additional primitive key under idObj.
|
|
122
|
+
* @see cleanResource
|
|
120
123
|
*/
|
|
121
124
|
export function asyncResource2retsync<T>(loaderFn: ()=> Promise<T>, idObj: object | undefined, idKey?: (string|number)): T {
|
|
122
|
-
|
|
125
|
+
// Validity check:
|
|
126
|
+
if(!idObj && !idKey) {
|
|
127
|
+
throw new Error("Either idObj or idKey must be specified");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
idObj = idObj || _global.globalObj;
|
|
123
131
|
|
|
124
|
-
const promisesForIdObj = resourcePromises.get(idObj);
|
|
132
|
+
const promisesForIdObj = _global.resourcePromises.get(idObj);
|
|
125
133
|
|
|
126
134
|
let promise = promisesForIdObj.get(idKey);
|
|
127
135
|
if(!promise) {
|
|
@@ -140,6 +148,23 @@ export function asyncResource2retsync<T>(loaderFn: ()=> Promise<T>, idObj: objec
|
|
|
140
148
|
}
|
|
141
149
|
}
|
|
142
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Cleans the promise and therefore the result behind the given obj+key like used in {@link asyncResource2retsync}.
|
|
153
|
+
* Call this i.e. on the event that the resource has change and you want to "invalidate the cached value", so it will be fetched fresh next time.
|
|
154
|
+
* @param idObj
|
|
155
|
+
* @param idKey
|
|
156
|
+
*/
|
|
157
|
+
function cleanResource(idObj: object | undefined, idKey?: (string|number)) {
|
|
158
|
+
// Validity check:
|
|
159
|
+
if(!idObj && !idKey) {
|
|
160
|
+
throw new Error("Either idObj or idKey must be specified");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
idObj = idObj || _global.globalObj;
|
|
164
|
+
const promisesForIdObj = _global.resourcePromises.get(idObj);
|
|
165
|
+
promisesForIdObj.delete(idKey);
|
|
166
|
+
}
|
|
167
|
+
|
|
143
168
|
|
|
144
169
|
|
|
145
170
|
export class RetsyncWaitsForPromiseException extends Error {
|