{"version":3,"file":"service_bridge.cjs","names":[],"sources":["../../src/services/service_bridge.ts"],"sourcesContent":["/**\n * Runtime service bridge — builds lazy service objects from dispatch + registry.\n * Used by the UI layer to provide typed service access.\n */\n\nimport type {\n  ServiceTypesLike,\n  InferServiceUi,\n  ServiceName,\n  ServiceDispatch,\n  UiServices as AllUiServices,\n} from \"@milaboratories/pl-model-common\";\nimport { UiServiceRegistry } from \"@milaboratories/pl-model-common\";\n\n// Makes a remote node service appear local.\n// Given a service ID, returns an object implementing the service's UI interface.\n// Provided by the desktop app (e.g. backed by Electron IPC).\nexport type ServiceProxy = <S extends ServiceTypesLike>(\n  serviceId: ServiceName<S>,\n) => InferServiceUi<S>;\n\n/**\n * Builds a lazy services object from ServiceDispatch and UiServiceRegistry.\n * Each service is instantiated on first access. Errors are cached to prevent\n * repeated factory calls on failure.\n */\nexport function buildServices<S extends Partial<AllUiServices> = Partial<AllUiServices>>(\n  dispatch: ServiceDispatch,\n  registry: UiServiceRegistry,\n): S {\n  return Object.create(\n    null,\n    Object.fromEntries(\n      dispatch.getServiceNames().map((id) => {\n        let fetched = false;\n        let cached: unknown;\n        return [\n          id,\n          {\n            enumerable: true,\n            get() {\n              if (!fetched) {\n                fetched = true;\n                cached = registry.get(id);\n              }\n              return cached;\n            },\n          },\n        ];\n      }),\n    ),\n  );\n}\n\n/**\n * Builds a ServiceProxy from a ServiceDispatch.\n * Each service method call is forwarded to dispatch.callServiceMethod.\n */\nexport function createServiceProxy(dispatch: ServiceDispatch): ServiceProxy {\n  return ((serviceId: ServiceName) =>\n    Object.freeze(\n      Object.fromEntries(\n        dispatch\n          .getServiceMethods(serviceId)\n          .map((method) => [\n            method,\n            (...args: unknown[]) => dispatch.callServiceMethod(serviceId, method, ...args),\n          ]),\n      ),\n    )) as ServiceProxy;\n}\n"],"mappings":";;;;;;AA0BA,SAAgB,cACd,UACA,UACG;CACH,OAAO,OAAO,OACZ,MACA,OAAO,YACL,SAAS,gBAAgB,CAAC,CAAC,KAAK,OAAO;EACrC,IAAI,UAAU;EACd,IAAI;EACJ,OAAO,CACL,IACA;GACE,YAAY;GACZ,MAAM;IACJ,IAAI,CAAC,SAAS;KACZ,UAAU;KACV,SAAS,SAAS,IAAI,EAAE;IAC1B;IACA,OAAO;GACT;EACF,CACF;CACF,CAAC,CACH,CACF;AACF;;;;;AAMA,SAAgB,mBAAmB,UAAyC;CAC1E,SAAS,cACP,OAAO,OACL,OAAO,YACL,SACG,kBAAkB,SAAS,CAAC,CAC5B,KAAK,WAAW,CACf,SACC,GAAG,SAAoB,SAAS,kBAAkB,WAAW,QAAQ,GAAG,IAAI,CAC/E,CAAC,CACL,CACF;AACJ"}