import { withSnapshot } from './enhancers/with-snapshot.js' import { withSubscribe } from './enhancers/with-subscribe.js' import { proxy } from './proxy.js' import { deepCloneWithRef } from './ref.js' import type { SubscribeListener } from './subscribe.js' import type { WithSnapshotContributes } from './enhancers/with-snapshot.js' import type { WithSubscribeContributes } from './enhancers/with-subscribe.js' // biome-ignore lint/suspicious/noEmptyInterface: for future use export interface StoreCreateOptions {} export type StoreSubscriber = ( /** * Callback to be called when state changes. */ callback: SubscribeListener, /** * Whether to sync the callback with the current state. */ sync?: boolean, /** * @deprecated It is confusing, and makes TS type wrong in callback's `changes` argument. It will be removed in the next major version. */ selector?: (state: State) => object ) => () => void export interface VanillaStore { /** * The mutable state object, whose changes will trigger subscribers. */ mutate: State /** * Restore to initial state. */ restore: (options?: RestoreOptions) => void } export interface RestoreOptions { /** * Exclude some **top** keys from restoring. * * @since 0.2.5 */ exclude?: (keyof State)[] } /** * Create a vanilla store. * * @param initState The initial state object. * @param options Options for creating the store. * * @since 0.2.0 */ export function createVanilla( initState: State, _options: StoreCreateOptions = {} ): VanillaStore & WithSubscribeContributes & WithSnapshotContributes { const proxyState = proxy(initState) function restore(options: RestoreOptions = {}) { const { exclude = [] } = options const clonedState = deepCloneWithRef(initState) const initialKeys = Object.keys(initState) for (const key of Object.keys(clonedState) as (keyof State)[]) { if (exclude.includes(key)) continue proxyState[key] = clonedState[key] } const addedKeys = Object.keys(proxyState).filter((key) => !initialKeys.includes(key)) for (const key of addedKeys as (keyof State)[]) { if (exclude.includes(key)) continue // delete keys that are not in the initial state delete proxyState[key] } } const store = { mutate: proxyState, restore } return withSubscribe(withSnapshot(store)) } export const create = createVanilla