|
1 |
| -import { fetchResourceAsync, fetchRelationAsync } from "./async.js"; |
| 1 | +import { fetchResourceAsync } from "./async.js"; |
2 | 2 |
|
3 |
| -import { shallowRef } from "vue"; |
| 3 | +import { shallowRef, watch } from "vue"; |
4 | 4 |
|
5 | 5 | // cache for reactive object. 1 entry per requested path (=uri or =uri + relation path)
|
6 | 6 | // Hence, 1 resource can have multiple entries if requested via different paths
|
@@ -37,11 +37,21 @@ async function fetchResourceAndUpdateReactive(uri, reactiveObject) {
|
37 | 37 | }
|
38 | 38 |
|
39 | 39 | async function fetchRelationAndUpdateReactive(uri, relation, reactiveObject) {
|
40 |
| - const resource = await fetchRelationAsync(uri, relation); |
| 40 | + const resource = await fetchResourceAsync(uri); |
41 | 41 |
|
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 | + ); |
45 | 55 | }
|
46 | 56 |
|
47 | 57 | function getReactiveFromCache(cacheKey) {
|
|
0 commit comments