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, * as well as a function to observe and update the component's clock,
* which automatically stops observing when the component is unmounted. * 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 {{ * @returns {{
* observeClock: () => void, * observeClock: () => void,
* timeMode: import('vue').Ref<string>, * timeMode: import('vue').Ref<string>,
@ -37,16 +38,16 @@ import { TIME_CONTEXT_EVENTS } from '../../api/time/constants.js';
* isRealTimeMode: import('vue').Ref<boolean> * isRealTimeMode: import('vue').Ref<boolean>
* }} * }}
*/ */
export function useClock(openmct, options) { export function useClock(openmct, timeContext = openmct.time) {
let stopObservingClock; let stopObservingClock;
const clock = ref(openmct.time.getClock()); const clock = ref(timeContext.getClock());
onBeforeUnmount(() => stopObservingClock?.()); onBeforeUnmount(() => stopObservingClock?.());
function observeClock() { function observeClock() {
openmct.time.on(TIME_CONTEXT_EVENTS.clockChanged, updateClock); timeContext.on(TIME_CONTEXT_EVENTS.clockChanged, updateClock);
stopObservingClock = () => openmct.time.off(TIME_CONTEXT_EVENTS.clockChanged, updateClock); stopObservingClock = () => timeContext.off(TIME_CONTEXT_EVENTS.clockChanged, updateClock);
} }
function getAllClockMetadata(menuOptions) { function getAllClockMetadata(menuOptions) {
@ -54,8 +55,8 @@ export function useClock(openmct, options) {
? menuOptions ? menuOptions
.map((menuOption) => menuOption.clock) .map((menuOption) => menuOption.clock)
.filter((key, index, array) => key !== undefined && array.indexOf(key) === index) .filter((key, index, array) => key !== undefined && array.indexOf(key) === index)
.map((clockKey) => openmct.time.getAllClocks().find((_clock) => _clock.key === clockKey)) .map((clockKey) => timeContext.getAllClocks().find((_clock) => _clock.key === clockKey))
: openmct.time.getAllClocks(); : timeContext.getAllClocks();
const clockMetadata = clocks.map(getClockMetadata); const clockMetadata = clocks.map(getClockMetadata);
@ -79,7 +80,7 @@ export function useClock(openmct, options) {
} }
function setClock(key) { function setClock(key) {
openmct.time.setClock(key); timeContext.setClock(key);
} }
function updateClock(_clock) { 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, * as well as a function to observe and update offsets changes,
* which automatically stops observing when the component is unmounted. * 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 {{ * @returns {{
* observeClockOffsets: () => void, * observeClockOffsets: () => void,
* offsets: import('vue').Ref<object>, * offsets: import('vue').Ref<object>,
* }} * }}
*/ */
export function useClockOffsets(openmct, options) { export function useClockOffsets(openmct, timeContext = openmct.time) {
let stopObservingClockOffsets; let stopObservingClockOffsets;
const offsets = shallowRef(openmct.time.getClockOffsets()); const offsets = shallowRef(timeContext.getClockOffsets());
onBeforeUnmount(() => stopObservingClockOffsets?.()); onBeforeUnmount(() => stopObservingClockOffsets?.());
function observeClockOffsets() { function observeClockOffsets() {
openmct.time.on(TIME_CONTEXT_EVENTS.clockOffsetsChanged, updateClockOffsets); timeContext.on(TIME_CONTEXT_EVENTS.clockOffsetsChanged, updateClockOffsets);
stopObservingClockOffsets = () => stopObservingClockOffsets = () =>
openmct.time.off(TIME_CONTEXT_EVENTS.clockOffsetsChanged, updateClockOffsets); timeContext.off(TIME_CONTEXT_EVENTS.clockOffsetsChanged, updateClockOffsets);
} }
function updateClockOffsets(_offsets) { 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, * as well as a function to observe and update bounds changes,
* which automatically stops observing when the component is unmounted. * 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 {{ * @returns {{
* observeTimeBounds: () => void, * observeTimeBounds: () => void,
* bounds: import('vue').Ref<object>, * bounds: import('vue').Ref<object>,
* isTick: import('vue').Ref<boolean> * isTick: import('vue').Ref<boolean>
* }} * }}
*/ */
export function useTimeBounds(openmct, options) { export function useTimeBounds(openmct, timeContext = openmct.time) {
let stopObservingTimeBounds; let stopObservingTimeBounds;
const bounds = shallowRef(openmct.time.getBounds()); const bounds = shallowRef(timeContext.getBounds());
const isTick = ref(false); const isTick = ref(false);
onBeforeUnmount(() => stopObservingTimeBounds?.()); onBeforeUnmount(() => stopObservingTimeBounds?.());
function observeTimeBounds(milliseconds = 300) { function observeTimeBounds(milliseconds = 300) {
openmct.time.on(TIME_CONTEXT_EVENTS.boundsChanged, throttle(updateTimeBounds), milliseconds); timeContext.on(TIME_CONTEXT_EVENTS.boundsChanged, throttle(updateTimeBounds), milliseconds);
stopObservingTimeBounds = () => stopObservingTimeBounds = () =>
openmct.time.off(TIME_CONTEXT_EVENTS.boundsChanged, throttle(updateTimeBounds), milliseconds); timeContext.off(TIME_CONTEXT_EVENTS.boundsChanged, throttle(updateTimeBounds), milliseconds);
} }
function updateTimeBounds(_timeBounds, _isTick) { function updateTimeBounds(_timeBounds, _isTick) {

View File

@ -34,6 +34,7 @@ import {
* which automatically stops observing when the component is unmounted. * 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 mode events
* @returns {{ * @returns {{
* observeTimeMode: () => void, * observeTimeMode: () => void,
* timeMode: import('vue').Ref<string>, * timeMode: import('vue').Ref<string>,
@ -41,18 +42,18 @@ import {
* isRealTimeMode: import('vue').Ref<boolean> * isRealTimeMode: import('vue').Ref<boolean>
* }} * }}
*/ */
export function useTimeMode(openmct, options) { export function useTimeMode(openmct, timeContext = openmct.time) {
let stopObservingTimeMode; let stopObservingTimeMode;
const timeMode = ref(openmct.time.getMode()); const timeMode = ref(timeContext.getMode());
const isFixedTimeMode = computed(() => timeMode.value === FIXED_MODE_KEY); const isFixedTimeMode = computed(() => timeMode.value === FIXED_MODE_KEY);
const isRealTimeMode = computed(() => timeMode.value === REALTIME_MODE_KEY); const isRealTimeMode = computed(() => timeMode.value === REALTIME_MODE_KEY);
onBeforeUnmount(() => stopObservingTimeMode?.()); onBeforeUnmount(() => stopObservingTimeMode?.());
function observeTimeMode() { function observeTimeMode() {
openmct.time.on(TIME_CONTEXT_EVENTS.modeChanged, updateTimeMode); timeContext.on(TIME_CONTEXT_EVENTS.modeChanged, updateTimeMode);
stopObservingTimeMode = () => openmct.time.off(TIME_CONTEXT_EVENTS.modeChanged, updateTimeMode); stopObservingTimeMode = () => timeContext.off(TIME_CONTEXT_EVENTS.modeChanged, updateTimeMode);
} }
function getAllModeMetadata() { function getAllModeMetadata() {
@ -81,7 +82,7 @@ export function useTimeMode(openmct, options) {
} }
function setTimeMode(_timeMode) { function setTimeMode(_timeMode) {
openmct.time.setMode(_timeMode); timeContext.setMode(_timeMode);
} }
function updateTimeMode(_timeMode) { function updateTimeMode(_timeMode) {

View File

@ -30,6 +30,7 @@ const DEFAULT_DURATION_FORMATTER = 'duration';
* which automatically stops observing when the component is unmounted. * 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 time system events
* @returns {{ * @returns {{
* observeTimeSystem: () => void, * observeTimeSystem: () => void,
* timeSystemKey: import('vue').Ref<string>, * timeSystemKey: import('vue').Ref<string>,
@ -38,10 +39,10 @@ const DEFAULT_DURATION_FORMATTER = 'duration';
* isTimeSystemUTCBased: import('vue').Ref<boolean> * isTimeSystemUTCBased: import('vue').Ref<boolean>
* }} * }}
*/ */
export function useTimeSystem(openmct, options) { export function useTimeSystem(openmct, timeContext = openmct.time) {
let stopObservingTimeSystem; let stopObservingTimeSystem;
const currentTimeSystem = openmct.time.getTimeSystem(); const currentTimeSystem = timeContext.getTimeSystem();
const timeSystemKey = ref(currentTimeSystem.key); const timeSystemKey = ref(currentTimeSystem.key);
const timeSystemFormatter = ref(getFormatter(openmct, currentTimeSystem.timeFormat)); const timeSystemFormatter = ref(getFormatter(openmct, currentTimeSystem.timeFormat));
@ -53,8 +54,8 @@ export function useTimeSystem(openmct, options) {
onBeforeUnmount(() => stopObservingTimeSystem?.()); onBeforeUnmount(() => stopObservingTimeSystem?.());
function observeTimeSystem() { function observeTimeSystem() {
openmct.time.on('timeSystemChanged', updateTimeSystem); timeContext.on('timeSystemChanged', updateTimeSystem);
stopObservingTimeSystem = () => openmct.time.off('timeSystemChanged', updateTimeSystem); stopObservingTimeSystem = () => timeContext.off('timeSystemChanged', updateTimeSystem);
} }
function updateTimeSystem(timeSystem) { function updateTimeSystem(timeSystem) {