Skip to content

Commit 36dbdf2

Browse files
committed
reactive relations
1 parent b222441 commit 36dbdf2

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/api/reactive.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { fetchResourceAsync, fetchRelationAsync } from "./async.js";
1+
import { fetchResourceAsync } from "./async.js";
22

3-
import { shallowRef } from "vue";
3+
import { shallowRef, watch } from "vue";
44

55
// cache for reactive object. 1 entry per requested path (=uri or =uri + relation path)
66
// Hence, 1 resource can have multiple entries if requested via different paths
@@ -37,11 +37,21 @@ async function fetchResourceAndUpdateReactive(uri, reactiveObject) {
3737
}
3838

3939
async function fetchRelationAndUpdateReactive(uri, relation, reactiveObject) {
40-
const resource = await fetchRelationAsync(uri, relation);
40+
const resource = await fetchResourceAsync(uri);
4141

42-
// replacing the complete value of a shallowRef is reactive
43-
// however: if any data within resource changes, the reactivity system wouldn't know (manual trigger of reactivity needed)
44-
reactiveObject.value = resource;
42+
// because resource is a reactive object, `fetchResourceAsync` is triggered, whenever `resource?.data?._links[relation]` changes
43+
watch(
44+
() => resource?.data?._links[relation],
45+
async () => {
46+
console.log(
47+
`${uri} ${relation} changed to ${resource?.data?._links[relation]} - reloading new data...`
48+
);
49+
reactiveObject.value = await fetchResourceAsync(
50+
resource?.data?._links[relation]
51+
);
52+
},
53+
{ immediate: true }
54+
);
4555
}
4656

4757
function getReactiveFromCache(cacheKey) {

0 commit comments

Comments
 (0)