From 8deac106855da739de9bf4cc76dc8f54bdac54cf Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Sun, 22 Jan 2023 13:08:20 +0100 Subject: [PATCH] Wait until the UI is visible before checking for updates --- theatre/studio/src/checkForUpdates.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/theatre/studio/src/checkForUpdates.ts b/theatre/studio/src/checkForUpdates.ts index 0d8dff7..b5ea02a 100644 --- a/theatre/studio/src/checkForUpdates.ts +++ b/theatre/studio/src/checkForUpdates.ts @@ -1,4 +1,5 @@ -import {val} from '@theatre/dataverse' +import {pointerToPrism, val} from '@theatre/dataverse' +import {defer} from '@theatre/shared/utils/defer' import type {$IntentionalAny} from '@theatre/shared/utils/types' import getStudio from './getStudio' import type {UpdateCheckerResponse} from './store/types' @@ -6,7 +7,30 @@ import type {UpdateCheckerResponse} from './store/types' const UPDATE_CHECK_INTERVAL = 30 * 60 * 1000 // check for updates every 30 minutes const TIME_TO_WAIT_ON_ERROR = 1000 * 60 * 60 // an hour +/** + * Returns a promise that will resolve when the UI is visible. If the UI is hidden, it'll + * wait for it to become visible. + */ +async function waitTilUIIsVisible(): Promise { + const visibilityStatePt = getStudio().atomP.ahistoric.visibilityState + if (val(visibilityStatePt) === 'everythingIsVisible') return + const deferred = defer() + + const unsub = pointerToPrism(visibilityStatePt).onStale(() => { + const newVal = val(visibilityStatePt) + if (newVal === 'everythingIsVisible') { + unsub() + deferred.resolve(undefined) + } + }) + + return deferred.promise +} + export default async function checkForUpdates() { + // let's wait a bit in case the user has called for the UI to be hidden. + await wait(500) + await waitTilUIIsVisible() while (true) { const state = val(getStudio().atomP.ahistoric.updateChecker) if (state) {