Skip to content

An open-source, self-hosted system for apps built with web technologies.

Take the new Theme's view once the Programs show it, so they change in the Desktop's step

zohayrslilehd6e6f9a6c5f8
diff --git a/source/client/core/link-manager/desktop-preferences-state.ts b/source/client/core/link-manager/desktop-preferences-state.ts
index 54fa8db..4684911 100644
--- a/source/client/core/link-manager/desktop-preferences-state.ts
+++ b/source/client/core/link-manager/desktop-preferences-state.ts
@@ -1,11 +1,23 @@
import { type DesktopPreferences } from "@phreshos/core"
import { Tunnel } from "@the-link/core"
+/**
+ * The longest a change waits for one Program to show it. A Program built before Programs confirmed
+ * what they show never does, and the change goes on without it.
+ */
+const showingLimit = 150
+
/** Complete effective preferences owned by one browser Desktop. */
export default class DesktopPreferencesState {
public readonly tunnel = new Tunnel()
+ /** The Programs told of the current change that have not shown it yet, by their pane. */
+ private readonly showing = new Map<string, Readonly<{ shown: Promise<void>, resolve: () => void }>>()
+
+ /** Settles once the current preferences are shown, here and by every Program told of them. */
+ private settled: Promise<void> = Promise.resolve()
+
public constructor(private current: DesktopPreferences) {}
public get value() {
@@ -13,15 +25,53 @@ export default class DesktopPreferencesState {
return this.current
}
- public async update(preferences: DesktopPreferences) {
+ /**
+ * Makes these the effective preferences and tells every Program that follows them. It settles
+ * once each Program told has shown them, or has had its time: a change the Desktop shows in
+ * one step, such as a new Theme, waits for it, so Programs change in the same step.
+ */
+ public update(preferences: DesktopPreferences) {
- if (samePreferences(this.current, preferences)) return
+ if (samePreferences(this.current, preferences)) return this.settled
this.current = preferences
- // This event is intentionally local: Desktop preferences do not belong
- // to the System transport or to any other Desktop representation.
- await this.tunnel.publish("change", preferences)
+ this.settled = (async () => {
+
+ // This event is intentionally local: Desktop preferences do not belong
+ // to the System transport or to any other Desktop representation.
+ await this.tunnel.publish("change", preferences)
+
+ await Promise.all([...this.showing.values()].map(waiting => waiting.shown))
+ })()
+
+ return this.settled
+ }
+
+ /** One Program is told of the current change; the change is shown once it says so, or its time is up. */
+ public telling(pane: string) {
+
+ this.showing.get(pane)?.resolve()
+
+ let resolve!: () => void
+ const shown = new Promise<void>(settle => { resolve = settle })
+ const timer = setTimeout(() => done(), showingLimit)
+ const done = () => {
+
+ clearTimeout(timer)
+
+ if (this.showing.get(pane)?.shown === shown) this.showing.delete(pane)
+
+ resolve()
+ }
+
+ this.showing.set(pane, { shown, resolve: done })
+ }
+
+ /** A Program says it shows the current preferences. */
+ public shown(pane: string) {
+
+ this.showing.get(pane)?.resolve()
}
}
diff --git a/source/client/view/appearance/desktop-preferences.tsx b/source/client/view/appearance/desktop-preferences.tsx
index 047aac0..2fb729f 100644
--- a/source/client/view/appearance/desktop-preferences.tsx
+++ b/source/client/view/appearance/desktop-preferences.tsx
@@ -36,6 +36,7 @@ export default function DesktopPreferencesProvider({ children }: Readonly<{ chil
const pending = useRef<PendingCommit | null>(null)
const revision = useRef(0)
const transaction = useRef<AppearanceTransaction>(defaultAppearance.transaction)
+ const show = useRef<Show | null>(null)
const update = useCallback(function (change: DesktopPreferencesUpdate) {
if (change.theme !== undefined) {
@@ -59,6 +60,10 @@ export default function DesktopPreferencesProvider({ children }: Readonly<{ chil
transaction.current = value
}, [])
+ const setShow = useCallback(function (value: Show | null) {
+ show.current = value
+ }, [])
+
useEffect(() => {
if (samePreferences(current.current, desired)) return
@@ -80,6 +85,9 @@ export default function DesktopPreferencesProvider({ children }: Readonly<{ chil
pending.current = { preferences: desired, resolve }
setPreferences(desired)
})
+
+ // Programs show the new Theme in their own frames; the new view is taken once they do.
+ if (revision.current === change) await show.current?.(desired)
})
}, [desired])
@@ -105,7 +113,7 @@ export default function DesktopPreferencesProvider({ children }: Readonly<{ chil
pending.current = null
}, [])
- const owner = useMemo(() => ({ preferences, update, setTransaction }), [preferences, update, setTransaction])
+ const owner = useMemo(() => ({ preferences, update, setTransaction, setShow }), [preferences, update, setTransaction, setShow])
return <DesktopPreferencesContext.Provider value={owner}>{children}</DesktopPreferencesContext.Provider>
}
@@ -160,10 +168,14 @@ function useMediaPreference(query: string) {
)
}
+/** Tells the Programs of new preferences, settling once they show them. */
+type Show = (preferences: DesktopPreferences) => Promise<void>
+
interface DesktopPreferencesOwner {
readonly preferences: DesktopPreferences
readonly update: (change: DesktopPreferencesUpdate) => void
readonly setTransaction: (transaction: AppearanceTransaction) => void
+ readonly setShow: (show: Show | null) => void
}
interface PendingCommit {
diff --git a/source/client/view/components/desktop-host/client-process-boundary.ts b/source/client/view/components/desktop-host/client-process-boundary.ts
index bcf3b73..01ed7a2 100644
--- a/source/client/view/components/desktop-host/client-process-boundary.ts
+++ b/source/client/view/components/desktop-host/client-process-boundary.ts
@@ -534,6 +534,9 @@ export default class ClientProcessBoundary extends TheLink {
if (!this.stopDesktopPreferences) this.stopDesktopPreferences = this.authManager.linkManager.desktopPreferences.tunnel.subscribe("change", (preferences: unknown) => {
+ // The change counts as shown once this Program says it shows it too.
+ this.authManager.linkManager.desktopPreferences.telling(this.pane)
+
this.deliver("host-desktop-preferences", "change", preferences).catch(() => undefined)
})
diff --git a/source/client/view/components/desktop-host/host.ts b/source/client/view/components/desktop-host/host.ts
index 1795521..ae0f2c2 100644
--- a/source/client/view/components/desktop-host/host.ts
+++ b/source/client/view/components/desktop-host/host.ts
@@ -327,6 +327,14 @@ export default function host(authManager: AuthManager, pane: string, viewport: D
if (word === "desktopPreferences") return [authManager.linkManager.desktopPreferences.value]
+ // This pane shows the preferences it was last told of.
+ if (word === "desktop-preferences-shown") {
+
+ authManager.linkManager.desktopPreferences.shown(pane)
+
+ return []
+ }
+
if (word === "updateDesktopPreferences") {
await access.require("desktopPreferences", [])
diff --git a/source/client/view/structure/structure.tsx b/source/client/view/structure/structure.tsx
index ede3c9d..ad38535 100644
--- a/source/client/view/structure/structure.tsx
+++ b/source/client/view/structure/structure.tsx
@@ -10,7 +10,7 @@ import usePromise from "@libs/react-promise"
import { useDesktopPreferences } from "../appearance/desktop-preferences"
import { useRememberSystemAppearance } from "../appearance/appearance"
import Authentication from "./authentication/authentication"
-import { useCallback, useEffect, useState } from "react"
+import { useCallback, useEffect, useLayoutEffect, useState } from "react"
import Readiness, { useReadiness, useReady } from "@libs/readiness"
import { type DesktopPreferencesUpdate } from "@phreshos/core"
import { cssEasing } from "../appearance/motion"
@@ -136,7 +136,7 @@ function ConnectedDesktop({ linkManager }: { linkManager: LinkManager }) {
useReady(connectionRequirement)
- const { preferences, update } = useDesktopPreferences()
+ const { preferences, update, setShow } = useDesktopPreferences()
const inbound = ReactTunnel.useFactory(linkManager.$inbound)
@@ -152,6 +152,12 @@ function ConnectedDesktop({ linkManager }: { linkManager: LinkManager }) {
void linkManager.updateDesktopPreferences(preferences)
}, [linkManager, preferences])
+ // A new Theme reaches the Programs within the Desktop's own change, so they change with it.
+ useLayoutEffect(function () {
+ setShow(next => linkManager.updateDesktopPreferences(next))
+ return () => setShow(null)
+ }, [linkManager, setShow])
+
return <LinkManagerContext.Provider value={linkManager}>
<Authentication />
diff --git a/tests/desktop-preferences-state.test.ts b/tests/desktop-preferences-state.test.ts
new file mode 100644
index 0000000..6390076
--- /dev/null
+++ b/tests/desktop-preferences-state.test.ts
@@ -0,0 +1,37 @@
+import assert from "node:assert/strict"
+import { test } from "vitest"
+import DesktopPreferencesState from "@client/core/link-manager/desktop-preferences-state"
+
+const light = { theme: "light", animations: true, scale: 1 } as const
+const dark = { theme: "dark", animations: true, scale: 1 } as const
+
+test("a change settles once every Program told of it shows it", async () => {
+
+ const state = new DesktopPreferencesState(light)
+ state.tunnel.subscribe("change", () => { state.telling("settings") })
+
+ let settled = false
+ const change = state.update(dark).then(() => { settled = true })
+
+ await new Promise(resolve => setTimeout(resolve, 20))
+ assert.equal(settled, false)
+
+ // The same preferences again wait for the same showing.
+ assert.equal(state.update(dark), state.update(dark))
+
+ state.shown("settings")
+ await change
+ assert.equal(settled, true)
+})
+
+test("a Program that never says it shows a change is not waited for past its time", async () => {
+
+ const state = new DesktopPreferencesState(light)
+ state.tunnel.subscribe("change", () => { state.telling("older-program") })
+
+ const started = Date.now()
+ await state.update(dark)
+ const waited = Date.now() - started
+
+ assert.ok(waited >= 140 && waited < 400, `waited ${waited} ms`)
+})