pockethost/packages/common/src/mkSingleton.ts
2023-09-10 16:33:38 -07:00

31 lines
669 B
TypeScript

import { Logger } from './Logger'
export type SingletonApi = {
shutdown: () => void | Promise<void>
}
export type SingletonBaseConfig = {
logger: Logger
}
export const mkSingleton = <
TConfig,
TApi extends SingletonApi | Promise<SingletonApi>,
>(
factory: (config: TConfig) => TApi,
) => {
let _service: TApi | undefined = undefined
return (config?: TConfig) => {
if (_service && config) {
throw new Error(`Attempted to initialize service twice`)
}
if (!_service) {
if (!config) {
throw new Error(`Attempted to use service before initialization`)
}
_service = factory(config)
}
return _service
}
}