import type { AtomIOToken } from "atom.io" import { newest } from "./lineage.ts" import { type Store, withdraw } from "./store/index.ts" import type { RootStore } from "./transaction/index.ts" import { isChildStore } from "./transaction/index.ts" /** * @public * Install the given tokens into the target store * @param tokens - States, families, transactions, and timelines to install into the target store * @param target - The store to install the tokens into * @param source - The store to install the tokens from * */ export function installIntoStore( tokens: AtomIOToken[], target: RootStore, source: Store, ): void { const sourceNewest = newest(source) if (isChildStore(sourceNewest)) { source.logger.error( `❌`, `transaction`, sourceNewest.transactionMeta.update.token.key, `could not install the following tokens into store "${target.config.name} from "${source.config.name}":`, tokens, `${sourceNewest.config.name} is undergoing a transaction.`, ) return } const targetNewest = newest(target) if (isChildStore(targetNewest)) { target.logger.error( `❌`, `transaction`, targetNewest.transactionMeta.update.token.key, `could not install the following tokens into store "${target.config.name} from "${source.config.name}":`, tokens, `${targetNewest.config.name} is undergoing a transaction.`, ) return } for (const token of tokens) { const resource = withdraw(source, token) resource.install(target) } }