socket-function 0.8.20 → 0.8.22
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 +1 -76
- package/package.json +2 -2
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { observable, Reaction, IObservable } from "mobx";
|
|
2
2
|
import { cacheLimited } from "../src/caching";
|
|
3
3
|
|
|
4
|
-
interface InternalResult {
|
|
4
|
+
export interface InternalResult {
|
|
5
5
|
result: { value: unknown } | undefined;
|
|
6
6
|
}
|
|
7
7
|
export function promiseToObservable<T>(promise: Promise<T>, staleValue?: T): { value: T | undefined } {
|
|
@@ -39,78 +39,3 @@ export function promiseToObservable<T>(promise: Promise<T>, staleValue?: T): { v
|
|
|
39
39
|
}
|
|
40
40
|
}, { internalResult });
|
|
41
41
|
}
|
|
42
|
-
|
|
43
|
-
export function asyncObservable<Output, Args extends any[]>(maxCount: number, getValue: (...args: Args) => Promise<Output>): {
|
|
44
|
-
(...args: Args): Output | undefined;
|
|
45
|
-
invalidate(...args: Args): void;
|
|
46
|
-
invalidateAll(): void;
|
|
47
|
-
} {
|
|
48
|
-
let invalidateAllSeqNum = observable({ seqNum: 1 }, undefined, { deep: false, proxy: false });
|
|
49
|
-
let startingCalculating = new Set<string>();
|
|
50
|
-
let values = new Map<string, {
|
|
51
|
-
valueObs: { value: Output | undefined };
|
|
52
|
-
seqNum: { value: number };
|
|
53
|
-
}>();
|
|
54
|
-
let staleValues = new Map<string, Output>();
|
|
55
|
-
|
|
56
|
-
get["invalidate"] = (...args: Args) => {
|
|
57
|
-
let hash = JSON.stringify(args);
|
|
58
|
-
let value = values.get(hash);
|
|
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
|
-
}
|
|
65
|
-
values.delete(hash);
|
|
66
|
-
startingCalculating.delete(hash);
|
|
67
|
-
value.seqNum.value++;
|
|
68
|
-
};
|
|
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
|
-
|
|
80
|
-
startingCalculating.clear();
|
|
81
|
-
values.clear();
|
|
82
|
-
invalidateAllSeqNum.seqNum++;
|
|
83
|
-
};
|
|
84
|
-
function get(...args: Args) {
|
|
85
|
-
let hash = JSON.stringify(args);
|
|
86
|
-
let value = values.get(hash);
|
|
87
|
-
if (!value) {
|
|
88
|
-
|
|
89
|
-
if (startingCalculating.has(hash)) {
|
|
90
|
-
throw new Error(`Cyclic access in cache`);
|
|
91
|
-
}
|
|
92
|
-
startingCalculating.add(hash);
|
|
93
|
-
|
|
94
|
-
// Not very efficient, but clearing the entire state is a lot easier to do then
|
|
95
|
-
// keep track of the order they are accessed (and it does make MOST accesses
|
|
96
|
-
// MUCH faster).
|
|
97
|
-
if (values.size >= maxCount || staleValues.size > maxCount) {
|
|
98
|
-
values.clear();
|
|
99
|
-
startingCalculating.clear();
|
|
100
|
-
staleValues.clear();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// We call inside another function so that synchronous errors still get wrapped in the observable
|
|
104
|
-
let valueObs = promiseToObservable((async () => getValue(...args))(), staleValues.get(hash));
|
|
105
|
-
value = {
|
|
106
|
-
valueObs,
|
|
107
|
-
seqNum: observable({ value: 1 }, undefined, { deep: false, proxy: false }),
|
|
108
|
-
};
|
|
109
|
-
values.set(hash, value);
|
|
110
|
-
}
|
|
111
|
-
invalidateAllSeqNum.seqNum;
|
|
112
|
-
value.seqNum.value;
|
|
113
|
-
return value.valueObs.value;
|
|
114
|
-
}
|
|
115
|
-
return get;
|
|
116
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "socket-function",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.22",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"dependencies": {
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"debugbreak": "^0.6.5",
|
|
12
12
|
"mobx": "^6.6.2",
|
|
13
13
|
"preact": "^10.10.6",
|
|
14
|
-
"typenode": "^
|
|
14
|
+
"typenode": "^4.8.4",
|
|
15
15
|
"ws": "^8.8.0"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|