{"version":3,"file":"just-web-react.cjs","sources":["../ts/just_app_context.tsx","../ts/react_gizmo.ts","../ts/store.ts","../ts/store_context.tsx"],"sourcesContent":["import React from 'react'\nimport type { GizmoIncubator, JustApp } from '@just-web/app'\nimport { createContext, useContext, type Context, type PropsWithChildren } from 'react'\nimport type { NonUndefined } from 'type-plus'\nimport type { ReactGizmo } from './react_gizmo.js'\n\nexport type JustReactApp = JustApp & ReactGizmo\n\nconst JustAppRootContext = createContext<JustApp & Partial<ReactGizmo>>(undefined as any)\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 value={value} key={key}>\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\nexport function useJustAppContext<App extends JustReactApp>(context: Context<App>): App\nexport function useJustAppContext<App = JustApp>(): NonUndefined<App>\nexport function useJustAppContext(context = JustAppRootContext) {\n\tconst app = useContext(context)\n\tif (!app) throw new Error('A JustApp context provider must be used before using useJustAppContext()')\n\treturn app\n}\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<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 { 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))), [])\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(value => {\n\t\t\tif (updater) {\n\t\t\t\tstore.set(s =>\n\t\t\t\t\tupdater(\n\t\t\t\t\t\ts,\n\t\t\t\t\t\tisType<(value: V) => V>(value, u => typeof u === 'function') ? value(getState(s)) : value\n\t\t\t\t\t)\n\t\t\t\t)\n\n\t\t\t\treturn setValue(getState(store.get()))\n\t\t\t}\n\t\t\treturn setValue(value)\n\t\t}, [])\n\t]\n}\n","import type { Store, Updater } from '@just-web/states'\nimport { createContext, useContext, type Context } from 'react'\nimport { useStore } from './store.js'\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\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 */\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"],"names":["createContext","jsx","app","useContext","children","define","useState","useEffect","s","createStore","useCallback","value","isType"],"mappings":";;;;;;;AAQA,MAAM,qBAAqBA,MAAAA,cAA6C,MAAgB;AAoBjF,SAAS,qBAA+C,eAAqC;AAC7F,QAAA,UAAUA,MAAAA,cAAmB,MAAgB;AACnD,QAAM,gBAAgB,QAAQ;AACtB,UAAA,WAAW,SAAS,SAAS;AAAA,IACpC;AAAA,IACA;AAAA,IACA;AAAA,EAAA,GAIG;AAEF,WAAAC,2BAAA,IAAC,mBAAmB,UAAnB,EAA4B,OAC5B,yCAAC,eAAc,EAAA,OACb,SADgC,GAAA,GAElC,EACD,CAAA;AAAA,EAAA;AAGK,SAAA;AACR;AAIgB,SAAA,kBAAkB,UAAU,oBAAoB;AACzD,QAAAC,OAAMC,iBAAW,OA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