Skip to content

Commit f74e236

Browse files
committed
fix: prevent fetching on deactivated state
1 parent 3dde2f6 commit f74e236

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

packages/components-vue/src/components/loader/ContentFetch.vue

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
type FunctionalComponent,
3131
type DefineComponent,
3232
computed,
33+
onActivated,
34+
onDeactivated,
3335
} from "vue";
3436
import _ from "lodash";
3537
@@ -87,13 +89,18 @@
8789
const fetchedContent = ref<T>();
8890
const firstLoad = ref(false);
8991
const hydrated = ref(false);
92+
/**
93+
* Whether component was deactivated by keep-alive
94+
*/
95+
const deactivated = ref(false);
9096
/**
9197
* Hydrate content
9298
*
9399
* Useful with firebase client approach
94100
*/
95101
const hydrate: tHydrate<T> = (newContent, newErrors) => {
96102
// prevent hydration
103+
if (deactivated.value) return;
97104
if (fetchedContent.value && !hydrated.value) return (hydrated.value = true);
98105
if (!props.preventAutoload && !firstLoad.value) return;
99106
@@ -107,6 +114,9 @@
107114
}
108115
109116
async function refresh() {
117+
// prevent refresh
118+
if (deactivated.value) return;
119+
110120
try {
111121
if (props.promise || props.hydratablePromise || props.url) {
112122
loading.value = true;
@@ -150,7 +160,21 @@
150160
// Allow the parent to manually force an update
151161
emit("refresh", refresh);
152162
153-
// refetch on url or promise change
163+
function validatePromiseLike(newPromise: any, oldPromise: any) {
164+
/**
165+
* The same promise would trigger the watcher
166+
* We assume here that the same promise is provided
167+
*/
168+
const possibleSamePromise = !!newPromise && !!oldPromise;
169+
170+
// prevent muntiple requests
171+
if (newPromise === oldPromise || !!possibleSamePromise) return;
172+
173+
// refresh
174+
if (!loading.value && !!newPromise) refresh();
175+
}
176+
177+
// lifecycle, refetch on url or promise change
154178
watch(
155179
() => props.url,
156180
(newUrl, oldUrl) => {
@@ -162,37 +186,29 @@
162186
},
163187
{ immediate: false }
164188
);
165-
watch(
166-
() => props.promise,
167-
(newPromise, oldPromise) => {
168-
/**
169-
* The same promise would trigger the watcher
170-
* We assume here that the same promise is provided
171-
*/
172-
const possibleSamePromise = !!newPromise && !!oldPromise;
173-
174-
// prevent muntiple requests
175-
if (newPromise === oldPromise || !!possibleSamePromise) return;
176-
177-
// refresh
178-
if (!loading.value && !!newPromise) refresh();
179-
},
180-
{ immediate: false }
181-
);
182-
// load if the firstLoad was prevented
189+
watch(() => props.promise, validatePromiseLike, { immediate: false });
190+
watch(() => props.hydratablePromise, validatePromiseLike, { immediate: false });
183191
watch(
184192
() => props.preventAutoload,
185193
(prevent) => {
194+
// load if the firstLoad was prevented
186195
if (!prevent && !firstLoad.value) refresh();
187196
},
188197
{ immediate: false }
189198
);
190-
// Refresh if payload changes
191199
watch(
192200
() => props.payload,
193201
(newPayload, oldPayload) => {
202+
// Refresh if payload changes
194203
if (firstLoad.value && !_.isEqual(newPayload, oldPayload)) refresh();
195204
},
196205
{ immediate: false }
197206
);
207+
208+
onActivated(() => {
209+
deactivated.value = false;
210+
});
211+
onDeactivated(() => {
212+
deactivated.value = true;
213+
});
198214
</script>

0 commit comments

Comments
 (0)