An open-source, self-hosted system for apps built with web technologies.
Opening: a family's default applies after the exact type's, and every default change is announced
zohayrslilehf3ddf8ef084f
diff --git a/source/client/view/components/desktop-host/announcements.ts b/source/client/view/components/desktop-host/announcements.tsindex 97a208d..29ae5b7 100644--- a/source/client/view/components/desktop-host/announcements.ts+++ b/source/client/view/components/desktop-host/announcements.ts@@ -236,6 +236,11 @@ export default function useAnnouncements(authManager: AuthManager, panes: Map<stpostVisible(access => access.all(), "host-opening", "resolve", "system", request, program)}, [postVisible]))+ opening.useSubscribe("/default-change", useCallback((type: unknown, program: unknown) => {++ postVisible(access => access.all(), "host-opening", "changeDefault", "system", type, program)+ }, [postVisible]))+permissions.useSubscribe("/request", useCallback((request: unknown) => {const program = permissionProgram(request)diff --git a/source/server/core/opening-manager.ts b/source/server/core/opening-manager.tsindex 31607fa..1d96319 100644--- a/source/server/core/opening-manager.ts+++ b/source/server/core/opening-manager.ts@@ -1,20 +1,20 @@import { TheLink } from "@the-link/core"import { Subscribe } from "@the-link/core/decorators"-import { opensType, parseOpenTarget, type OpenRequestSnapshot, type OpenTarget } from "@phreshos/core"+import { opensType, parseOpenTarget, parseOpenType, type OpenRequestSnapshot, type OpenTarget } from "@phreshos/core"import shortIdentity from "@libs/short-identity"import type AuthManager from "./link-manager/auth-manager/auth-manager"import type Process from "./link-manager/auth-manager/process-manager/process"import { endpointReference } from "./link-manager/auth-manager/process-manager/endpoint-reference"-/** Where the default Program of each media type is kept. */+/** Where the default Program of each media type and family is kept. */const defaultsKey = "opening-defaults"/** How long a request waits for the owner to choose before it ends with nothing opened. */export const openRequestTimeout = 120_000/**- * Opening: something to open goes to its type's default Program, or waits as a request until the- * owner chooses one. The System only records requests and choices; how a request is shown is up to the+ * Opening: something to open goes to its type's default Program, else its family's (`image/*`), or+ * waits as a request until the owner chooses one. The System only records requests and choices; how a request is shown is up to the* Shell.*/export default class OpeningManager extends TheLink {@@ -38,7 +38,7 @@ export default class OpeningManager extends TheLink {return this.pendingRequests.has(identity)}- /** The installed Programs that open this exact type, by identity. */+ /** The installed Programs that open this exact type, or all of this family, by identity. */public programsFor(type: string): string[] {return [...this.authManager.programManager.programs.values()]@@ -58,9 +58,12 @@ export default class OpeningManager extends TheLink {if (!programs.length) throw new Error(`No Program opens ${target.type}`)- const chosen = (await this.chosen())[target.type]+ // The exact type's default is the stronger choice; its family's applies only without one.+ const defaults = await this.chosen()+ const chosen = [defaults[target.type], defaults[`${target.type.slice(0, target.type.indexOf("/"))}/*`]]+ .find(program => program !== undefined && programs.includes(program))- if (chosen && programs.includes(chosen)) {+ if (chosen) {await this.launch(chosen, target)return}@@ -143,7 +146,7 @@ export default class OpeningManager extends TheLink {}}- /** The default Program of each media type, as the Programs themselves; one no longer known is left out. */+ /** The default Program of each media type and family, as the Programs themselves; one no longer known is left out. */@Subscribe("/defaults")public async defaults() {@@ -152,7 +155,7 @@ export default class OpeningManager extends TheLink {.map(([type, program]) => [type, this.record(program)]))}- /** The default Program of each media type, by identity, as stored. */+ /** The default Program of each media type and family, by identity, as stored. */private async chosen(): Promise<Readonly<Record<string, string>>> {const stored = await this.store.get(defaultsKey) ?? {}@@ -167,22 +170,40 @@ export default class OpeningManager extends TheLink {@Subscribe("/set-default")public async setDefault(type: unknown, program: unknown) {- const target = parseOpenTarget({ type, uri: "about:blank" })+ const kept = parseOpenType(type)- if (typeof program !== "string" || !this.programsFor(target.type).includes(program)) {- throw new Error(`That Program does not open ${target.type}`)+ if (typeof program !== "string" || !this.programsFor(kept).includes(program)) {+ throw new Error(`That Program does not open ${kept}`)}- await this.store.set(defaultsKey, { ...await this.chosen(), [target.type]: program })+ const defaults = await this.chosen()++ if (defaults[kept] === program) return++ await this.store.set(defaultsKey, { ...defaults, [kept]: program })+ await this.announceDefault(kept, program)}@Subscribe("/clear-default")public async clearDefault(type: unknown) {- const target = parseOpenTarget({ type, uri: "about:blank" })- const { [target.type]: _cleared, ...kept } = await this.chosen()+ const cleared = parseOpenType(type)+ const { [cleared]: previous, ...kept } = await this.chosen()++ if (previous === undefined) returnawait this.store.set(defaultsKey, kept)+ await this.announceDefault(cleared, null)+ }++ private async announceDefault(type: string, program: string | null) {++ const record = program === null ? null : this.record(program)++ await Promise.all([+ this.authManager.processManager.announceHost("opening", null, "changeDefault", "system", type, record).catch(() => undefined),+ this.$outbound.publish("/default-change", type, record).catch(() => undefined)+ ])}/** What every Endpoint may know of a Program it is offered: its public record. */diff --git a/tests/opening.test.ts b/tests/opening.test.tsindex 304a66a..79b541d 100644--- a/tests/opening.test.ts+++ b/tests/opening.test.ts@@ -15,7 +15,8 @@ function fixture() {},processManager: { announceHost: vi.fn(async () => undefined) }}) as unknown as AuthManager- return { manager: new OpeningManager(auth), open, stored }+ const announceHost = (auth as unknown as { processManager: { announceHost: ReturnType<typeof vi.fn> } }).processManager.announceHost+ return { manager: new OpeningManager(auth), open, stored, announceHost }}const png = { type: "image/png", uri: "file:///home/me/a.png" }@@ -46,6 +47,36 @@ test("a default Program opens it at once, with no request", async () => {expect(manager.requests()).toEqual([])})+test("a family's default opens every type in it, and an exact type's default wins over it", async () => {+ const { manager, open, stored } = fixture()+ stored.set("opening-defaults", { "image/*": "preview" })++ await manager.open({ type: "image/jpeg", uri: "file:///a.jpg" }, null)+ expect(open).toHaveBeenLastCalledWith(expect.objectContaining({ identity: "preview" }), { type: "image/jpeg", uri: "file:///a.jpg" })++ stored.set("opening-defaults", { "image/*": "preview", "image/png": "paint" })+ await manager.open(png, null)+ expect(open).toHaveBeenLastCalledWith(expect.objectContaining({ identity: "paint" }), png)+ expect(manager.requests()).toEqual([])+})++test("a family's default needs a Program that opens all of it, and each change is announced once", async () => {+ const { manager, announceHost } = fixture()++ await expect(manager.setDefault("image/*", "paint")).rejects.toThrow(/does not open image\/\*/)+ await expect(manager.setDefault("*/*", "preview")).rejects.toThrow(/family/)++ await manager.setDefault("Image/*", "preview")+ await manager.setDefault("image/*", "preview")+ await manager.clearDefault("image/*")+ await manager.clearDefault("image/*")++ expect(announceHost.mock.calls.filter(([, , event]) => event === "changeDefault").map(([, , , , type, program]) => [type, program])).toEqual([+ ["image/*", { identity: "preview" }],+ ["image/*", null]+ ])+})+test("without a default, a request waits; choosing always opens it and makes the default", async () => {const { manager, open } = fixture()