diff --git a/client/src/track.ts b/client/src/track.ts index 6979c94f2..e56b45e12 100644 --- a/client/src/track.ts +++ b/client/src/track.ts @@ -1,4 +1,3 @@ -import Vue from 'vue'; import { Ref, ref } from '@vue/composition-api'; import { RectBounds } from './utils'; import { @@ -13,6 +12,9 @@ export type ConfidencePair = [string, number]; export type TrackId = number; export type TrackSupportedFeature = ( GeoJSON.Point | GeoJSON.Polygon | GeoJSON.LineString | GeoJSON.Point); +type TrackNotifier = ( + { track, event, oldValue }: { track: Track; event: string; oldValue: unknown } +) => void; export interface StringKeyObject { [key: string]: unknown; } @@ -50,6 +52,7 @@ interface TrackParams { features?: Array; confidencePairs?: Array; attributes?: StringKeyObject; + notifier?: TrackNotifier; } /** @@ -84,7 +87,8 @@ export default class Track { */ revision: Ref; - bus: Vue; + /** A callback to notify about changes to the track. */ + notifier?: TrackNotifier; constructor(trackId: TrackId, { meta = {}, @@ -93,8 +97,8 @@ export default class Track { features = [], confidencePairs = [], attributes = {}, + notifier = undefined, }: TrackParams) { - this.bus = new Vue(); this.trackId = trackId; this.meta = meta; this.attributes = attributes; @@ -106,6 +110,7 @@ export default class Track { this.begin = begin; this.end = end; this.confidencePairs = confidencePairs; + this.notifier = notifier; } get length() { @@ -198,11 +203,13 @@ export default class Track { /* Prevent broadcast until the first feature is initialized */ if (this.isInitialized()) { this.revision.value += 1; - this.bus.$emit('notify', { - track: this, - event: name, - oldValue, - }); + if (this.notifier) { + this.notifier({ + track: this, + event: name, + oldValue, + }); + } } } @@ -327,6 +334,10 @@ export default class Track { } } + setNotifier(notifier?: TrackNotifier) { + this.notifier = notifier; + } + setFeature(feature: Feature, geometry: GeoJSON.Feature[] = []): Feature { const f = this.features[feature.frame] || {}; this.features[feature.frame] = { diff --git a/client/src/use/useTrackStore.ts b/client/src/use/useTrackStore.ts index f750aaa9c..a47062e74 100644 --- a/client/src/use/useTrackStore.ts +++ b/client/src/use/useTrackStore.ts @@ -57,6 +57,7 @@ export default function useTrackStore({ markChangesPending }: UseTrackStoreParam const trackIds: Ref> = ref([]); const canary = ref(0); + function _depend(): number { return canary.value; } @@ -82,7 +83,7 @@ export default function useTrackStore({ markChangesPending }: UseTrackStoreParam } function insertTrack(track: Track, args?: InsertArgs) { - track.bus.$on('notify', onChange); + track.setNotifier(onChange); trackMap.set(track.trackId, track); intervalTree.insert([track.begin, track.end], track.trackId.toString()); if (args && args.afterId) { @@ -117,7 +118,7 @@ export default function useTrackStore({ markChangesPending }: UseTrackStoreParam if (!intervalTree.remove(range, trackId.toString())) { throw new Error(`TrackId ${trackId} with range ${range} not found in tree.`); } - track.bus.$off(); // remove all event listeners + track.setNotifier(undefined); trackMap.delete(trackId); const listIndex = trackIds.value.findIndex((v) => v === trackId); if (listIndex === -1) {