Skip to content

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

refactor(mvc): keep application behavior in core

zohayrslileh8b0634d9b4bb
diff --git a/scripts/verify-client-surface.mjs b/scripts/verify-client-surface.mjs
index 8a75871..41d5932 100644
--- a/scripts/verify-client-surface.mjs
+++ b/scripts/verify-client-surface.mjs
@@ -1,5 +1,6 @@
import assert from "node:assert/strict"
import ClientProcessBoundary from "../source/client/view/desktop/client-host/client-process-boundary.ts"
+import ClientProcessManager from "../source/client/core/link-manager/auth-manager/process-manager/process-manager.ts"
import { surfaceSettings, visualTransaction } from "../source/client/view/desktop/client-host/local-window.ts"
import host from "../source/client/view/desktop/client-host/host.ts"
import LocalWindows from "../source/client/view/desktop/window-manager/local-windows.ts"
@@ -127,8 +128,13 @@ const localWindow = {
setSurface(identity, value, motion) { calls.push(["set", identity, value, motion]) },
removeSurface(identity) { calls.push(["remove", identity]) }
}
+const processManager = {
+ processes,
+ front: ClientProcessManager.prototype.front,
+ scope: ClientProcessManager.prototype.scope
+}
const authManager = {
- processManager: { processes },
+ processManager,
programManager: { programs: new Map() }
}
const request = host(authManager, requester.identity, () => ({ width: 1, height: 1 }), () => "owner", {}, localWindow)
diff --git a/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts b/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts
index b07ea12..2a42604 100644
--- a/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts
+++ b/source/client/core/link-manager/auth-manager/process-manager/process-manager.ts
@@ -5,9 +5,10 @@ import { Publish, Subscribe } from "@libs/the-link/decorators/escript"
import TheLink from "@libs/the-link/the-link"
import AuthManager from "../auth-manager"
import Process from "./process"
+import ProcessScope from "./process-scope"
import { type LaunchClient } from "@server/core/link-manager/auth-manager/program-manager/program-manager"
import { type TrafficKind } from "@server/core/link-manager/auth-manager/process-manager/process-traffic"
-import { isPermissionName, type ServiceKey } from "@phreshos/core"
+import { isPermissionName, type ServiceKey, type WindowLayer } from "@phreshos/core"
import { type ServiceScope } from "@server/core/link-manager/auth-manager/process-manager/endpoint-services"
/**
@@ -42,6 +43,33 @@ export default class ProcessManager extends TheLink {
return [...this.processes.values()]
}
+ /** Bind application operations and visibility policy to one framed Process. */
+ public scope(pane: string) {
+
+ return new ProcessScope(this, pane)
+ }
+
+ /** The shown authoritative Window at the front of one projected layer. */
+ public front(layer: WindowLayer) {
+
+ let best: string | null = null
+
+ let depth = -Infinity
+
+ for (const [identity, process] of this.processes) {
+
+ const window = process.client?.window
+
+ if (!window || window.layer === "wallpaper" || window.minimized || window.layer !== layer || window.depth <= depth) continue
+
+ best = identity
+
+ depth = window.depth
+ }
+
+ return best
+ }
+
// The structural frame gate supplies `source`; Program code supplies only
// the target and payload.
public async publish(source: string, identity: string, which: "server" | "client", values: unknown[]) {
diff --git a/source/client/core/link-manager/auth-manager/process-manager/process-scope.ts b/source/client/core/link-manager/auth-manager/process-manager/process-scope.ts
new file mode 100644
index 0000000..ab9660c
--- /dev/null
+++ b/source/client/core/link-manager/auth-manager/process-manager/process-scope.ts
@@ -0,0 +1,159 @@
+import { type WindowLayer } from "@phreshos/core"
+import { type default as Process } from "./process"
+import { type default as ProcessManager } from "./process-manager"
+
+/**
+ * The Core capability scope of one framed Client Process.
+ *
+ * View supplies only the pane that received a request. This scope resolves
+ * every domain subject from that structural fact, so presentation never owns
+ * cross-Program visibility or Window mutability policy.
+ */
+export default class ProcessScope {
+
+ public constructor(public readonly processManager: ProcessManager, public readonly pane: string) { }
+
+ public current() {
+
+ const found = this.processManager.processes.get(this.pane)
+
+ if (!found) throw new Error("The desktop does not know this process")
+
+ return found
+ }
+
+ public related(named: unknown) {
+
+ if (!isHandleAddress(named)) return null
+
+ const found = this.processManager.processes.get(named.identity)
+
+ return found?.reference === named.reference && found.program === this.current().program ? found : null
+ }
+
+ public sibling(named: unknown) {
+
+ const found = this.related(named)
+
+ if (!found) throw new Error("The desktop does not know this process")
+
+ return found
+ }
+
+ public window(named: unknown) {
+
+ const found = this.sibling(named).client?.window
+
+ if (!found) throw new Error("This process has no live client endpoint")
+
+ return found
+ }
+
+ public mutableWindow(named: unknown) {
+
+ const found = this.window(named)
+
+ if (found.layer === "wallpaper") throw new Error("A wallpaper Window is managed by the system")
+
+ return found
+ }
+
+ public localProcess(named: unknown) {
+
+ const found = this.sibling(named)
+
+ if (!found.client) throw new Error("This process has no live client endpoint")
+
+ if (found.client.window.layer === "wallpaper") throw new Error("A wallpaper Window is managed by the system")
+
+ return found
+ }
+
+ public declared(found: Process, half: "server" | "client") {
+
+ return (this.processManager.authManager.programManager.programs.get(found.program)?.[half] ?? null) !== null
+ }
+
+ public programOf(found: Pick<Process, "program">) {
+
+ const program = this.processManager.authManager.programManager.programs.get(found.program)
+
+ if (!program) throw new Error("The desktop does not know this program")
+
+ return program
+ }
+
+ /** Resolve the current Process's live, same-Program parent. */
+ public parent(found: Process) {
+
+ const address = found.parent
+
+ if (!address) return null
+
+ const parent = this.processManager.processes.get(address.identity)
+
+ if (!parent || parent.reference !== address.reference) throw new Error("The parent Process no longer exists")
+
+ return parent.program === this.current().program ? parent : null
+ }
+
+ /** Whether a handle has ended or is outside this pane's Program scope. */
+ public exited(address: HandleAddress) {
+
+ const found = this.processManager.processes.get(address.identity)
+
+ return !found || found.reference !== address.reference || found.program !== this.current().program
+ }
+
+ /** Every live Process belonging to this pane's Program. */
+ public processes() {
+
+ return [...this.processManager.processes.values()].filter(entry => entry.program === this.current().program)
+ }
+
+ /** Resolve one same-Program Process by identity first, then by local name. */
+ public findProcess(wanted: string) {
+
+ const program = this.current().program
+
+ const identified = this.processManager.processes.get(wanted)
+
+ if (identified?.program === program) return identified
+
+ return [...this.processManager.processes.values()].find(entry => entry.program === program && entry.name === wanted) ?? null
+ }
+
+ /** Resolve a trusted Process identity returned by an application operation. */
+ public requireProcess(identity: string) {
+
+ const found = this.processManager.processes.get(identity)
+
+ if (!found) throw new Error("The desktop does not know this process")
+
+ return found
+ }
+
+ /** The Program that owns this pane. */
+ public program() {
+
+ return this.programOf(this.current())
+ }
+
+ public front(layer: WindowLayer) {
+
+ return this.processManager.front(layer)
+ }
+}
+
+function isHandleAddress(value: unknown): value is HandleAddress {
+
+ return typeof value === "object" && value !== null && "identity" in value && "reference" in value
+ && typeof value.identity === "string" && typeof value.reference === "string"
+}
+
+interface HandleAddress {
+
+ identity: string
+
+ reference: string
+}
diff --git a/source/client/view/desktop/client-host/host.ts b/source/client/view/desktop/client-host/host.ts
index 7916ff6..626b864 100644
--- a/source/client/view/desktop/client-host/host.ts
+++ b/source/client/view/desktop/client-host/host.ts
@@ -5,7 +5,7 @@ import { type ClientBody, type ProxiedResponse, type ServedValue } from "@client
import { type PointerHost } from "./pointer"
import { type TrafficKind } from "@server/core/link-manager/auth-manager/process-manager/process-traffic"
import { sdkProcess, sdkProgram } from "./sdk-records"
-import { isPermissionName, isServiceKey, type ProgramIconSize, type WindowLayer } from "@phreshos/core"
+import { isPermissionName, isServiceKey, type ProgramIconSize } from "@phreshos/core"
import {
localGeometry,
localPosition,
@@ -57,86 +57,11 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
const { processManager, programManager } = authManager
- function process() {
-
- const found = processManager.processes.get(pane)
-
- if (!found) throw new Error("The desktop does not know this process")
-
- return found
- }
-
- // The gate.
- //
- // Program words resolve their subject from the frame and discard
- // what was asked, which is why their isolation needs nothing checked:
- // there is nothing to get wrong. Process-handle words take an
- // identity — a sibling this program is running — and that identity
- // is the first subject this side accepts from a pane.
- //
- // So it is measured, once, here: an identity a pane names must belong to
- // a process of the program that pane is a face of. A stranger's is
- // refused whether or not it exists, because whether it exists is
- // itself something a pane may not learn.
- function related(named: unknown) {
-
- if (!isHandleAddress(named)) return null
-
- const found = processManager.processes.get(named.identity)
-
- return found?.reference === named.reference && found.program === process().program ? found : null
- }
-
- function sibling(named: unknown) {
-
- const found = related(named)
-
- if (!found) throw new Error("The desktop does not know this process")
-
- return found
- }
-
- function window(named: unknown) {
-
- const found = sibling(named).client?.window
-
- if (!found) throw new Error("This process has no live client endpoint")
-
- return found
- }
-
- function mutableWindow(named: unknown) {
-
- const found = window(named)
-
- if (found.layer === "wallpaper") throw new Error("A wallpaper Window is managed by the system")
-
- return found
- }
-
- function localProcess(named: unknown) {
-
- const found = sibling(named)
-
- if (!found.client) throw new Error("This process has no live client endpoint")
-
- if (found.client.window.layer === "wallpaper") throw new Error("A wallpaper Window is managed by the system")
-
- return found
- }
-
- function declared(found: ReturnType<typeof process>, half: "server" | "client") {
+ const scope = processManager.scope(pane)
- return (programManager.programs.get(found.program)?.[half] ?? null) !== null
- }
-
- function owner(found: ReturnType<typeof process> | NonNullable<ReturnType<typeof process>["parent"]>) {
-
- const program = programManager.programs.get(found.program)
-
- if (!program) throw new Error("The desktop does not know this program")
+ function process() {
- return program
+ return scope.current()
}
// One process record on every road. Nullable values carry the resolved
@@ -145,37 +70,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// the desktop's mutable record.
function record(found: ReturnType<typeof process> | NonNullable<ReturnType<typeof process>["parent"]>) {
- return sdkProcess(found, owner(found))
- }
-
- // Which window is topmost *of its own layer*, worked out the way the
- // desktop draws it: the shown one in that layer with the greatest
- // depth. A pane is told whether that is itself and nothing more, so
- // it learns no other window exists.
- //
- // This read across every layer until the layers existed, and the day
- // they did it began answering `false` to a window a person was
- // typing in because something in `over` had a larger number. Three
- // places now work this out — here, the core, and the desktop — and
- // all three read depth within one layer.
- function front(layer: WindowLayer) {
-
- let best: string | null = null
-
- let depth = -Infinity
-
- for (const entry of processManager.processes.values()) {
-
- const window = entry.client?.window
-
- if (!window || window.layer === "wallpaper" || window.minimized || window.layer !== layer || window.depth <= depth) continue
-
- best = entry.identity
-
- depth = window.depth
- }
-
- return best
+ return sdkProcess(found, scope.programOf(found))
}
return async function answer(word: unknown, ...args: unknown[]) {
@@ -196,7 +91,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "exists") {
- const target = args[1] === undefined ? process() : sibling(args[1])
+ const target = args[1] === undefined ? process() : scope.sibling(args[1])
if (args[0] === "server") return [target.server !== null]
@@ -207,7 +102,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "start-endpoint") {
- const target = args[0] === undefined ? process() : sibling(args[0])
+ const target = args[0] === undefined ? process() : scope.sibling(args[0])
if (args[1] !== "server" && args[1] !== "client") throw new Error("A Process endpoint is server or client")
@@ -218,7 +113,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "stop-endpoint") {
- const target = args[0] === undefined ? process() : sibling(args[0])
+ const target = args[0] === undefined ? process() : scope.sibling(args[0])
if (args[1] !== "server" && args[1] !== "client") throw new Error("A Process endpoint is server or client")
@@ -250,7 +145,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "endpoint-service") {
- const target = args[0] === null ? process() : sibling(args[0])
+ const target = args[0] === null ? process() : scope.sibling(args[0])
if (args[1] !== "server" && args[1] !== "client") throw new Error("A service Endpoint must be server or client")
@@ -320,17 +215,11 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// the same `null`, so the relationship cannot reveal a stranger.
if (word === "parent") {
- const target = args[0] === undefined ? process() : sibling(args[0])
-
- const address = target.parent
-
- if (!address) return [null]
+ const target = args[0] === undefined ? process() : scope.sibling(args[0])
- const parent = processManager.processes.get(address.identity)
+ const parent = scope.parent(target)
- if (!parent || parent.reference !== address.reference) throw new Error("The parent Process no longer exists")
-
- return [parent.program === process().program ? record(parent) : null]
+ return [parent ? record(parent) : null]
}
// Whether a process a pane holds has ended.
@@ -346,9 +235,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (!isHandleAddress(args[0])) throw new Error("The boundary returned an invalid Process handle")
- const found = processManager.processes.get(args[0].identity)
-
- return [!found || found.reference !== args[0].reference || found.program !== process().program]
+ return [scope.exited(args[0])]
}
// Every instance of this pane's program, this one among them: a
@@ -356,7 +243,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// are running. Nothing is named — the program is the frame's.
if (word === "processes") {
- return [[...processManager.processes.values()].filter(entry => entry.program === process().program).map(record)]
+ return [scope.processes().map(record)]
}
// One process of this program by immutable identity or by its
@@ -365,13 +252,9 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
const wanted = String(args[1])
- const identified = processManager.processes.get(wanted)
-
- if (identified?.program === process().program) return [record(identified)]
+ const found = scope.findProcess(wanted)
- const named = [...processManager.processes.values()].find(entry => entry.program === process().program && entry.name === wanted)
-
- return [named ? record(named) : null]
+ return [found ? record(found) : null]
}
// Another instance of this pane's own program. The identity comes
@@ -384,11 +267,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
const started = await programManager.createProcess(process().program, args[1] as Launch, process().identity)
- const startedRecord = processManager.processes.get(started)
-
- if (!startedRecord) throw new Error("The desktop does not know this process")
-
- return [record(startedRecord)]
+ return [record(scope.requireProcess(started))]
}
// Every instance ended, the asker last. One implementation on the
@@ -409,7 +288,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
const owner = frameOwner()
- const target = args[1] === null ? process() : related(args[1])
+ const target = args[1] === null ? process() : scope.related(args[1])
// Callback subscriptions are synchronous and remain silent when
// their source is unavailable. waitFor() and events() identify
@@ -422,7 +301,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return []
}
- if (!declared(target, half)) {
+ if (!scope.declared(target, half)) {
if (reportImpossible) throw new Error(`This program declared no ${half} half`)
@@ -457,7 +336,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
const frame = frameOwner()
- const target = args[1] === null ? process() : related(args[1])
+ const target = args[1] === null ? process() : scope.related(args[1])
if (!frame || !target) {
@@ -466,7 +345,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return []
}
- if (!declared(target, half)) {
+ if (!scope.declared(target, half)) {
if (reportImpossible) throw new Error(`This program declared no ${half} half`)
@@ -509,7 +388,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (args[1] !== "server" && args[1] !== "client") throw new Error(`A process has no "${String(args[1])}" end`)
- await processManager.publish(pane, sibling(args[0]).identity, args[1], args.slice(2))
+ await processManager.publish(pane, scope.sibling(args[0]).identity, args[1], args.slice(2))
return []
}
@@ -521,7 +400,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (args[1] !== "server") throw new Error("Only a server end can be asked — a client end has no one answerer")
- await processManager.askOf(pane, sibling(args[0]).identity, args.slice(2))
+ await processManager.askOf(pane, scope.sibling(args[0]).identity, args.slice(2))
return []
}
@@ -532,7 +411,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// exactly like every other `current` operation on this side.
if (word === "option") {
- const found = args[0] === undefined ? process() : sibling(args[0])
+ const found = args[0] === undefined ? process() : scope.sibling(args[0])
return [found.options[String(args[1])]]
}
@@ -543,9 +422,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "program") {
- const program = programManager.programs.get(process().program)
-
- if (!program) throw new Error("The desktop does not know this program")
+ const program = scope.program()
// What it declared about its window comes too — the same
// kind of fact as its name, and what a window needs to know
@@ -565,25 +442,19 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// the server side; neither is a capability of a client face.
if (word === "installed") {
- const program = programManager.programs.get(process().program)
-
- return [program?.installed === true]
+ return [scope.program().installed === true]
}
if (word === "uninstall") {
- const program = programManager.programs.get(process().program)
-
- if (!program) throw new Error("The desktop does not know this program")
+ const program = scope.program()
return [await program.uninstall(args[1] === true, pane)]
}
if (word === "forget") {
- const program = programManager.programs.get(process().program)
-
- if (!program) throw new Error("The desktop does not know this program")
+ const program = scope.program()
return [await program.forget(pane)]
}
@@ -595,7 +466,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
// was being used to ask.
if (word === "window") {
- const shown = window(args[0] ?? address(process()))
+ const shown = scope.window(args[0] ?? address(process()))
return [{
@@ -607,7 +478,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
minimized: shown.minimized,
- front: front(shown.layer) === pane,
+ front: scope.front(shown.layer) === pane,
// Which layer it lives in, and which of its own pages it
// was opened at. Both are the system's answers about
@@ -622,107 +493,107 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "move") {
- await mutableWindow(args[0]).move(args[1] as never)
+ await scope.mutableWindow(args[0]).move(args[1] as never)
return [pane]
}
if (word === "resize") {
- await mutableWindow(args[0]).resize(args[1] as never)
+ await scope.mutableWindow(args[0]).resize(args[1] as never)
return [pane]
}
if (word === "setGeometry") {
- await mutableWindow(args[0]).setGeometry(args[1] as never)
+ await scope.mutableWindow(args[0]).setGeometry(args[1] as never)
return [pane]
}
- if (word === "windowLocal") return [localWindow.state(localProcess(args[0]).identity)]
+ if (word === "windowLocal") return [localWindow.state(scope.localProcess(args[0]).identity)]
if (word === "windowLocalMove") {
- await localWindow.move(localProcess(args[0]).identity, localPosition(args[1]), visualTransaction(args[2]))
+ await localWindow.move(scope.localProcess(args[0]).identity, localPosition(args[1]), visualTransaction(args[2]))
return []
}
if (word === "windowLocalResize") {
- await localWindow.resize(localProcess(args[0]).identity, localSize(args[1]), visualTransaction(args[2]))
+ await localWindow.resize(scope.localProcess(args[0]).identity, localSize(args[1]), visualTransaction(args[2]))
return []
}
if (word === "windowLocalGeometry") {
- await localWindow.geometry(localProcess(args[0]).identity, localGeometry(args[1]), visualTransaction(args[2]))
+ await localWindow.geometry(scope.localProcess(args[0]).identity, localGeometry(args[1]), visualTransaction(args[2]))
return []
}
if (word === "windowLocalMinimize") {
- localWindow.minimize(localProcess(args[0]).identity, args[1] !== false)
+ localWindow.minimize(scope.localProcess(args[0]).identity, args[1] !== false)
return []
}
if (word === "windowLocalTitle") {
- localWindow.title(localProcess(args[0]).identity, String(args[1] ?? ""))
+ localWindow.title(scope.localProcess(args[0]).identity, String(args[1] ?? ""))
return []
}
if (word === "windowLocalRaise") {
- localWindow.raise(localProcess(args[0]).identity)
+ localWindow.raise(scope.localProcess(args[0]).identity)
return []
}
if (word === "windowLocalSurfaceSet") {
- await localWindow.setSurface(localProcess(args[0]).identity, surfaceSettings(args[1] === undefined ? {} : args[1]), visualTransaction(args[2]))
+ await localWindow.setSurface(scope.localProcess(args[0]).identity, surfaceSettings(args[1] === undefined ? {} : args[1]), visualTransaction(args[2]))
return []
}
if (word === "windowLocalSurfaceRemove") {
- localWindow.removeSurface(localProcess(args[0]).identity)
+ localWindow.removeSurface(scope.localProcess(args[0]).identity)
return []
}
if (word === "changeTitle") {
- await mutableWindow(args[0]).changeTitle(String(args[1] ?? ""))
+ await scope.mutableWindow(args[0]).changeTitle(String(args[1] ?? ""))
return [pane]
}
if (word === "raise") {
- await mutableWindow(args[0]).raise()
+ await scope.mutableWindow(args[0]).raise()
return [pane]
}
if (word === "minimize") {
- await mutableWindow(args[0]).minimize(args[1] !== false)
+ await scope.mutableWindow(args[0]).minimize(args[1] !== false)
return [pane]
}
if (word === "exit") {
- await sibling(args[0]).stop()
+ await scope.sibling(args[0]).stop()
return [pane]
}
diff --git a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
index 5733f85..19855bf 100644
--- a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
+++ b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
@@ -580,7 +580,12 @@ export default class ProgramManager extends TheLink {
return await this.forget(program, asker)
}
- public async installSource(source: ProgramConfig) {
+ /**
+ * Install one external Program description and coordinate every requested
+ * follow-up as one Core operation. Each yielded stage lets a View represent
+ * progress without taking ownership of the workflow.
+ */
+ public async *installSource(source: ProgramConfig, options: InstallSourceOptions = {}): AsyncGenerator<InstallSourceStage> {
const program = new Program(source)
@@ -591,7 +596,25 @@ export default class ProgramManager extends TheLink {
// durable files, not the current registry entry's installed flag.
const replaced = existsSync(this.fileManager.join(program.identity, "program.json"))
- return { entry: await this.install(program), replaced }
+ const entry = await this.install(program)
+
+ yield { stage: "installed", entry, replaced }
+
+ const launch = {}
+
+ if (options.startup) {
+
+ await this.startup(entry.program, "enable", launch)
+
+ yield { stage: "startup-enabled" }
+ }
+
+ if (options.run) {
+
+ const process = await this.runInstalled(entry.program, launch)
+
+ yield { stage: "running", process }
+ }
}
/** Run a durable Program independently of the local intake connection. */
@@ -1160,6 +1183,21 @@ export default class ProgramManager extends TheLink {
export type TransmittedProgramManager = Transmitted<ProgramManager>
+export interface InstallSourceOptions {
+
+ run?: boolean
+
+ startup?: boolean
+}
+
+export type InstallSourceStage =
+
+ | { stage: "installed", entry: Entry, replaced: boolean }
+
+ | { stage: "startup-enabled" }
+
+ | { stage: "running", process: string }
+
// Which of a half's own pages a launch asked for.
//
// A launch location is rooted in the client half's declared place,
diff --git a/source/server/view/intake.ts b/source/server/view/intake.ts
index 5f7ae88..8545364 100644
--- a/source/server/view/intake.ts
+++ b/source/server/view/intake.ts
@@ -233,26 +233,18 @@ async function answer(application: Application, attached: Set<string>, say: (eve
if (asked.startup !== undefined && typeof asked.startup !== "boolean") throw new Error("Enabling startup during installation must be true or false")
- const { entry, replaced } = await programManager.installSource(asked.program)
+ for await (const stage of programManager.installSource(asked.program, { run: asked.run, startup: asked.startup })) {
- // Whether it replaced one. Installation has already ended every
- // process that could have retained one of the previous paths.
- say({ event: "installed", replaced, program: { identity: entry.identity, name: entry.program.name, version: entry.program.config.version ?? null } })
+ if (stage.stage === "installed") {
- const launch = {}
-
- if (asked.startup) {
-
- await programManager.startup(entry.program, "enable", launch)
-
- say({ event: "startupEnabled" })
- }
-
- if (asked.run) {
+ // Whether it replaced one. Installation has already ended every
+ // process that could have retained one of the previous paths.
+ say({ event: "installed", replaced: stage.replaced, program: { identity: stage.entry.identity, name: stage.entry.program.name, version: stage.entry.program.config.version ?? null } })
+ }
- const process = await programManager.runInstalled(entry.program, launch)
+ else if (stage.stage === "startup-enabled") say({ event: "startupEnabled" })
- say({ event: "running", process })
+ else say({ event: "running", process: stage.process })
}
return done()