import { computed, handler, NAME, pattern, Stream, type TrustedActionWrite, UI, type VNode, Writable, } from "commonfabric"; export const TRUSTED_LONG_RUNNING_JOB_SURFACE = "TrustedLongRunningJobSurface"; const AUTHORIZE_LONG_RUNNING_JOB_ACTION = "TrustedAuthorizeLongRunningJob"; const CANCEL_LONG_RUNNING_JOB_ACTION = "TrustedCancelLongRunningJob"; export const authorizeTrustedLongRunningJob = handler< void, { jobName: Writable; jobStatus: Writable; jobAuthorization: Writable; } >((_, { jobName, jobStatus, jobAuthorization }) => { const name = jobName.get().trim() || "job"; jobStatus.set("Running"); jobAuthorization.set(`Authorized long-running job: ${name}`); }); export const cancelTrustedLongRunningJob = handler< void, { jobName: Writable; jobStatus: Writable; jobCancellation: Writable; } >((_, { jobName, jobStatus, jobCancellation }) => { const name = jobName.get().trim() || "job"; jobStatus.set("Cancelled"); jobCancellation.set(`Cancelled long-running job: ${name}`); }); export interface TrustedLongRunningJobSurfaceInput { jobName: Writable; jobStatus: Writable; jobAuthorization: Writable; jobCancellation: Writable; } export interface TrustedLongRunningJobSurfaceOutput { [NAME]: string; [UI]: VNode; jobAuthorization: TrustedActionWrite< string, typeof authorizeTrustedLongRunningJob, typeof AUTHORIZE_LONG_RUNNING_JOB_ACTION, typeof TRUSTED_LONG_RUNNING_JOB_SURFACE >; jobCancellation: TrustedActionWrite< string, typeof cancelTrustedLongRunningJob, typeof CANCEL_LONG_RUNNING_JOB_ACTION, typeof TRUSTED_LONG_RUNNING_JOB_SURFACE >; startJob: Stream; cancelJob: Stream; } export const TrustedLongRunningJobSurface = pattern< TrustedLongRunningJobSurfaceInput, TrustedLongRunningJobSurfaceOutput >( ({ jobName, jobStatus, jobAuthorization, jobCancellation }) => { const startJob = authorizeTrustedLongRunningJob({ jobName, jobStatus, jobAuthorization, }); const cancelJob = cancelTrustedLongRunningJob({ jobName, jobStatus, jobCancellation, }); return { [NAME]: computed(() => "Trusted Long Running Job Surface"), [UI]: ( Trusted long-running job Keep the job visible and cancelable while the trusted kernel authorizes it. Job name Authorize job Cancel job Current status
{jobStatus}
Authorization
{jobAuthorization}
Cancellation
{jobCancellation}
), jobAuthorization, jobCancellation, startJob, cancelJob, }; }, );