An open-source, self-hosted system for apps built with web technologies.
import type { OpenTarget } from "@phreshos/core"
import { ProcessSnapshot } from "@server/core/link-manager/auth-manager/process-manager/process"
import ProcessManager from "./process-manager"
import ClientState from "./client-state"
import Window from "./window"
/**
* A running instance, as this side holds it: rebuilt from what the core
* transmitted. Its Client Endpoint handle survives between runs, while the
* Window values belong only to the current execution context.
*/
export default class Process {
public readonly processManager: ProcessManager
public readonly identity: string
/** Opaque identity of this one Process entity. */
public readonly reference: string
public readonly name: string | null
public readonly program: string
// A real date on this side: the link carries dates rather than
// stringifying them.
public readonly startedAt: Date
// Current live endpoint state mirrored from the authoritative host.
public server: { ready: boolean, service: string | false } | null
public client: ClientState | null
/** Permanent Client Endpoint handle retained between execution contexts. */
public readonly clientEndpoint: Readonly<{ window: Window }> | null
// Retained lineage from creation. It remains sufficient to reconstruct
// the same Process handle after the parent leaves the live registry.
public readonly parent: ProcessLineage | null
// What its launch said, carried for its whole life.
public readonly options: Record<string, string>
// What it was started to open, carried for its whole life too.
public readonly opened: OpenTarget | null
public constructor(processManager: ProcessManager, payload: ProcessSnapshot) {
this.processManager = processManager
this.identity = payload.identity
this.reference = payload.reference
this.name = payload.name
this.program = payload.program
this.startedAt = payload.startedAt
this.server = payload.server
this.clientEndpoint = payload.clientEndpoint
? Object.freeze({ window: new Window(processManager, payload.identity, payload.clientEndpoint.window) })
: null
this.client = payload.client && this.clientEndpoint ? new ClientState(this.clientEndpoint.window, payload.client) : null
this.parent = payload.parent
this.options = payload.options
this.opened = payload.opened
}
// Ending is asked for; the process leaves when its exit echoes.
public async exit() {
await this.processManager.$outbound.publish("/exit", this.identity)
}
// The program's own words, to its other half.
public async endEnd(...args: unknown[]) {
await this.processManager.$outbound.publish("/frame/end-end", this.identity, args)
}
public serverStarted(payload: ProcessSnapshot) {
if (payload.server) this.server = payload.server
}
public serverStopped() {
this.server = null
}
public clientStarted(payload: ProcessSnapshot) {
if (!payload.client || !this.clientEndpoint) return
if (!payload.clientEndpoint?.window) throw new Error("A running Client Endpoint requires Window state")
this.clientEndpoint.window.start(payload.clientEndpoint.window)
// A Process creation snapshot already contains every endpoint that is
// live at birth. Its following lifecycle announcement describes that
// same Client; it must update the existing projection rather than
// inventing a second local incarnation for the Window Manager.
if (this.client) return
this.client = new ClientState(this.clientEndpoint.window, payload.client)
}
public clientStopped() {
this.client = null
this.clientEndpoint?.window.stop()
}
}
/** Retained identity needed only to reconstruct a departed parent handle. */
export interface ProcessLineage {
readonly reference: string
readonly identity: string
readonly name: string | null
readonly program: string
readonly options: Record<string, string>
readonly opened: OpenTarget | null
readonly startedAt: Date
readonly server: { service: string | false } | null
readonly client: { service: string | false } | null
}