/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import type { DiContainerForInjection, Injectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable"; import type { InitializableStateValue } from "./create"; export interface CreateLazyInitializableStateArgs { id: string; init: (di: DiContainerForInjection) => T; } export interface LazyInitializableState { get: () => T; } export function createLazyInitializableState(args: CreateLazyInitializableStateArgs): Injectable, unknown, void> { const { id, init } = args; return getInjectable({ id, instantiate: (di): LazyInitializableState => { let box: InitializableStateValue = { set: false, }; return { get: () => { if (box.set === false) { box = { set: true, value: init(di), }; } return box.value; }, }; }, }); }