change composables to allow for timeContexts

This commit is contained in:
David Tsay 2024-05-16 12:37:47 -07:00
parent bd1c7d9da0
commit 852ee74094
5 changed files with 32 additions and 27 deletions

View File

@ -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<string>,
@ -37,16 +38,16 @@ import { TIME_CONTEXT_EVENTS } from '../../api/time/constants.js';
* isRealTimeMode: import('vue').Ref<boolean>
* }}
*/
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) {

View File

@ -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<object>,
* }}
*/
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) {

View File

@ -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<object>,
* isTick: import('vue').Ref<boolean>
* }}
*/
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) {

View File

@ -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<string>,
@ -41,18 +42,18 @@ import {
* isRealTimeMode: import('vue').Ref<boolean>
* }}
*/
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) {

View File

@ -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<string>,
@ -38,10 +39,10 @@ const DEFAULT_DURATION_FORMATTER = 'duration';
* isTimeSystemUTCBased: import('vue').Ref<boolean>
* }}
*/
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) {