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.
@@ -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
- // NOTE: Not very efficient (invalidates too much), but... makes triggering invalidations much faster/easier,
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, { value: Output | undefined }>();
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
- invalidateSeqNum.seqNum++;
55
+ value.seqNum.value++;
55
56
  };
56
57
  get["invalidateAll"] = () => {
57
58
  startingCalculating.clear();
58
59
  values.clear();
59
- invalidateSeqNum.seqNum++;
60
+ invalidateAllSeqNum.seqNum++;
60
61
  };
61
62
  function get(key: Key) {
62
- invalidateSeqNum.seqNum;
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
- // 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
- }
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
- // We call inside another function so that synchronous errors still get wrapped in the observable
82
- value = promiseToObservable((async () => getValue(key))());
83
- values.set(hash, value);
84
- return value.value;
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.8.15",
3
+ "version": "0.8.16",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {