{"version":3,"sources":["../../../src/vanilla/enhancers/with-subscribe.ts"],"sourcesContent":["import { subscribe } from '../subscribe.js'\n\nimport type { SubscribeListener, VanillaStore } from '../index.js'\n\nexport interface WithSubscribeContributes<State extends object> {\n  /**\n   * Subscribe to the store's state changes.\n   *\n   * @param listener - The listener to be called when the state changes.\n   * @param sync - Whether to call the listener synchronously.\n   * @param selector - A selector to select a part of the state to be passed to the listener.\n   */\n  subscribe: (\n    listener: SubscribeListener<State>,\n    sync?: boolean,\n    selector?: (state: State) => object\n  ) => () => void\n}\n\n/**\n * Enhance a store with the `subscribe` method.\n *\n * @param store - The store to be enhanced.\n * @returns The enhanced store.\n *\n * @since 0.2.0\n *\n * @example\n *\n * ```ts\n * import { createVanilla, withSubscribe } from '@reactive-react/core'\n *\n * const store = withSubscribe(createVanilla({ count: 0 }))\n *\n * const unsubscribe = store.subscribe((state) => {\n *   console.log(state.count)\n * })\n * ```\n *\n */\nexport function withSubscribe<Store extends VanillaStore<object>>(\n  store: Store\n): Store & WithSubscribeContributes<Store['mutate']> {\n  function boundSubscribe(\n    listener: SubscribeListener<Store['mutate']>,\n    sync?: boolean,\n    selector: (state: Store['mutate']) => object = (s: Store['mutate']) => s\n  ) {\n    return subscribe(selector(store.mutate) as Store['mutate'], listener, sync)\n  }\n\n  return {\n    ...store,\n    subscribe: boundSubscribe,\n  }\n}\n"],"names":["withSubscribe","store","boundSubscribe","listener","sync","selector","s","subscribe","mutate"],"mappings":";;;;+BAwCgBA;;;eAAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAT,SAASA,cACdC,KAAY;IAEZ,SAASC,eACPC,QAA4C,EAC5CC,IAAc;YACdC,WAAAA,iEAA+C,SAACC;mBAAuBA;;QAEvE,OAAOC,IAAAA,uBAAS,EAACF,SAASJ,MAAMO,MAAM,GAAsBL,UAAUC;IACxE;IAEA,OAAO,wCACFH;QACHM,WAAWL;;AAEf"}