import type { ReadableFamilyToken, ReadableToken } from "atom.io" import type { Canonical } from "atom.io/foundations/canonical" import type { RootStore } from "atom.io/internal" import { IMPLICIT, seekInStore, withdraw } from "atom.io/internal" /** * Check whether a state currently exists in a store without creating it. * * Useful for asserting that disposal released a state, while keeping tests away * from the store's internal data structures. */ export function stateExists(token: ReadableToken): boolean export function stateExists( family: ReadableFamilyToken, key: NoInfer, ): boolean export function stateExists( ...params: | [token: ReadableToken] | [family: ReadableFamilyToken, key: NoInfer] ): boolean { return stateExistsInStore(IMPLICIT.STORE, ...params) } export function stateExistsInStore( store: RootStore, token: ReadableToken, ): boolean export function stateExistsInStore( store: RootStore, family: ReadableFamilyToken, key: NoInfer, ): boolean export function stateExistsInStore( store: RootStore, ...params: | [token: ReadableToken] | [family: ReadableFamilyToken, key: NoInfer] ): boolean export function stateExistsInStore( store: RootStore, ...params: | [token: ReadableToken] | [family: ReadableFamilyToken, key: NoInfer] ): boolean { if (params.length === 2) { const [family, key] = params return seekInStore(store, family, key) !== undefined } const [token] = params try { withdraw(store, token) return true } catch { return false } }