socket-function 0.8.15 → 0.8.16
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 +32 -25
- package/package.json +1 -1
|
@@ -39,11 +39,12 @@ export function asyncObservable<Output, Key>(maxCount: number, getValue: (key: K
|
|
|
39
39
|
invalidate(key: Key): void;
|
|
40
40
|
invalidateAll(): void;
|
|
41
41
|
} {
|
|
42
|
-
|
|
43
|
-
// making an invalidate depend on the total render time, not the total cached state size.
|
|
44
|
-
let invalidateSeqNum = observable({ seqNum: 1 }, undefined, { deep: false, proxy: false });
|
|
42
|
+
let invalidateAllSeqNum = observable({ seqNum: 1 }, undefined, { deep: false, proxy: false });
|
|
45
43
|
let startingCalculating = new Set<string>();
|
|
46
|
-
let values = new Map<string, {
|
|
44
|
+
let values = new Map<string, {
|
|
45
|
+
valueObs: { value: Output | undefined };
|
|
46
|
+
seqNum: { value: number };
|
|
47
|
+
}>();
|
|
47
48
|
|
|
48
49
|
get["invalidate"] = (key: Key) => {
|
|
49
50
|
let hash = JSON.stringify(key);
|
|
@@ -51,37 +52,43 @@ export function asyncObservable<Output, Key>(maxCount: number, getValue: (key: K
|
|
|
51
52
|
if (!value) return;
|
|
52
53
|
values.delete(hash);
|
|
53
54
|
startingCalculating.delete(hash);
|
|
54
|
-
|
|
55
|
+
value.seqNum.value++;
|
|
55
56
|
};
|
|
56
57
|
get["invalidateAll"] = () => {
|
|
57
58
|
startingCalculating.clear();
|
|
58
59
|
values.clear();
|
|
59
|
-
|
|
60
|
+
invalidateAllSeqNum.seqNum++;
|
|
60
61
|
};
|
|
61
62
|
function get(key: Key) {
|
|
62
|
-
|
|
63
|
+
|
|
63
64
|
let hash = JSON.stringify(key);
|
|
64
65
|
let value = values.get(hash);
|
|
65
|
-
if (value) {
|
|
66
|
-
return value.value;
|
|
67
|
-
}
|
|
68
|
-
if (startingCalculating.has(hash)) {
|
|
69
|
-
throw new Error(`Cyclic access in cache`);
|
|
70
|
-
}
|
|
71
|
-
startingCalculating.add(hash);
|
|
66
|
+
if (!value) {
|
|
72
67
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
80
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
// We call inside another function so that synchronous errors still get wrapped in the observable
|
|
82
|
+
let valueObs = promiseToObservable((async () => getValue(key))());
|
|
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;
|
|
85
92
|
}
|
|
86
93
|
return get;
|
|
87
94
|
}
|