Skip to content

armgit / kiln

Public Imported from GitHub
0

Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.

1.0 KB
Raw Blame
export type IslandState = 'idle' | 'loading' | 'ready' | 'error';

/** A request owns its mount; an obsolete request may never reopen a closed island. */
export function createIslandSession(
  load: () => Promise<() => void>,
  onState: (state: IslandState, error?: Error) => void,
) {
  let generation = 0;
  let active = false;
  let cleanup: (() => void) | undefined;
  return {
    async start() {
      if (active) return;
      active = true;
      const request = ++generation;
      onState('loading');
      try {
        const dispose = await load();
        if (request !== generation) {
          dispose();
          return;
        }
        cleanup = dispose;
        onState('ready');
      } catch (cause) {
        if (request !== generation) return;
        active = false;
        onState('error', cause instanceof Error ? cause : new Error(String(cause)));
      }
    },
    stop() {
      generation++;
      active = false;
      cleanup?.();
      cleanup = undefined;
      onState('idle');
    },
  };
}