@statezero/core 0.1.34 → 0.1.36
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.
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { computed, getCurrentInstance, onBeforeUnmount } from
|
|
2
|
-
import { querysetStoreRegistry } from
|
|
3
|
-
import { metricRegistry } from
|
|
4
|
-
import { syncManager } from
|
|
5
|
-
import {
|
|
1
|
+
import { computed, getCurrentInstance, onBeforeUnmount } from "vue";
|
|
2
|
+
import { querysetStoreRegistry } from "../../syncEngine/registries/querysetStoreRegistry";
|
|
3
|
+
import { metricRegistry } from "../../syncEngine/registries/metricRegistry";
|
|
4
|
+
import { syncManager } from "../../syncEngine/sync";
|
|
5
|
+
import { wrappedQuerysetCache } from "./reactivity.js";
|
|
6
|
+
import { v7 } from "uuid";
|
|
6
7
|
syncManager.followAllQuerysets = false;
|
|
7
8
|
export const querysets = new Map(); // Map of composableId -> queryset
|
|
8
9
|
function updateSyncManager() {
|
|
@@ -13,11 +14,16 @@ function updateSyncManager() {
|
|
|
13
14
|
export function useQueryset(querysetFactory) {
|
|
14
15
|
const instance = getCurrentInstance();
|
|
15
16
|
if (!instance) {
|
|
16
|
-
throw new Error(
|
|
17
|
+
throw new Error("useQueryset must be called within a component setup function");
|
|
17
18
|
}
|
|
18
19
|
const composableId = v7();
|
|
19
20
|
let lastQueryset = null;
|
|
20
21
|
onBeforeUnmount(() => {
|
|
22
|
+
// Clear cache when component unmounts
|
|
23
|
+
if (lastQueryset?.semanticKey &&
|
|
24
|
+
wrappedQuerysetCache.has(lastQueryset.semanticKey)) {
|
|
25
|
+
wrappedQuerysetCache.delete(lastQueryset.semanticKey);
|
|
26
|
+
}
|
|
21
27
|
querysets.delete(composableId);
|
|
22
28
|
updateSyncManager();
|
|
23
29
|
});
|
|
@@ -27,6 +33,12 @@ export function useQueryset(querysetFactory) {
|
|
|
27
33
|
const queryset = original?.queryset || original;
|
|
28
34
|
// Only update if queryset actually changed
|
|
29
35
|
if (lastQueryset !== queryset) {
|
|
36
|
+
// Clear cache for previous queryset if it changed
|
|
37
|
+
if (lastQueryset?.semanticKey &&
|
|
38
|
+
lastQueryset !== queryset &&
|
|
39
|
+
wrappedQuerysetCache.has(lastQueryset.semanticKey)) {
|
|
40
|
+
wrappedQuerysetCache.delete(lastQueryset.semanticKey);
|
|
41
|
+
}
|
|
30
42
|
querysets.set(composableId, queryset);
|
|
31
43
|
updateSyncManager();
|
|
32
44
|
lastQueryset = queryset;
|
|
@@ -16,3 +16,4 @@ export function ModelAdaptor(modelInstance: Object, reactivityFn?: Function): an
|
|
|
16
16
|
*/
|
|
17
17
|
export function QuerySetAdaptor(liveQuerySet: Object, reactivityFn?: Function): any | import("vue").Ref;
|
|
18
18
|
export function MetricAdaptor(metric: any): any;
|
|
19
|
+
export const wrappedQuerysetCache: Map<any, any>;
|
|
@@ -57,17 +57,7 @@ export function QuerySetAdaptor(liveQuerySet, reactivityFn = reactive) {
|
|
|
57
57
|
const cacheKey = queryset.semanticKey;
|
|
58
58
|
// Check the cache first
|
|
59
59
|
if (cacheKey && wrappedQuerysetCache.has(cacheKey)) {
|
|
60
|
-
|
|
61
|
-
// Refresh the cached wrapper with current data
|
|
62
|
-
const freshData = [...liveQuerySet];
|
|
63
|
-
if (reactivityFn === ref) {
|
|
64
|
-
cachedWrapper.value = freshData;
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
cachedWrapper.splice(0, cachedWrapper.length);
|
|
68
|
-
cachedWrapper.push(...freshData);
|
|
69
|
-
}
|
|
70
|
-
return cachedWrapper;
|
|
60
|
+
return wrappedQuerysetCache.get(cacheKey);
|
|
71
61
|
}
|
|
72
62
|
const querysetAst = queryset.build();
|
|
73
63
|
// Make the queryset reactive using the specified function
|
|
@@ -89,10 +79,10 @@ export function QuerySetAdaptor(liveQuerySet, reactivityFn = reactive) {
|
|
|
89
79
|
// Subscribe to queryset events indefinitely
|
|
90
80
|
querysetEventEmitter.on(eventName, renderHandler);
|
|
91
81
|
/* Dont delete the innocuous looking queryset.length check. There is some weird interaction
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
82
|
+
with vue, where when we load an empty queryset from the cache, the reactivity completely breaks.
|
|
83
|
+
I wasted over 2 days on this bug, and it won't show up in the e2e tests because it just impacts the
|
|
84
|
+
vue reactivity. If this causes performance issues, and needs to be refactored. Make sure you understand
|
|
85
|
+
the vue reactivity interaction correctly and find a different way to fix the broken reactivity for empty querysets */
|
|
96
86
|
if (cacheKey && liveQuerySet && liveQuerySet.length > 0) {
|
|
97
87
|
wrappedQuerysetCache.set(cacheKey, wrapper);
|
|
98
88
|
}
|
|
@@ -108,10 +98,7 @@ export function MetricAdaptor(metric) {
|
|
|
108
98
|
const cacheKey = `${configKey}::${modelName}::${metric.metricType}::${metric.field}::${hash(querysetAst)}`;
|
|
109
99
|
// Check the cache first
|
|
110
100
|
if (cacheKey && wrappedMetricCache.has(cacheKey)) {
|
|
111
|
-
|
|
112
|
-
// Refresh the cached wrapper with current metric value
|
|
113
|
-
cachedWrapper.value = metric.value;
|
|
114
|
-
return cachedWrapper;
|
|
101
|
+
return wrappedMetricCache.get(cacheKey);
|
|
115
102
|
}
|
|
116
103
|
// Create a reactive reference with the initial value
|
|
117
104
|
const wrapper = ref(metric.value);
|
|
@@ -129,10 +116,11 @@ export function MetricAdaptor(metric) {
|
|
|
129
116
|
}
|
|
130
117
|
};
|
|
131
118
|
// Only listen for metric render events
|
|
132
|
-
metricEventEmitter.on(
|
|
119
|
+
metricEventEmitter.on('metric::render', metricRenderHandler);
|
|
133
120
|
// Store in cache
|
|
134
121
|
if (cacheKey) {
|
|
135
122
|
wrappedMetricCache.set(cacheKey, wrapper);
|
|
136
123
|
}
|
|
137
124
|
return wrapper;
|
|
138
125
|
}
|
|
126
|
+
export { wrappedQuerysetCache };
|
package/package.json
CHANGED