socket-function 0.8.19 → 0.8.20
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 +29 -7
- 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
|
-
|
|
4
|
+
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,16 +28,16 @@ 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
42
|
|
|
37
43
|
export function asyncObservable<Output, Args extends any[]>(maxCount: number, getValue: (...args: Args) => Promise<Output>): {
|
|
@@ -45,22 +51,37 @@ export function asyncObservable<Output, Args extends any[]>(maxCount: number, ge
|
|
|
45
51
|
valueObs: { value: Output | undefined };
|
|
46
52
|
seqNum: { value: number };
|
|
47
53
|
}>();
|
|
54
|
+
let staleValues = new Map<string, Output>();
|
|
48
55
|
|
|
49
56
|
get["invalidate"] = (...args: Args) => {
|
|
50
57
|
let hash = JSON.stringify(args);
|
|
51
58
|
let value = values.get(hash);
|
|
52
59
|
if (!value) return;
|
|
60
|
+
// Access the value specially, to prevent causing a mobx subscription
|
|
61
|
+
let internalValue = (value.valueObs as any as { internalResult: InternalResult }).internalResult;
|
|
62
|
+
if (internalValue.result) {
|
|
63
|
+
staleValues.set(hash, internalValue.result.value as any);
|
|
64
|
+
}
|
|
53
65
|
values.delete(hash);
|
|
54
66
|
startingCalculating.delete(hash);
|
|
55
67
|
value.seqNum.value++;
|
|
56
68
|
};
|
|
57
69
|
get["invalidateAll"] = () => {
|
|
70
|
+
// HACK: It sucks to have to iterate over everything, but... our maxCount is limited anyway, so... hopefully it isn't too slow?
|
|
71
|
+
// TODO: If we are iterating anyway... we should probably just store a LRU queue for cache eviction...
|
|
72
|
+
for (let [hash, value] of values) {
|
|
73
|
+
// Access the value specially, to prevent causing a mobx subscription
|
|
74
|
+
let internalValue = (value.valueObs as any as { internalResult: InternalResult }).internalResult;
|
|
75
|
+
if (internalValue.result) {
|
|
76
|
+
staleValues.set(hash, internalValue.result.value as any);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
58
80
|
startingCalculating.clear();
|
|
59
81
|
values.clear();
|
|
60
82
|
invalidateAllSeqNum.seqNum++;
|
|
61
83
|
};
|
|
62
84
|
function get(...args: Args) {
|
|
63
|
-
|
|
64
85
|
let hash = JSON.stringify(args);
|
|
65
86
|
let value = values.get(hash);
|
|
66
87
|
if (!value) {
|
|
@@ -73,13 +94,14 @@ export function asyncObservable<Output, Args extends any[]>(maxCount: number, ge
|
|
|
73
94
|
// Not very efficient, but clearing the entire state is a lot easier to do then
|
|
74
95
|
// keep track of the order they are accessed (and it does make MOST accesses
|
|
75
96
|
// MUCH faster).
|
|
76
|
-
if (values.size >= maxCount) {
|
|
97
|
+
if (values.size >= maxCount || staleValues.size > maxCount) {
|
|
77
98
|
values.clear();
|
|
78
99
|
startingCalculating.clear();
|
|
100
|
+
staleValues.clear();
|
|
79
101
|
}
|
|
80
102
|
|
|
81
103
|
// We call inside another function so that synchronous errors still get wrapped in the observable
|
|
82
|
-
let valueObs = promiseToObservable((async () => getValue(...args))());
|
|
104
|
+
let valueObs = promiseToObservable((async () => getValue(...args))(), staleValues.get(hash));
|
|
83
105
|
value = {
|
|
84
106
|
valueObs,
|
|
85
107
|
seqNum: observable({ value: 1 }, undefined, { deep: false, proxy: false }),
|