socket-function 0.8.19 → 0.8.21
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/mobx/promiseToObservable.tsx +10 -63
- package/package.json +1 -1
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
import { observable, Reaction, IObservable } from "mobx";
|
|
2
2
|
import { cacheLimited } from "../src/caching";
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export interface InternalResult {
|
|
5
|
+
result: { value: unknown } | undefined;
|
|
6
|
+
}
|
|
7
|
+
export function promiseToObservable<T>(promise: Promise<T>, staleValue?: T): { value: T | undefined } {
|
|
5
8
|
let isDone = false;
|
|
6
9
|
let error: unknown;
|
|
7
10
|
let result: T | undefined;
|
|
8
11
|
|
|
12
|
+
let internalResult: InternalResult = { result: undefined };
|
|
13
|
+
|
|
9
14
|
let isDoneTrigger = observable({
|
|
10
15
|
value: false
|
|
11
16
|
});
|
|
12
17
|
promise.then(
|
|
13
18
|
r => {
|
|
19
|
+
internalResult.result = { value: r };
|
|
14
20
|
result = r;
|
|
15
21
|
isDoneTrigger.value = true;
|
|
16
22
|
isDone = true;
|
|
@@ -22,73 +28,14 @@ export function promiseToObservable<T>(promise: Promise<T>): { value: T | undefi
|
|
|
22
28
|
}
|
|
23
29
|
);
|
|
24
30
|
|
|
25
|
-
return {
|
|
31
|
+
return Object.assign({
|
|
26
32
|
get value() {
|
|
27
33
|
if (isDone) {
|
|
28
34
|
if (error) throw error;
|
|
29
35
|
return result;
|
|
30
36
|
}
|
|
31
37
|
isDoneTrigger.value;
|
|
32
|
-
return
|
|
38
|
+
return staleValue;
|
|
33
39
|
}
|
|
34
|
-
};
|
|
40
|
+
}, { internalResult });
|
|
35
41
|
}
|
|
36
|
-
|
|
37
|
-
export function asyncObservable<Output, Args extends any[]>(maxCount: number, getValue: (...args: Args) => Promise<Output>): {
|
|
38
|
-
(...args: Args): Output | undefined;
|
|
39
|
-
invalidate(...args: Args): void;
|
|
40
|
-
invalidateAll(): void;
|
|
41
|
-
} {
|
|
42
|
-
let invalidateAllSeqNum = observable({ seqNum: 1 }, undefined, { deep: false, proxy: false });
|
|
43
|
-
let startingCalculating = new Set<string>();
|
|
44
|
-
let values = new Map<string, {
|
|
45
|
-
valueObs: { value: Output | undefined };
|
|
46
|
-
seqNum: { value: number };
|
|
47
|
-
}>();
|
|
48
|
-
|
|
49
|
-
get["invalidate"] = (...args: Args) => {
|
|
50
|
-
let hash = JSON.stringify(args);
|
|
51
|
-
let value = values.get(hash);
|
|
52
|
-
if (!value) return;
|
|
53
|
-
values.delete(hash);
|
|
54
|
-
startingCalculating.delete(hash);
|
|
55
|
-
value.seqNum.value++;
|
|
56
|
-
};
|
|
57
|
-
get["invalidateAll"] = () => {
|
|
58
|
-
startingCalculating.clear();
|
|
59
|
-
values.clear();
|
|
60
|
-
invalidateAllSeqNum.seqNum++;
|
|
61
|
-
};
|
|
62
|
-
function get(...args: Args) {
|
|
63
|
-
|
|
64
|
-
let hash = JSON.stringify(args);
|
|
65
|
-
let value = values.get(hash);
|
|
66
|
-
if (!value) {
|
|
67
|
-
|
|
68
|
-
if (startingCalculating.has(hash)) {
|
|
69
|
-
throw new Error(`Cyclic access in cache`);
|
|
70
|
-
}
|
|
71
|
-
startingCalculating.add(hash);
|
|
72
|
-
|
|
73
|
-
// Not very efficient, but clearing the entire state is a lot easier to do then
|
|
74
|
-
// keep track of the order they are accessed (and it does make MOST accesses
|
|
75
|
-
// MUCH faster).
|
|
76
|
-
if (values.size >= maxCount) {
|
|
77
|
-
values.clear();
|
|
78
|
-
startingCalculating.clear();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// We call inside another function so that synchronous errors still get wrapped in the observable
|
|
82
|
-
let valueObs = promiseToObservable((async () => getValue(...args))());
|
|
83
|
-
value = {
|
|
84
|
-
valueObs,
|
|
85
|
-
seqNum: observable({ value: 1 }, undefined, { deep: false, proxy: false }),
|
|
86
|
-
};
|
|
87
|
-
values.set(hash, value);
|
|
88
|
-
}
|
|
89
|
-
invalidateAllSeqNum.seqNum;
|
|
90
|
-
value.seqNum.value;
|
|
91
|
-
return value.valueObs.value;
|
|
92
|
-
}
|
|
93
|
-
return get;
|
|
94
|
-
}
|