UNPKG

@qundus/qstate

Version:
1 lines 25.7 kB
{"version":3,"sources":["../../../src/integrations/astro/const.ts","../../../src/checks/is-server-side.ts","../../../src/integrations/astro/methods.ts","../../../src/integrations/astro/addon.ts","../../../src/core/addons/checks.ts","../../../src/core/hooks/_processor.ts","../../../src/core/persist-options/_processor.ts","../../../src/core/persist-options/persist-options-from-setup.ts","../../../src/stores/derivations/index.ts","../../../src/core/addons/derive.ts","../../../src/core/addons/get-new.ts","../../../src/core/addons/update.ts"],"names":["ACTIONS_KEY","is_server_side_default","methods_default","ctx","key","data","initialData","_a","result","form","button","clickEvent","e","astroIntegrationAddon","state","_options","Astro","newValue","_actions","actions","a","error","actionsKey","err","checksAddon","optionHooksProcessor","_derivedOptions","options","hooks","hasKey","func","optionPersistOptionsProcessor","persistOptions","__spreadValues","persistOptionsForDerived","_base","base","target","derive","store","fn","derived","computed","value","_b","deriveAddon","optionsMerger","getNewAddon","next","prev","abort","updateAddon"],"mappings":"kDAAO,IAAA,CAAA,CAAA,MAAA,CAAA,cAAA,CAAA,IAAA,CAAA,CAAA,MAAA,CAAA,qBAAA,CAAA,IAAA,CAAA,CAAA,MAAA,CAAA,SAAA,CAAA,cAAA,CAAA,CAAA,CAAA,MAAA,CAAA,SAAA,CAAA,oBAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,UAAA,CAAA,IAAA,CAAA,YAAA,CAAA,IAAA,CAAA,QAAA,CAAA,IAAA,CAAA,KAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,IAAA,IAAA,CAAA,IAAA,CAAA,GAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CA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CAAA,KAAA,CAAA,CAAAA,EAAe,CAAE,KAAA,CAAOkB,EAAM,IAAA,CAAAC,CAAAA,CAAM,KAAA,CAAAC,CAAM,CAAA,CAAA,CAClDpC,CAAAA,CAAM,IAAIkC,CAAI,CAAA,CACP,IACR,CAAA,MAASpC,CAAAA,CAAG,CAEX,OAAO,CAAE,OAAA,CAASA,CAAE,CACrB,CACD","file":"index.cjs","sourcesContent":["export const ACTIONS_KEY = \"QSTATE\";\n","export default function () {\n\tif (typeof document === \"undefined\") {\n\t\treturn true;\n\t}\n\treturn false;\n}\n","/// <reference types=\"astro/client\" />\nimport type { ActionAPIContext } from \"astro:actions\";\nimport type { APIContext } from \"astro\";\nimport { ActionError } from \"astro/actions/runtime/virtual/shared.js\";\nimport isServerSide from \"../../checks/is-server-side\";\n\nexport default {\n\tsetLocal: (ctx: APIContext | ActionAPIContext, key: string, data: any) => {\n\t\tctx.cookies.set(key, data, { path: \"/\" });\n\t\t// @ts-ignore\n\t\tctx.locals[key] = data;\n\t\t// locale.$store.set(result);\n\t\treturn { key, data };\n\t},\n\tgetLocal: (ctx: APIContext | ActionAPIContext, key: string, initialData: any) => {\n\t\tlet result = ctx.cookies.get(key)?.json();\n\t\t// console.log('inside getLocale :: ', result);\n\t\tif (result == null) {\n\t\t\tresult = initialData;\n\t\t\tctx.cookies.set(key, result, { path: \"/\" });\n\t\t}\n\t\t// @ts-ignore\n\t\tctx.locals[key] = result;\n\t\t// locale.$store.set(result);\n\t\treturn { key, data: result };\n\t},\n\trefreshServerContent() {\n\t\tif (isServerSide()) {\n\t\t\tconsole.warn(\"qstate: you're trying to set state with astroSetAddon on server, aborting!\");\n\t\t\treturn;\n\t\t}\n\t\t// 1. Create a hidden form (identical to your original)\n\t\tconst form = document.createElement(\"form\");\n\t\tform.method = \"POST\";\n\t\tform.action = window.location.pathname;\n\t\t// form.action = Astro.url.pathname;\n\t\tform.style.display = \"none\";\n\n\t\t// 2. Create and append a submit button\n\t\tconst button = document.createElement(\"button\");\n\t\tbutton.type = \"submit\";\n\t\tform.appendChild(button);\n\n\t\t// 3. Add to DOM (required for submission)\n\t\tdocument.body.appendChild(form);\n\n\t\t// 4. Create and dispatch a perfect click event\n\t\tconst clickEvent = new MouseEvent(\"click\", {\n\t\t\tbubbles: true,\n\t\t\tcancelable: true,\n\t\t\tview: window,\n\t\t});\n\n\t\t// 5. Clean up (but delay slightly for submission to complete)\n\t\tform.addEventListener(\"\", (e) => {\n\t\t\t// if (form.parentNode) {\n\t\t\t// \tdocument.body.removeChild(form);\n\t\t\t// }\n\t\t});\n\n\t\t// 6. Trigger the click AND let default form handling occur\n\t\tbutton.dispatchEvent(clickEvent);\n\t},\n};\n","import type { APIContext, AstroGlobal } from \"astro\";\nimport type { NanoAnyStore, NanoStoreValue } from \"../../_model\";\nimport type { _AddonFactory } from \"../../core/addons/_model\";\nimport { ACTIONS_KEY } from \"./const\";\nimport methods from \"./methods\";\n\nexport type AddonAstroIntegration<S extends NanoAnyStore> = {\n\tmiddleware: (ctx: any) => NanoStoreValue<S>;\n\tset: (value: NanoStoreValue<S>, actions?: any) => Promise<void>;\n};\n\n/**\n * extra functionality for astrojs framework\n */\nexport const astroIntegrationAddon: _AddonFactory<\n\tAddonAstroIntegration<NanoAnyStore>,\n\t{ name: string }\n> = (state, _options) => {\n\tif (state == null) {\n\t\treturn null as any;\n\t} else if (state.name == null) {\n\t\treturn null as any;\n\t}\n\tconst key = state.name;\n\treturn {\n\t\tmiddleware: (ctx) => {\n\t\t\tconst result = methods.getLocal(ctx, key, state.value);\n\t\t\treturn result.data;\n\t\t},\n\t\tget: (Astro: AstroGlobal) => {\n\t\t\tif (typeof Astro === \"undefined\") {\n\t\t\t\tconsole.error(\"qstate: please pass Astro global object to use store value!\");\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\t// @ts-ignore\n\t\t\tconst result = Astro.locals[key];\n\t\t\t// console.log(\"astro locals :: \", key, \" :: \", Astro.locals);\n\t\t\treturn result;\n\t\t},\n\t\tset: async (newValue, _actions) => {\n\t\t\ttry {\n\t\t\t\tlet actions = _actions as any;\n\t\t\t\tif (actions == null) {\n\t\t\t\t\t// @ts-ignore\n\t\t\t\t\tconst { actions: a } = await import(\"astro:actions\");\n\t\t\t\t\tactions = a;\n\t\t\t\t}\n\n\t\t\t\tlet error = null as string | null;\n\t\t\t\tconst actionsKey = ACTIONS_KEY;\n\n\t\t\t\tif (actions == null) {\n\t\t\t\t\terror = \"astro:actions actions object do not exist\";\n\t\t\t\t} else if (actions[actionsKey] == null) {\n\t\t\t\t\terror = actionsKey + \" is not added to actions, please add qStateAstroActions!\";\n\t\t\t\t} else if (typeof actions[actionsKey].setLocal !== \"function\") {\n\t\t\t\t\terror = \"setLocal action is undefined, this is fatal!, how did this happen?\";\n\t\t\t\t}\n\t\t\t\tif (error != null) {\n\t\t\t\t\tconsole.error(\"qstate: \", error);\n\t\t\t\t\treturn state.get();\n\t\t\t\t}\n\n\t\t\t\tstate.set(newValue);\n\t\t\t\tawait actions[actionsKey].setLocal({ key, data: newValue });\n\t\t\t\tmethods.refreshServerContent();\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(\"qstate: an error occured while trying to setLocal \" + key + \" \\n \" + err);\n\t\t\t}\n\t\t},\n\t};\n};\n","import isServerSide from \"../../checks/is-server-side\";\nimport type { _AddonFactory } from \"./_model\";\n\nexport type AddonChecks = {\n\tisServerSide: typeof isServerSide;\n};\nexport const checksAddon: _AddonFactory<AddonChecks> = () => {\n\treturn {\n\t\tget isServerSide() {\n\t\t\treturn isServerSide;\n\t\t},\n\t};\n};\n","import type { _OptionProcessorFactory } from \"../../_model\";\n\nexport const optionHooksProcessor: _OptionProcessorFactory<{ hooks: any }> = (\n\tstate,\n\t_options,\n\t_derivedOptions,\n) => {\n\tconst options = _options ?? _derivedOptions;\n\tif (options == null || options.hooks == null) {\n\t\treturn;\n\t}\n\tconst { hooks } = options;\n\tlet hasKey = false;\n\tstate.hooks = {};\n\tfor (const key in hooks) {\n\t\tconst func = hooks[key] as any;\n\t\tif (typeof func !== \"function\") {\n\t\t\tcontinue;\n\t\t}\n\t\thasKey = true;\n\t\tObject.defineProperty(state.hooks, key, {\n\t\t\tenumerable: true,\n\t\t\twritable: false,\n\t\t\tconfigurable: false,\n\t\t\tvalue: func?.(state, options),\n\t\t});\n\t}\n\tif (!hasKey) {\n\t\tstate.hooks = null;\n\t}\n};\n","import type { _OptionProcessorFactory } from \"../../_model\";\n\nexport const optionPersistOptionsProcessor: _OptionProcessorFactory<{ __options: any }> = (\n\tstate,\n\t_options,\n\t_derivedOptions,\n) => {\n\tconst options = _options ?? _derivedOptions;\n\tif (options == null || !options.persistOptions) {\n\t\treturn;\n\t}\n\tconst persistOptions = options?.persistOptions;\n\tif (persistOptions) {\n\t\tif (persistOptions !== \"pass-down\") {\n\t\t\tdelete options.persistOptions;\n\t\t}\n\t\tstate.__options = Object.freeze(Object.assign({}, { ...options }));\n\t}\n};\n","import type { NanoAnyStore, NanoReadable, Options, OptionsDerived } from \"../../_model\";\nimport type { MergedOptions, MergedOptionsDerived, OptionsSetup } from \"./_model\";\n\nexport function persistOptionsFromSetup<G extends OptionsSetup<NanoAnyStore>>(_base?: G) {\n\t// first mix both incase of store specific options like atom persistent\n\tconst base = _base as G;\n\t// const base = Object.assign({}, _base ?? {}) as G;\n\n\t// TODO: process setup options\n\n\t// for now types have to be defined manually\n\treturn function persistOptionsFromSetupRoot<S extends NanoAnyStore, O extends Options<S>>(\n\t\ttarget?: O,\n\t): MergedOptions<S, O, G> {\n\t\tif (target == null) {\n\t\t\tif (_base == null) {\n\t\t\t\treturn null as any;\n\t\t\t}\n\t\t\treturn base as any;\n\t\t}\n\t\ttarget = { ...base, ...target };\n\n\t\t// special handling\n\t\t// addons\n\t\tif (base?.addons != null || target?.addons != null) {\n\t\t\ttarget.addons = { ...(base?.addons as any), ...target?.addons };\n\t\t}\n\n\t\t// hooks\n\t\tif (base?.hooks != null || target?.hooks != null) {\n\t\t\ttarget.hooks = { ...(base?.hooks as any), ...target?.hooks };\n\t\t}\n\n\t\t// events\n\t\tif (base?.events != null || target?.events != null) {\n\t\t\ttarget.events = { ...(base?.events as any), ...target?.events };\n\t\t\t// console.log(\"merging options :: \", target.events);\n\t\t}\n\n\t\treturn target as any;\n\t};\n}\n\nexport function persistOptionsForDerived<G extends OptionsDerived | Options<NanoAnyStore>>(\n\t_base?: G,\n) {\n\t// first mix both incase of store specific options like atom persistent\n\tconst base = Object.assign({}, _base ?? {}) as G;\n\n\t// TODO: process setup options\n\n\t// for now types have to be defined manually\n\treturn function persistOptionsForDerivedRoot<S extends NanoReadable, O extends OptionsDerived>(\n\t\ttarget?: O,\n\t): MergedOptionsDerived<S, O, G> {\n\t\tif (target == null) {\n\t\t\tif (_base == null) {\n\t\t\t\treturn null as any;\n\t\t\t}\n\t\t\treturn base as any;\n\t\t}\n\t\ttarget = { ...base, ...target };\n\n\t\t// special handling\n\t\t// hooks\n\t\tif (base?.hooks != null || target?.hooks != null) {\n\t\t\ttarget.hooks = { ...(base?.hooks as any), ...target?.hooks };\n\t\t}\n\n\t\t// events\n\t\t// if (target?.events != null) {\n\t\t// \t// why even merge on error??????!!!\n\t\t// \t// let baseOnError = null as null | ((error: unknown) => void);\n\t\t// \t// if (base?.events != null && \"onError\" in base.events) {\n\t\t// \t// \tbaseOnError = base.events.onError as typeof baseOnError;\n\t\t// \t// }\n\t\t// \t// if (baseOnError != null || target?.events?.onError != null) {\n\t\t// \t// \tresult.events.onError = (err) => {\n\t\t// \t// \t\tbaseOnError?.(err);\n\t\t// \t// \t\ttarget?.events?.onError?.(err);\n\t\t// \t// \t};\n\t\t// \t// }\n\t\t// }\n\n\t\treturn target as any;\n\t};\n}\n//\n// export type MergedOptionsDerived<\n// \tG extends OptionsDerived | Options<NanoAnyStore> | unknown,\n// \tD extends OptionsDerived | unknown,\n// > = G extends OptionsDerived | Options<NanoAnyStore>\n// \t? {\n// \t\t\thooks: D extends OptionsDerived ? G[\"hooks\"] & D[\"hooks\"] : G[\"hooks\"];\n// \t\t\tevents: D extends OptionsDerived\n// \t\t\t\t? G extends OptionsDerived\n// \t\t\t\t\t? G[\"events\"] & D[\"events\"]\n// \t\t\t\t\t: D[\"events\"]\n// \t\t\t\t: G[\"events\"];\n// \t\t\tpersistOptions: D extends OptionsDerived ? D[\"persistOptions\"] : unknown;\n// \t\t}\n// \t: D;\n// export function mergeOptionsDerived<\n// \tG extends OptionsDerived | Options<NanoAnyStore> | undefined | null,\n// \tD extends OptionsDerived | undefined | null,\n// >(base?: G, target?: D) {\n// \tif (base == null && target == null) {\n// \t\treturn null as any;\n// \t}\n\n// \t// first mix both incase of store specific options like atom persistent\n// \tconst result = { ...base, ...target, events: null, hooks: null } as unknown as OptionsDerived;\n\n// \t// hooks\n// \tif (base?.hooks == null && target?.hooks == null) {\n// \t\tresult.hooks = null;\n// \t} else {\n// \t\tresult.hooks = {};\n// \t\tif (base?.hooks != null) {\n// \t\t\tresult.hooks = { ...base?.hooks };\n// \t\t}\n// \t\tif (target?.hooks != null) {\n// \t\t\tresult.hooks = { ...(result.hooks as any), ...target?.hooks };\n// \t\t}\n// \t}\n\n// \t// events\n// \tif (base?.events == null && target?.events == null) {\n// \t\tresult.events = null as any;\n// \t} else {\n// \t\tresult.events = {};\n// \t\tlet baseOnError = null as null | ((error: unknown) => void);\n// \t\t// @ts-ignore\n// \t\tif (base?.events != null && \"onError\" in base.events) {\n// \t\t\tbaseOnError = base.events.onError as typeof baseOnError;\n// \t\t}\n// \t\tif (baseOnError != null || target?.events?.onError != null) {\n// \t\t\tresult.events.onError = (err) => {\n// \t\t\t\tbaseOnError?.(err);\n// \t\t\t\ttarget?.events?.onError?.(err);\n// \t\t\t};\n// \t\t}\n// \t}\n\n// \treturn result as MergedOptionsDerived<G, D>;\n// }\n","import { computed, type Task } from \"nanostores\";\nimport type {\n\tNanoAnyStore,\n\tNanoStoreValue,\n\tNanoStoreValues,\n\tOptionsDerived,\n\tStoreDerived,\n\tStoreDerivedWithOptions,\n} from \"../../_model\";\nimport { optionHooksProcessor } from \"../../core/hooks/_processor\";\nimport { optionPersistOptionsProcessor } from \"../../core/persist-options/_processor\";\nimport { persistOptionsFromStoreDerived } from \"../../core/persist-options/persist-options-from-store\";\n\nexport function derive<S extends NanoAnyStore, R, O extends OptionsDerived>(\n\tstore: S,\n\tfn: (value: NanoStoreValue<S>) => Task<R> | R,\n\toptions?: O,\n) {\n\tif (fn == null || typeof fn !== \"function\") {\n\t\tthrow new Error(\"state: derived function must exist!\");\n\t}\n\tconst derived = computed(store, (value) => {\n\t\ttry {\n\t\t\treturn fn(value);\n\t\t} catch (err) {\n\t\t\toptions?.events?.onError?.(err);\n\t\t\treturn null; // Fallback\n\t\t}\n\t});\n\toptionHooksProcessor(derived, undefined, options);\n\toptionPersistOptionsProcessor(derived, undefined, options);\n\treturn derived as StoreDerived<R, O>;\n}\n\nexport function deriveMany<S extends NanoAnyStore[], R, O extends OptionsDerived>(\n\tstore: [...S],\n\tfn: (...value: NanoStoreValues<S>) => Task<R> | R,\n\toptions?: O,\n) {\n\tif (fn == null || typeof fn !== \"function\") {\n\t\tthrow new Error(\"state: derived function must exist!\");\n\t}\n\tconst derived = computed(store, (...value) => {\n\t\ttry {\n\t\t\treturn fn(...value);\n\t\t} catch (err) {\n\t\t\toptions?.events?.onError?.(err);\n\t\t\treturn null; // Fallback\n\t\t}\n\t});\n\toptionHooksProcessor(derived, undefined, options);\n\toptionPersistOptionsProcessor(derived, undefined, options);\n\treturn derived as StoreDerived<R, O>;\n}\n\nexport function deriveFrom<S extends StoreDerivedWithOptions, R, O extends OptionsDerived>(\n\tstore: S,\n\tfn: (value: NanoStoreValue<S>) => Task<R> | R,\n\t_options?: O,\n) {\n\tconst options = persistOptionsFromStoreDerived(store, _options);\n\treturn derive(store, fn, options);\n}\n","import type {\n\tNanoAnyStore,\n\tNanoStoreValue,\n\tOptions,\n\tOptionsDerived,\n\tStoreDerived,\n} from \"../../_model\";\nimport { derive } from \"../../stores/derivations\";\nimport type { MergedOptionsDerived } from \"../persist-options/_model\";\nimport { persistOptionsForDerived } from \"../persist-options/persist-options-from-setup\";\nimport type { _AddonFactory } from \"./_model\";\n\nexport type AddonDerive<S extends NanoAnyStore, O extends Options<S>> = <\n\tR,\n\tD extends OptionsDerived,\n>(\n\tfunc: (value: NanoStoreValue<S>) => R,\n\t_options?: D,\n) => StoreDerived<R, MergedOptionsDerived<S, D, O>>;\n\n/**\n * this addon uses deriveFrom offered by stores.derivations, it will internally adjust\n * options.persistOptions if it's not set or if it's set to false to allow passing options\n * to children/derivatives\n * @returns derived store based on this store's options;\n */\nexport const deriveAddon: _AddonFactory<AddonDerive<NanoAnyStore, Options<NanoAnyStore>>> = (\n\tstate,\n\tbase,\n) => {\n\tif (state == null) {\n\t\treturn null as any;\n\t}\n\tconst optionsMerger = persistOptionsForDerived(base);\n\t// console.log(\"base :: \", base);\n\treturn (func, _options) => {\n\t\tconst options = optionsMerger(_options);\n\t\t// if (state)\n\t\treturn derive(state, func, options); //as StoreDerived<R, typeof options>;\n\t};\n};\n","import type { NanoAnyStore, NanoStoreValue } from \"../../_model\";\nimport type { _AddonFactory } from \"./_model\";\n\n/**\n * unlike get, this gets the value after creating completely new value that helps\n * guarantees state notifying after using state.set with it.\n */\nexport type AddonGetNew<S extends NanoAnyStore> = () => NanoStoreValue<S>;\nexport const getNewAddon: _AddonFactory<AddonGetNew<NanoAnyStore>> = (state) => {\n\tif (state == null) {\n\t\treturn null as any;\n\t}\n\tlet next = null as any; //as NanoStoreValue<S>;\n\tconst prev = state.get();\n\tif (Array.isArray(prev)) {\n\t\tnext = [...prev];\n\t\t// console.log(\"update is array\");\n\t} else if (typeof prev === \"object\") {\n\t\tnext = Object.assign({}, prev);\n\t\t// console.log(\"update is object\");\n\t} else {\n\t\tnext = prev;\n\t\t// console.log(\"update is \", typeof prev);\n\t}\n\treturn next;\n};\n","import type { NanoAnyStore, NanoStoreValue } from \"../../_model\";\nimport type { _AddonFactory } from \"./_model\";\n\nfunction abort() {\n\tthrow new Error(\"qstate: aborting state update!\");\n}\n\n/**\n * update store value, no need to set outer value to invoke reactivity.\n * use abort to cancel updates from within function, it throws an error\n * that is caught and returned\n * @example\n * const $state = atom(\"qstate\")\n * const err_update = $state.actions.update(({$next, abort}) => {\n * if ($next == null) {\n * abort();\n * }\n * return $next + \" awesome\"\n * })\n */\nexport type AddonUpdate<S extends NanoAnyStore> = (\n\tfunc: (props: {\n\t\t$next: NanoStoreValue<S>;\n\t\tprev: NanoStoreValue<S>;\n\t\tabort: () => void;\n\t}) => NanoStoreValue<S>,\n) => null | { message: any };\n\nexport const updateAddon: _AddonFactory<AddonUpdate<NanoAnyStore>> = (state) => {\n\tif (state == null) {\n\t\treturn null as any;\n\t}\n\treturn (func) => {\n\t\ttry {\n\t\t\tif (typeof func !== \"function\") {\n\t\t\t\tthrow new Error(\"qstate: update is not of type function!\");\n\t\t\t}\n\t\t\tlet next = null; //as V;\n\t\t\tconst prev = state.get();\n\t\t\tif (Array.isArray(prev)) {\n\t\t\t\tnext = [...prev];\n\t\t\t\t// console.log(\"update is array\");\n\t\t\t} else if (typeof prev === \"object\") {\n\t\t\t\tnext = Object.assign({}, prev);\n\t\t\t\t// console.log(\"update is object\");\n\t\t\t} else {\n\t\t\t\tnext = prev;\n\t\t\t\t// console.log(\"update is \", typeof prev);\n\t\t\t}\n\t\t\tnext = (func as any)?.({ $next: next, prev, abort });\n\t\t\tstate.set(next);\n\t\t\treturn null;\n\t\t} catch (e) {\n\t\t\t// TODO: do something;\n\t\t\treturn { message: e };\n\t\t}\n\t};\n};\n"]}