From 4ceabdffa07b1af8d99eb73622a4d549d99ec6d2 Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 18 Sep 2025 10:59:01 -0400 Subject: wip: zen --- packages/console/core/src/actor.ts | 74 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 packages/console/core/src/actor.ts (limited to 'packages/console/core/src/actor.ts') diff --git a/packages/console/core/src/actor.ts b/packages/console/core/src/actor.ts new file mode 100644 index 000000000..0d13f7216 --- /dev/null +++ b/packages/console/core/src/actor.ts @@ -0,0 +1,74 @@ +import { Context } from "./context" +import { Log } from "./util/log" + +export namespace Actor { + interface Account { + type: "account" + properties: { + accountID: string + email: string + } + } + + interface Public { + type: "public" + properties: {} + } + + interface User { + type: "user" + properties: { + userID: string + workspaceID: string + } + } + + interface System { + type: "system" + properties: { + workspaceID: string + } + } + + export type Info = Account | Public | User | System + + const ctx = Context.create() + export const use = ctx.use + + const log = Log.create().tag("namespace", "actor") + + export function provide( + type: T, + properties: Extract["properties"], + cb: () => R, + ) { + return ctx.provide( + { + type, + properties, + } as any, + () => { + return Log.provide({ ...properties }, () => { + log.info("provided") + return cb() + }) + }, + ) + } + + export function assert(type: T) { + const actor = use() + if (actor.type !== type) { + throw new Error(`Expected actor type ${type}, got ${actor.type}`) + } + return actor as Extract + } + + export function workspace() { + const actor = use() + if ("workspaceID" in actor.properties) { + return actor.properties.workspaceID + } + throw new Error(`actor of type "${actor.type}" is not associated with a workspace`) + } +} -- cgit v1.2.3