import { PluginError, type PluginContext, type CustomMetadata, type DocCapability, type DocumentMetadata, type OperationOptions, } from '@embedpdf/core'; import type { CustomMetadataCapability, CustomMetadataResyncedEvent, CustomMetadataUpdatedEvent, MetadataCapability, MetadataResyncedEvent, MetadataUpdatedEvent, } from './contract'; import { changedKeys } from './model'; const METADATA_MODIFY: DocCapability = 'doc.metadata.modify'; /** * The metadata controller. Both halves of the Info dict are mirrors: each * changes only when a load lands or its confirmed document event arrives * (`metadata.updated`, `metadata.customUpdated`), whoever caused the edit, * and that is also the one place its `onUpdated` and `onResynced` fire. * Nothing needs them when a document opens, so they load on demand: the * first `ensureLoaded()` (what `metadataState` asks for) reads them. */ export function createMetadataController(ctx: PluginContext) { const updated = ctx.events.source(); const resynced = ctx.events.source(); const customUpdated = ctx.events.source(); const customResynced = ctx.events.source(); const metadata = ctx.mirror({ name: 'metadata', onDemand: true, initial: () => null, load: async (doc) => ({ value: await doc.metadata.get() }), fold: (value, event) => (event.type === 'metadata.updated' ? event.metadata : value), onChanged: ({ cause, event, previous, next }) => { if (!next) return; if (cause === 'load') { resynced.emit({ metadata: next }); } else if (event && 'origin' in event) { updated.emit({ metadata: next, previous, changedKeys: changedKeys(previous, next), origin: event.origin, }); } }, }); const custom = ctx.mirror({ name: 'customMetadata', onDemand: true, initial: () => null, load: async (doc) => ({ value: await doc.metadata.custom.get() }), fold: (value, event) => (event.type === 'metadata.customUpdated' ? event.custom : value), onChanged: ({ cause, event, previous, next }) => { if (!next) return; if (cause === 'load') { customResynced.emit({ custom: next }); } else if (event && 'origin' in event) { customUpdated.emit({ custom: next, previous, changedKeys: changedKeys(previous, next), origin: event.origin, }); } }, }); const canUpdate = () => ctx.allows(METADATA_MODIFY); /** * Both writes refuse the same way before reaching the engine: the verbs are * async, so a refusal rejects, and nothing changes. */ const refuseWrite = (verb: string, options?: OperationOptions): void => { ctx.assertAllowed(METADATA_MODIFY, verb); if (options?.signal?.aborted) { throw new PluginError('operation-cancelled', 'metadata', `${verb} was cancelled`); } }; const customApi: CustomMetadataCapability = { getSnapshot: custom.get, getStatus: custom.getStatus, update: async (patch, options) => { refuseWrite('metadata.custom.update', options); const result = await ctx.cancellable(options?.signal, ctx.doc.metadata.custom.update(patch)); return { custom: result.custom }; }, refresh: (options) => ctx.cancellable(options?.signal, custom.refresh()), onUpdated: customUpdated.on, onResynced: customResynced.on, }; const api: MetadataCapability = { getSnapshot: metadata.get, getStatus: metadata.getStatus, ensureLoaded: (options) => ctx.cancellable( options?.signal, Promise.all([metadata.ensureLoaded(), custom.ensureLoaded()]).then(() => undefined), ), canUpdate, update: async (patch, options) => { refuseWrite('metadata.update', options); const result = await ctx.cancellable(options?.signal, ctx.doc.metadata.update(patch)); return { metadata: result.metadata }; }, refresh: (options) => ctx.cancellable(options?.signal, metadata.refresh()), onUpdated: updated.on, onResynced: resynced.on, custom: customApi, }; return { api }; }