From 852ee74094bb5e8dab28ad748400c3888a07f3e6 Mon Sep 17 00:00:00 2001 From: David Tsay Date: Thu, 16 May 2024 12:37:47 -0700 Subject: [PATCH] change composables to allow for timeContexts --- src/plugins/timeConductor/useClock.js | 17 +++++++++-------- src/plugins/timeConductor/useClockOffsets.js | 11 ++++++----- src/plugins/timeConductor/useTimeBounds.js | 11 ++++++----- src/plugins/timeConductor/useTimeMode.js | 11 ++++++----- src/plugins/timeConductor/useTimeSystem.js | 9 +++++---- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/plugins/timeConductor/useClock.js b/src/plugins/timeConductor/useClock.js index 864d9b289a..812f93dca8 100644 --- a/src/plugins/timeConductor/useClock.js +++ b/src/plugins/timeConductor/useClock.js @@ -29,7 +29,8 @@ import { TIME_CONTEXT_EVENTS } from '../../api/time/constants.js'; * as well as a function to observe and update the component's clock, * which automatically stops observing when the component is unmounted. * - * @param {OpenMCT} openmct the Open MCT API + * @param {OpenMCT} [openmct] the Open MCT API + * @param {TimeContext} [timeContext] the time context to use for time API clock events * @returns {{ * observeClock: () => void, * timeMode: import('vue').Ref, @@ -37,16 +38,16 @@ import { TIME_CONTEXT_EVENTS } from '../../api/time/constants.js'; * isRealTimeMode: import('vue').Ref * }} */ -export function useClock(openmct, options) { +export function useClock(openmct, timeContext = openmct.time) { let stopObservingClock; - const clock = ref(openmct.time.getClock()); + const clock = ref(timeContext.getClock()); onBeforeUnmount(() => stopObservingClock?.()); function observeClock() { - openmct.time.on(TIME_CONTEXT_EVENTS.clockChanged, updateClock); - stopObservingClock = () => openmct.time.off(TIME_CONTEXT_EVENTS.clockChanged, updateClock); + timeContext.on(TIME_CONTEXT_EVENTS.clockChanged, updateClock); + stopObservingClock = () => timeContext.off(TIME_CONTEXT_EVENTS.clockChanged, updateClock); } function getAllClockMetadata(menuOptions) { @@ -54,8 +55,8 @@ export function useClock(openmct, options) { ? menuOptions .map((menuOption) => menuOption.clock) .filter((key, index, array) => key !== undefined && array.indexOf(key) === index) - .map((clockKey) => openmct.time.getAllClocks().find((_clock) => _clock.key === clockKey)) - : openmct.time.getAllClocks(); + .map((clockKey) => timeContext.getAllClocks().find((_clock) => _clock.key === clockKey)) + : timeContext.getAllClocks(); const clockMetadata = clocks.map(getClockMetadata); @@ -79,7 +80,7 @@ export function useClock(openmct, options) { } function setClock(key) { - openmct.time.setClock(key); + timeContext.setClock(key); } function updateClock(_clock) { diff --git a/src/plugins/timeConductor/useClockOffsets.js b/src/plugins/timeConductor/useClockOffsets.js index 9f02a23b0a..ac0a00085f 100644 --- a/src/plugins/timeConductor/useClockOffsets.js +++ b/src/plugins/timeConductor/useClockOffsets.js @@ -29,23 +29,24 @@ import { TIME_CONTEXT_EVENTS } from '../../api/time/constants.js'; * as well as a function to observe and update offsets changes, * which automatically stops observing when the component is unmounted. * - * @param {OpenMCT} openmct the Open MCT API + * @param {OpenMCT} [openmct] the Open MCT API + * @param {TimeContext} [timeContext] the time context to use for time API clock offsets events * @returns {{ * observeClockOffsets: () => void, * offsets: import('vue').Ref, * }} */ -export function useClockOffsets(openmct, options) { +export function useClockOffsets(openmct, timeContext = openmct.time) { let stopObservingClockOffsets; - const offsets = shallowRef(openmct.time.getClockOffsets()); + const offsets = shallowRef(timeContext.getClockOffsets()); onBeforeUnmount(() => stopObservingClockOffsets?.()); function observeClockOffsets() { - openmct.time.on(TIME_CONTEXT_EVENTS.clockOffsetsChanged, updateClockOffsets); + timeContext.on(TIME_CONTEXT_EVENTS.clockOffsetsChanged, updateClockOffsets); stopObservingClockOffsets = () => - openmct.time.off(TIME_CONTEXT_EVENTS.clockOffsetsChanged, updateClockOffsets); + timeContext.off(TIME_CONTEXT_EVENTS.clockOffsetsChanged, updateClockOffsets); } function updateClockOffsets(_offsets) { diff --git a/src/plugins/timeConductor/useTimeBounds.js b/src/plugins/timeConductor/useTimeBounds.js index 4a5b5c9078..b32f92b068 100644 --- a/src/plugins/timeConductor/useTimeBounds.js +++ b/src/plugins/timeConductor/useTimeBounds.js @@ -30,25 +30,26 @@ import throttle from '../../utils/throttle.js'; * as well as a function to observe and update bounds changes, * which automatically stops observing when the component is unmounted. * - * @param {OpenMCT} openmct the Open MCT API + * @param {OpenMCT} [openmct] the Open MCT API + * @param {TimeContext} [timeContext] the time context to use for time API bounds events * @returns {{ * observeTimeBounds: () => void, * bounds: import('vue').Ref, * isTick: import('vue').Ref * }} */ -export function useTimeBounds(openmct, options) { +export function useTimeBounds(openmct, timeContext = openmct.time) { let stopObservingTimeBounds; - const bounds = shallowRef(openmct.time.getBounds()); + const bounds = shallowRef(timeContext.getBounds()); const isTick = ref(false); onBeforeUnmount(() => stopObservingTimeBounds?.()); function observeTimeBounds(milliseconds = 300) { - openmct.time.on(TIME_CONTEXT_EVENTS.boundsChanged, throttle(updateTimeBounds), milliseconds); + timeContext.on(TIME_CONTEXT_EVENTS.boundsChanged, throttle(updateTimeBounds), milliseconds); stopObservingTimeBounds = () => - openmct.time.off(TIME_CONTEXT_EVENTS.boundsChanged, throttle(updateTimeBounds), milliseconds); + timeContext.off(TIME_CONTEXT_EVENTS.boundsChanged, throttle(updateTimeBounds), milliseconds); } function updateTimeBounds(_timeBounds, _isTick) { diff --git a/src/plugins/timeConductor/useTimeMode.js b/src/plugins/timeConductor/useTimeMode.js index 33c6489ba2..f591ba8246 100644 --- a/src/plugins/timeConductor/useTimeMode.js +++ b/src/plugins/timeConductor/useTimeMode.js @@ -34,6 +34,7 @@ import { * which automatically stops observing when the component is unmounted. * * @param {OpenMCT} openmct the Open MCT API + * @param {TimeContext} [timeContext] the time context to use for time API mode events * @returns {{ * observeTimeMode: () => void, * timeMode: import('vue').Ref, @@ -41,18 +42,18 @@ import { * isRealTimeMode: import('vue').Ref * }} */ -export function useTimeMode(openmct, options) { +export function useTimeMode(openmct, timeContext = openmct.time) { let stopObservingTimeMode; - const timeMode = ref(openmct.time.getMode()); + const timeMode = ref(timeContext.getMode()); const isFixedTimeMode = computed(() => timeMode.value === FIXED_MODE_KEY); const isRealTimeMode = computed(() => timeMode.value === REALTIME_MODE_KEY); onBeforeUnmount(() => stopObservingTimeMode?.()); function observeTimeMode() { - openmct.time.on(TIME_CONTEXT_EVENTS.modeChanged, updateTimeMode); - stopObservingTimeMode = () => openmct.time.off(TIME_CONTEXT_EVENTS.modeChanged, updateTimeMode); + timeContext.on(TIME_CONTEXT_EVENTS.modeChanged, updateTimeMode); + stopObservingTimeMode = () => timeContext.off(TIME_CONTEXT_EVENTS.modeChanged, updateTimeMode); } function getAllModeMetadata() { @@ -81,7 +82,7 @@ export function useTimeMode(openmct, options) { } function setTimeMode(_timeMode) { - openmct.time.setMode(_timeMode); + timeContext.setMode(_timeMode); } function updateTimeMode(_timeMode) { diff --git a/src/plugins/timeConductor/useTimeSystem.js b/src/plugins/timeConductor/useTimeSystem.js index 6092376f84..547cf49c98 100644 --- a/src/plugins/timeConductor/useTimeSystem.js +++ b/src/plugins/timeConductor/useTimeSystem.js @@ -30,6 +30,7 @@ const DEFAULT_DURATION_FORMATTER = 'duration'; * which automatically stops observing when the component is unmounted. * * @param {OpenMCT} openmct the Open MCT API + * @param {TimeContext} [timeContext] the time context to use for time API time system events * @returns {{ * observeTimeSystem: () => void, * timeSystemKey: import('vue').Ref, @@ -38,10 +39,10 @@ const DEFAULT_DURATION_FORMATTER = 'duration'; * isTimeSystemUTCBased: import('vue').Ref * }} */ -export function useTimeSystem(openmct, options) { +export function useTimeSystem(openmct, timeContext = openmct.time) { let stopObservingTimeSystem; - const currentTimeSystem = openmct.time.getTimeSystem(); + const currentTimeSystem = timeContext.getTimeSystem(); const timeSystemKey = ref(currentTimeSystem.key); const timeSystemFormatter = ref(getFormatter(openmct, currentTimeSystem.timeFormat)); @@ -53,8 +54,8 @@ export function useTimeSystem(openmct, options) { onBeforeUnmount(() => stopObservingTimeSystem?.()); function observeTimeSystem() { - openmct.time.on('timeSystemChanged', updateTimeSystem); - stopObservingTimeSystem = () => openmct.time.off('timeSystemChanged', updateTimeSystem); + timeContext.on('timeSystemChanged', updateTimeSystem); + stopObservingTimeSystem = () => timeContext.off('timeSystemChanged', updateTimeSystem); } function updateTimeSystem(timeSystem) {