Skip to content

Commit cf28f8f

Browse files
committed
fix(wheneverImageLoaded): run callback when switching images
The bug is that the current implementation of wheneverImageLoaded  only watches a derived boolean that only toggles when the image goes from not-loaded to loaded. If the imageId changes from one valid ID to another (with both returning true), the callback never fires.
1 parent aac0d0d commit cf28f8f

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { useImageCacheStore } from '@/src/store/image-cache';
22
import { Maybe } from '@/src/types';
33
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
4-
import { whenever } from '@vueuse/core';
5-
import { computed, MaybeRef, unref, WatchCallback } from 'vue';
4+
import { computed, MaybeRef, unref, WatchCallback, watch } from 'vue';
65

76
export function wheneverImageLoaded(
87
imageId: MaybeRef<Maybe<string>>,
@@ -15,11 +14,16 @@ export function wheneverImageLoaded(
1514
return imageCacheStore.imageById[id];
1615
});
1716
const imageIsLoaded = computed(() => image.value?.loaded.value ?? false);
18-
return whenever(imageIsLoaded, (newVal, oldVal, onCleanup) => {
19-
cb(
20-
{ id: unref(imageId)!, imageData: image.value!.getVtkImageData() },
21-
undefined,
22-
onCleanup
23-
);
24-
});
17+
return watch(
18+
[() => unref(imageId), imageIsLoaded],
19+
([id, loaded], _, onCleanup) => {
20+
if (loaded && id) {
21+
cb(
22+
{ id, imageData: image.value!.getVtkImageData() },
23+
undefined,
24+
onCleanup
25+
);
26+
}
27+
}
28+
);
2529
}

0 commit comments

Comments
 (0)