{"version":3,"file":"index.mjs","names":[],"sources":["../src/contexts/create_just_app_context.tsx","../src/contexts/just_app_provider.tsx","../src/contexts/store_context.tsx","../src/gizmos/react_gizmo.ts","../src/hooks/use_store.ts","../src/hooks/use_store_context.tsx"],"sourcesContent":["import type { GizmoIncubator } from '@just-web/app'\nimport React, { createContext, type PropsWithChildren } from 'react'\nimport { JustAppRootContext, type JustReactApp } from './just_app_context'\n\n/**\n * Creates a context of a `JustReactApp` to be used in `useJustAppContext()`.\n *\n * Typically, you would specify the generic type `App` when callying this function.\n *\n * You can also call it and pass the `appIncubator` to infer the generic type `App`.\n * However, that is not the typical usage.\n *\n * @example\n * ```ts\n * const App1Context = createJustAppContext<App1>()\n *\n * function App1Info() {\n * \tconst app = useContext(App1Context)\n * \t// ...\n * }\n * ```\n */\nexport function createJustAppContext<App extends JustReactApp>(_appIncubator?: GizmoIncubator<App>) {\n\tconst Context = createContext<App>(undefined as any)\n\tconst InnerProvider = Context.Provider\n\tContext.Provider = function Provider({\n\t\tvalue,\n\t\tkey,\n\t\tchildren\n\t}: PropsWithChildren<{\n\t\tvalue: App\n\t\tkey?: string\n\t}>) {\n\t\treturn (\n\t\t\t<JustAppRootContext.Provider value={value}>\n\t\t\t\t<InnerProvider key={key} value={value}>\n\t\t\t\t\t{children}\n\t\t\t\t</InnerProvider>\n\t\t\t</JustAppRootContext.Provider>\n\t\t)\n\t} as any\n\treturn Context\n}\n","import type { JustApp } from '@just-web/app'\nimport React from 'react'\nimport type { ReactGizmo } from '../gizmos/react_gizmo'\nimport { JustAppRootContext } from './just_app_context'\n\nexport function JustAppProvider<App extends JustApp & Partial<ReactGizmo>>({\n\tvalue,\n\tchildren\n}: {\n\tvalue: App\n\tchildren: React.ReactNode\n}) {\n\tconst providers = Array.from(value.react?.providers.values() ?? [])\n\treturn (\n\t\t<JustAppRootContext.Provider value={value}>\n\t\t\t{providers.reduce(\n\t\t\t\t(children, Component) => (\n\t\t\t\t\t// biome-ignore lint/correctness/useJsxKeyInIterable: TODO\n\t\t\t\t\t<Component>{children}</Component>\n\t\t\t\t),\n\t\t\t\tchildren\n\t\t\t)}\n\t\t</JustAppRootContext.Provider>\n\t)\n}\n","import type { Store } from '@just-web/states'\nimport { createContext } from 'react'\n\n/**\n * Creates a `Store<T>`context to be used in `useStoreContext()`\n * @type T Type of the store value.\n */\nexport function createStoreContext<T extends Record<any, any>>() {\n\treturn createContext<Store<T>>(undefined as any)\n}\n","import { define } from '@just-web/app'\nimport type { JSXElementConstructor, ReactNode } from 'react'\n\nexport const reactGizmo = define({\n\tasync create() {\n\t\tconst components: Array<JSXElementConstructor<{ children: ReactNode }>> = []\n\t\treturn {\n\t\t\treact: {\n\t\t\t\tproviders: {\n\t\t\t\t\tregister(Component: JSXElementConstructor<{ children: ReactNode }>) {\n\t\t\t\t\t\tcomponents.push(Component)\n\t\t\t\t\t},\n\t\t\t\t\tvalues(): IterableIterator<JSXElementConstructor<{ children: ReactNode }>> {\n\t\t\t\t\t\treturn components.values()\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n})\n\nexport type ReactGizmo = define.Infer<typeof reactGizmo>\n","import type { Store, Updater } from '@just-web/states'\nimport { createStore } from '@just-web/states'\nimport { useCallback, useEffect, useState } from 'react'\nimport { isType } from 'type-plus'\n\n/**\n * Use a value in the store for `useState()`.\n * @param getState a function to get the value to be used in `useState()`.\n * @param updater optional function to update the store value when the state changes\n */\nexport function useStore<S extends Record<any, any>, V>(\n\tstore: Store<S>,\n\tgetState: (s: S) => V,\n\tupdater?: (draft: S, value: V) => ReturnType<Updater<S>>\n): [value: V, setValue: (value: V | ((value: V) => V)) => void] {\n\tconst [value, setValue] = useState(() => getState(store.get()))\n\n\tuseEffect(() => store.onChange((s) => setValue(getState(s))), [getState, store.onChange])\n\n\tconst s = createStore<{ a: number }>({ a: 1 })\n\ts.set((d) => {\n\t\td.a += 1\n\t})\n\treturn [\n\t\tvalue,\n\t\tuseCallback(\n\t\t\t(value) => {\n\t\t\t\tif (updater) {\n\t\t\t\t\tstore.set((s) => {\n\t\t\t\t\t\tconst v = s as unknown as S\n\t\t\t\t\t\treturn updater(\n\t\t\t\t\t\t\tv,\n\t\t\t\t\t\t\tisType<(value: V) => V>(value, (u) => typeof u === 'function') ? value(getState(v)) : value\n\t\t\t\t\t\t)\n\t\t\t\t\t})\n\n\t\t\t\t\treturn setValue(getState(store.get()))\n\t\t\t\t}\n\t\t\t\treturn setValue(value)\n\t\t\t},\n\t\t\t[getState, store.get, store.set, updater]\n\t\t)\n\t]\n}\n","import type { Store, Updater } from '@just-web/states'\nimport { type Context, useContext } from 'react'\nimport { useStore } from './use_store'\n\n/**\n * Uses a store context.\n * @param reactContext The context created from `createStoreContext()`.\n * @param getState The function to get a particular value from the store.\n * @param updateStore Optional. The function to update the store when the returning `setValue()` is called.\n */\n\nexport function useStoreContext<S extends Record<any, any>, V>(\n\treactContext: Context<Store<S>>,\n\tgetState: (s: S) => V,\n\tupdateStore?: (draft: S, value: V) => ReturnType<Updater<S>>\n): [value: V, setValue: (value: V | ((value: V) => V)) => void] {\n\tconst store = useContext(reactContext)\n\tif (!store) {\n\t\tthrow new Error('Context.Provider must be used before using useStoreContext()')\n\t}\n\treturn useStore(store, getState, updateStore)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,qBAA+C,eAAqC;CACnG,MAAM,UAAU,cAAmB,MAAgB;CACnD,MAAM,gBAAgB,QAAQ;CAC9B,QAAQ,WAAW,SAAS,SAAS,EACpC,OACA,KACA,YAIG;EACH,OACC,oCAAC,mBAAmB,UAApB,EAAoC,MAIP,GAH5B,oCAAC,eAAD;GAAoB;GAAY;EAEjB,GADb,QACa,CACa;CAE/B;CACA,OAAO;AACR;;;;ACrCA,SAAgB,gBAA2D,EAC1E,OACA,YAIE;CACF,MAAM,YAAY,MAAM,KAAK,MAAM,OAAO,UAAU,OAAO,KAAK,CAAC,CAAC;CAClE,OACC,oCAAC,mBAAmB,UA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