@shined/reactive
Version:
⚛️ Proxy-driven state library for JavaScript application, Intuitive, Flexible and Written in TypeScript.
1 lines • 15.7 kB
Source Map (JSON)
{"version":3,"sources":["/home/runner/work/reactive/reactive/packages/reactive/dist/chunk-VDKMANM7.cjs","../src/react/use-snapshot.ts","../src/enhancers/react/with-use-snapshot.ts","../src/react/use-subscribe.ts","../src/enhancers/react/with-use-subscribe.ts","../src/react/create-with-hooks.ts"],"names":["Object","defineProperty","exports","value","_nullishCoalesce","lhs","rhsFn","_chunk7Y3UI2QQcjs","require","_react","_withselectorjs","useSnapshot","proxyState","selectorOrOption","maybeOptions","isUnmountedRef","useRef","call","useEffect","current","options","selector","isFunction","sync","updateInSync","isEqual","shallowEqual","_subscribe","useCallback","callback","subscribe","_getSnapshot","snapshot","_selector","s","_snapshot","useSyncExternalStoreWithSelector","a","b","withUseSnapshot","store","boundUseSnapshot","mutate","useSubscribe","listener","notifyInSync","listenerRef","unsubscribe","args","withUseSubscribe","boundUseSubscribe","createWithHooks","initialState","createVanilla"],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAAA,OAAAC,cAAA,CAAAC,SAAA,cAAA;IAAAC,OAAA;AAAA;AAAA,SAAAC,iBAAAC,GAAA,EAAAC,KAAA;IAAA,IAAAD,OAAA,MAAA;QAAA,OAAAA;IAAA,OAAA;QAAA,OAAAC;IAAA;AAAA;AAMA,IAAAC,oBAAAC,QAAA;AAEA,4BAAA;ACRA,IAAAC,SAAAD,QAAA;AACA,IAAAE,kBAAAF,QAAA;AAiDO,SAASG,YACdC,UAAA,EACAC,gBAAA,EACAC,YAAA;IAEA,IAAMC,iBAAiBN,OAAAO,MAAA,CAAAC,IAAA,CAAA,KAAA,GAAA;IAEvBR,OAAAS,SAAA,CAAAD,IAAA,CAAA,KAAA,GAAA;QACE,OAAO;YACLF,eAAeI,OAAA,GAAU;QAC3B;IACF,GA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O,wCACFoB;QACH7B,aAAa8B;;AAEjB;AFDA,6BAAA;AGrDO,SAASE,aACd/B,UAAA,EACAgC,QAAA,EACAC,YAAA;IAGA,IAAMC,cAAc9B,OAAAA,MAAAA,CAAAA,IAAAA,CAAAA,KAAAA,GAAAA;IACpB8B,YAAY3B,OAAA,GAAUyB;IAEtB1B,OAAAA,SAAAA,CAAAA,IAAAA,CAAAA,KAAAA,GAAAA;YAGiB4B;QAFf,IAAMC,cAAcxC,kBAAAuB,SAAA,CAAAb,IAAA,CAAA,KAAA,GAClBL,YACA;6CAAIoC;gBAAAA;;mBAASF,CAAAA,eAAAA,aAAY3B,OAAA,OAAZ2B,cAAoB,qBAAGE;WACpCH;QAGF,OAAOE;IACT,GAAG;QAACnC;QAAYiC;KAAa;AAC/B;AHkDA,4CAAA;AI9CO,SAASI,iBACdT,KAAA;IAEA,IAAMU,oBAAoB,SACxBN,UACAC;QAEA,OAAOF,aAAaH,MAAME,MAAA,EAAQE,UAAUC;IAC9C;IAEA,OAAO,wCACFL;QACHG,cAAcO;;AAElB;AJ2CA,iCAAA;AK7DO,SAASC,gBACdC,YAAA;QACAhC,UAAAA,iEAA8B,CAAC;IAE/B,OAAOmB,gBAAgBU,iBAAiB1C,kBAAA8C,aAAA,CAAApC,IAAA,CAAA,KAAA,GAAAmC,cAAmChC;AAC7E;ALmEAlB,QAAAS,WAAA,GAAAA;AAAAT,QAAAqC,eAAA,GAAAA;AAAArC,QAAAyC,YAAA,GAAAA;AAAAzC,QAAA+C,gBAAA,GAAAA;AAAA/C,QAAAiD,eAAA,GAAAA","sourcesContent":["\"use strict\";Object.defineProperty(exports, \"__esModule\", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }\n\n\n\n\n\nvar _chunk7Y3UI2QQcjs = require('./chunk-7Y3UI2QQ.cjs');\n\n// src/react/use-snapshot.ts\nvar _react = require('react');\nvar _withselectorjs = require('use-sync-external-store/shim/with-selector.js');\nfunction useSnapshot(proxyState, selectorOrOption, maybeOptions) {\n const isUnmountedRef = _react.useRef.call(void 0, false);\n _react.useEffect.call(void 0, () => {\n return () => {\n isUnmountedRef.current = true;\n };\n }, []);\n let options;\n let selector;\n if (selectorOrOption && !_chunk7Y3UI2QQcjs.isFunction.call(void 0, selectorOrOption)) {\n options = selectorOrOption;\n selector = void 0;\n } else {\n options = maybeOptions;\n selector = selectorOrOption;\n }\n const { sync: updateInSync, isEqual = _chunk7Y3UI2QQcjs.shallowEqual } = _nullishCoalesce(options, () => ( {}));\n const _subscribe = _react.useCallback.call(void 0, \n (callback) => _chunk7Y3UI2QQcjs.subscribe.call(void 0, proxyState, callback, updateInSync),\n [proxyState, updateInSync]\n );\n const _getSnapshot = _react.useCallback.call(void 0, () => _chunk7Y3UI2QQcjs.snapshot.call(void 0, proxyState), [proxyState]);\n const _selector = selector || ((s) => s);\n const _snapshot = _withselectorjs.useSyncExternalStoreWithSelector.call(void 0, \n _subscribe,\n _getSnapshot,\n _getSnapshot,\n _selector,\n (a, b) => {\n if (isUnmountedRef.current) return true;\n return isEqual(a, b);\n }\n );\n return _snapshot;\n}\n\n// src/enhancers/react/with-use-snapshot.ts\nfunction withUseSnapshot(store) {\n const boundUseSnapshot = (selectorOrOption, maybeOptions) => {\n let options;\n let selector;\n if (selectorOrOption && !_chunk7Y3UI2QQcjs.isFunction.call(void 0, selectorOrOption)) {\n options = selectorOrOption;\n selector = void 0;\n } else {\n options = maybeOptions;\n selector = selectorOrOption;\n }\n return useSnapshot(store.mutate, selector, options);\n };\n return {\n ...store,\n useSnapshot: boundUseSnapshot\n };\n}\n\n// src/react/use-subscribe.ts\n\nfunction useSubscribe(proxyState, listener, notifyInSync) {\n const listenerRef = _react.useRef.call(void 0, listener);\n listenerRef.current = listener;\n _react.useEffect.call(void 0, () => {\n const unsubscribe = _chunk7Y3UI2QQcjs.subscribe.call(void 0, \n proxyState,\n (...args) => listenerRef.current(...args),\n notifyInSync\n );\n return unsubscribe;\n }, [proxyState, notifyInSync]);\n}\n\n// src/enhancers/react/with-use-subscribe.ts\nfunction withUseSubscribe(store) {\n const boundUseSubscribe = (listener, notifyInSync) => {\n return useSubscribe(store.mutate, listener, notifyInSync);\n };\n return {\n ...store,\n useSubscribe: boundUseSubscribe\n };\n}\n\n// src/react/create-with-hooks.ts\nfunction createWithHooks(initialState, options = {}) {\n return withUseSnapshot(withUseSubscribe(_chunk7Y3UI2QQcjs.createVanilla.call(void 0, initialState, options)));\n}\n\n\n\n\n\n\n\nexports.useSnapshot = useSnapshot; exports.withUseSnapshot = withUseSnapshot; exports.useSubscribe = useSubscribe; exports.withUseSubscribe = withUseSubscribe; exports.createWithHooks = createWithHooks;\n","import { useCallback, useEffect, useRef } from 'react'\nimport { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector.js'\nimport { isFunction, shallowEqual } from '../utils/index.js'\nimport { snapshot, subscribe } from '../vanilla/index.js'\n\nimport type { SubscribeListener } from '../vanilla/index.js'\n\nexport interface SnapshotOptions<StateSlice> {\n /**\n * Whether to notify updates synchronously, default is false, which means updates are batched asynchronously to improve performance.\n *\n * In some scenarios (e.g., using Chinese input method in a text box), it is necessary to immediately obtain the latest state, in which case this can be set to true.\n *\n * @defaultValue false\n */\n sync?: boolean\n /**\n * Custom equality function to compare the previous and next state slices.\n *\n * @defaultValue Object.is\n */\n isEqual?: (a: StateSlice, b: StateSlice) => boolean\n}\n\nexport type SnapshotSelector<State, StateSlice> = (state: State) => StateSlice\n\n/**\n * Returns a snapshot of the store state.\n *\n * @since 0.2.0\n */\nexport function useSnapshot<State extends object>(state: State): State\nexport function useSnapshot<State extends object>(\n state: State,\n options: SnapshotOptions<State>\n): State\nexport function useSnapshot<State extends object, StateSlice>(\n state: State,\n selector: SnapshotSelector<State, StateSlice>\n): StateSlice\nexport function useSnapshot<State extends object, StateSlice>(\n state: State,\n selector: undefined,\n options: SnapshotOptions<StateSlice>\n): State\nexport function useSnapshot<State extends object, StateSlice>(\n state: State,\n selector?: SnapshotSelector<State, StateSlice>,\n options?: SnapshotOptions<StateSlice>\n): StateSlice\nexport function useSnapshot<State extends object, StateSlice>(\n proxyState: State,\n selectorOrOption?: SnapshotOptions<StateSlice> | SnapshotSelector<State, StateSlice>,\n maybeOptions?: SnapshotOptions<StateSlice>\n) {\n const isUnmountedRef = useRef(false)\n\n useEffect(() => {\n return () => {\n isUnmountedRef.current = true\n }\n }, [])\n\n let options: SnapshotOptions<StateSlice> | undefined\n let selector: SnapshotSelector<State, StateSlice> | undefined\n\n if (selectorOrOption && !isFunction(selectorOrOption)) {\n options = selectorOrOption\n selector = undefined\n } else {\n options = maybeOptions\n selector = selectorOrOption\n }\n\n const { sync: updateInSync, isEqual = shallowEqual } = options ?? {}\n\n const _subscribe = useCallback(\n (callback: SubscribeListener<State>) => subscribe(proxyState, callback, updateInSync),\n [proxyState, updateInSync]\n )\n\n const _getSnapshot = useCallback(() => snapshot(proxyState), [proxyState])\n\n const _selector = (selector || ((s) => s)) as (state: State) => StateSlice\n\n const _snapshot = useSyncExternalStoreWithSelector<State, StateSlice>(\n _subscribe,\n _getSnapshot,\n _getSnapshot,\n _selector,\n (a, b) => {\n if (isUnmountedRef.current) return true\n return isEqual(a, b)\n }\n )\n\n return _snapshot as StateSlice\n}\n","import { useSnapshot } from '../../react/use-snapshot.js'\nimport { isFunction } from '../../utils/index.js'\n\nimport type { StoreUseSnapshot } from '../../react/create-with-hooks.js'\nimport type { SnapshotOptions, SnapshotSelector } from '../../react/use-snapshot.js'\nimport type { VanillaStore } from '../../vanilla/create.js'\n\nexport interface WithUseSnapshotContributes<State extends object> {\n /**\n * Get the snapshot of the state.\n */\n useSnapshot: StoreUseSnapshot<State>\n}\n\n/**\n * Enhances a store with `useSnapshot` method that returns a snapshot of the store's state.\n *\n * @param store - The store to enhance.\n * @returns The enhanced store.\n *\n * @since 0.2.0\n *\n * @example\n *\n * ```tsx\n * const store = withUseSnapshot(\n * createVanilla({\n * count: 123,\n * info: { name: 'Viki' },\n * }),\n * )\n *\n * // in normal JS/TS\n * console.log(store.snapshot(), store.snapshot((s) => s.count))\n *\n * // in React component\n * export function App() {\n * const { count } = store.useSnapshot()\n * const count = store.useSnapshot((s) => s.count)\n * }\n *\n * ```\n */\nexport function withUseSnapshot<Store extends VanillaStore<object>>(\n store: Store,\n): Store & WithUseSnapshotContributes<Store['mutate']> {\n const boundUseSnapshot: StoreUseSnapshot<Store['mutate']> = <StateSlice>(\n selectorOrOption?: SnapshotOptions<StateSlice> | SnapshotSelector<Store['mutate'], StateSlice> | undefined,\n maybeOptions?: SnapshotOptions<StateSlice>,\n ) => {\n let options: SnapshotOptions<StateSlice> | undefined\n let selector: SnapshotSelector<Store['mutate'], StateSlice> | undefined\n\n if (selectorOrOption && !isFunction(selectorOrOption)) {\n options = selectorOrOption\n selector = undefined\n } else {\n options = maybeOptions\n selector = selectorOrOption\n }\n\n return useSnapshot(store.mutate, selector, options)\n }\n\n return {\n ...store,\n useSnapshot: boundUseSnapshot,\n }\n}\n","import { useEffect, useRef } from 'react'\nimport { subscribe } from '../vanilla/subscribe'\n\nimport type { SubscribeListener } from '../vanilla/subscribe'\n\n/**\n * Subscribes to the store state changes in React components.\n *\n * @param proxyState - The store state.\n * @param listener - The listener function.\n * @param notifyInSync - Whether to notify the listener in sync.\n *\n * @since 0.2.0\n */\nexport function useSubscribe<State extends object>(\n proxyState: State,\n listener: SubscribeListener<State>,\n notifyInSync?: boolean\n) {\n // useLatest\n const listenerRef = useRef(listener)\n listenerRef.current = listener\n\n useEffect(() => {\n const unsubscribe = subscribe(\n proxyState,\n (...args) => listenerRef.current(...args),\n notifyInSync\n )\n\n return unsubscribe\n }, [proxyState, notifyInSync])\n}\n","import { useSubscribe } from '../../react/use-subscribe.js'\n\nimport type { SubscribeListener, VanillaStore } from '../../vanilla/index.js'\n\nexport interface WithUseSubscribeContributes<State extends object> {\n /**\n * Subscribes to the store state changes in React components.\n *\n * @param listener - The listener function.\n * @param notifyInSync - Whether to notify the listener in sync.\n */\n useSubscribe: (listener: SubscribeListener<State>, notifyInSync?: boolean) => void\n}\n\n/**\n * Enhance a store with the `useSubscribe` methods.\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, withUseSubscribe } from '@reactive-react/core'\n *\n * const store = withUseSubscribe(createVanilla({ count: 0 }))\n *\n * // In a React component\n * store.useSubscribe((state) => {\n * console.log(state.count)\n * })\n * ```\n *\n */\nexport function withUseSubscribe<Store extends VanillaStore<object>>(\n store: Store\n): Store & WithUseSubscribeContributes<Store['mutate']> {\n const boundUseSubscribe = (\n listener: SubscribeListener<Store['mutate']>,\n notifyInSync?: boolean\n ) => {\n return useSubscribe(store.mutate, listener, notifyInSync)\n }\n\n return {\n ...store,\n useSubscribe: boundUseSubscribe,\n }\n}\n","import { createVanilla } from '../vanilla/index.js'\nimport { withUseSnapshot, withUseSubscribe } from '../enhancers/react/index.js'\n\nimport type {\n WithUseSnapshotContributes,\n WithUseSubscribeContributes,\n} from '../enhancers/react/index.js'\nimport type { ExpandType } from '../utils/index.js'\nimport type { StoreCreateOptions, VanillaStore } from '../vanilla/create.js'\nimport type { WithSnapshotContributes, WithSubscribeContributes } from '../vanilla/index.js'\nimport type { SnapshotOptions, SnapshotSelector } from './use-snapshot.js'\n\nexport interface StoreUseSnapshot<State> {\n (): State\n (options: SnapshotOptions<State>): State\n <StateSlice>(selector: SnapshotSelector<State, StateSlice>): StateSlice\n <StateSlice>(selector: undefined, options: SnapshotOptions<StateSlice>): State\n <StateSlice>(\n selector: SnapshotSelector<State, StateSlice>,\n options: SnapshotOptions<StateSlice>\n ): StateSlice\n}\n\nexport interface Store<State extends object>\n extends ExpandType<\n VanillaStore<State> &\n WithSubscribeContributes<State> &\n WithSnapshotContributes<State> &\n WithUseSnapshotContributes<State> &\n WithUseSubscribeContributes<State>\n > {}\n\nexport function createWithHooks<State extends object>(\n initialState: State,\n options: StoreCreateOptions = {}\n): Store<State> {\n return withUseSnapshot(withUseSubscribe(createVanilla<State>(initialState, options)))\n}\n"]}