socket-function 0.8.8 → 0.8.11
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 +22 -0
- package/package.json +1 -1
- package/src/caching.ts +16 -3
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { observable, Reaction, IObservable } from "mobx";
|
|
2
|
+
import { cacheLimited } from "../src/caching";
|
|
2
3
|
|
|
3
4
|
export function promiseToObservable<T>(promise: Promise<T>): { value: T | undefined } {
|
|
4
5
|
let isDone = false;
|
|
@@ -29,4 +30,25 @@ export function promiseToObservable<T>(promise: Promise<T>): { value: T | undefi
|
|
|
29
30
|
return undefined;
|
|
30
31
|
}
|
|
31
32
|
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function asyncObservable<Output, Key>(maxCount: number, getValue: (key: Key) => Promise<Output>): {
|
|
36
|
+
(key: Key): Output | undefined;
|
|
37
|
+
invalidate(key: Key): void;
|
|
38
|
+
invalidateAll(): void;
|
|
39
|
+
} {
|
|
40
|
+
let cache = cacheLimited(maxCount, (keyJSON: string) => {
|
|
41
|
+
let key = keyJSON ? JSON.parse(keyJSON) : keyJSON;
|
|
42
|
+
// We call inside another function so that synchronous errors still get wrapped in the observable
|
|
43
|
+
let value = (async () => getValue(key))();
|
|
44
|
+
return promiseToObservable(value);
|
|
45
|
+
});
|
|
46
|
+
get["invalidate"] = (key: Key) => {
|
|
47
|
+
cache.invalidate(JSON.stringify(key));
|
|
48
|
+
};
|
|
49
|
+
get["invalidateAll"] = () => cache.invalidateAll();
|
|
50
|
+
function get(key: Key) {
|
|
51
|
+
return cache(JSON.stringify(key)).value;
|
|
52
|
+
}
|
|
53
|
+
return get;
|
|
32
54
|
}
|
package/package.json
CHANGED
package/src/caching.ts
CHANGED
|
@@ -64,10 +64,22 @@ export function cacheLimited<Output, Key>(
|
|
|
64
64
|
// calculating, keeping a consistent output can save (a considerable amount of) time in downstream caches.
|
|
65
65
|
maxCount: number,
|
|
66
66
|
getValue: (key: Key) => Output
|
|
67
|
-
):
|
|
67
|
+
): {
|
|
68
|
+
(key: Key): Output;
|
|
69
|
+
invalidate(key: Key): void;
|
|
70
|
+
invalidateAll(): void;
|
|
71
|
+
} {
|
|
68
72
|
let startingCalculating = new Set<Key>();
|
|
69
73
|
let values = new Map<Key, Output>();
|
|
70
|
-
|
|
74
|
+
get["invalidate"] = (key: Key) => {
|
|
75
|
+
values.delete(key);
|
|
76
|
+
startingCalculating.delete(key);
|
|
77
|
+
};
|
|
78
|
+
get["invalidateAll"] = () => {
|
|
79
|
+
values.clear();
|
|
80
|
+
startingCalculating.clear();
|
|
81
|
+
};
|
|
82
|
+
function get(input: Key) {
|
|
71
83
|
let key = input;
|
|
72
84
|
if (values.has(key)) {
|
|
73
85
|
return values.get(key) as any;
|
|
@@ -91,7 +103,8 @@ export function cacheLimited<Output, Key>(
|
|
|
91
103
|
let value = getValue(input);
|
|
92
104
|
values.set(key, value);
|
|
93
105
|
return value;
|
|
94
|
-
}
|
|
106
|
+
}
|
|
107
|
+
return get;
|
|
95
108
|
}
|
|
96
109
|
|
|
97
110
|
export function cacheWeak<Output, Key extends object>(getValue: (key: Key) => Output): (key: Key) => Output {
|