unwrapped 0.1.14 → 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/vue/index.js +3 -1
- package/dist/vue/index.js.map +1 -1
- package/dist/vue/index.mjs +3 -1
- package/dist/vue/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/vue/index.js
CHANGED
|
@@ -88,7 +88,9 @@ function useLazyGenerator(generatorFunc) {
|
|
|
88
88
|
function useReactiveGenerator(source, generatorFunc, options = { immediate: true }) {
|
|
89
89
|
const resultRef = useAsyncResultRef(new import_core.AsyncResult());
|
|
90
90
|
(0, import_vue.watch)(source, (newInputs) => {
|
|
91
|
-
resultRef.value.runInPlace((notifyProgress) =>
|
|
91
|
+
resultRef.value.runInPlace((notifyProgress) => {
|
|
92
|
+
return generatorFunc(newInputs, notifyProgress);
|
|
93
|
+
});
|
|
92
94
|
}, { immediate: options.immediate });
|
|
93
95
|
return resultRef;
|
|
94
96
|
}
|
package/dist/vue/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/vue/index.ts","../../src/vue/composables.ts","../../src/vue/components/asyncResultLoader.ts"],"sourcesContent":["export * from \"./composables\"\nexport * from \"./components/asyncResultLoader\"","import { onUnmounted, ref, triggerRef, watch, type Ref, type WatchSource } from \"vue\";\nimport { AsyncResult, AsyncResultCollection, ErrorBase, type Action, type AsyncResultGenerator, type FlatChainStep, type Result } from \"unwrapped/core\";\n\n\n// === Vue specific types ===\n\ninterface ReactiveProcessOptions {\n immediate: boolean;\n}\n\n\n\n// === Vue Composables for AsyncResult ===\n\n/**\n * Makes a ref to the given AsyncResult. Whenever the state of the AsyncResult changes,\n * the ref gets retriggered, making those changes visible to Vue's reactivity system.\n * \n * @param asyncResult the result to make reactive\n * @returns the ref to the result\n */\nexport function useAsyncResultRef<T, E extends ErrorBase = ErrorBase, P = unknown>(asyncResult?: AsyncResult<T, E, P>) {\n if (!asyncResult) {\n asyncResult = new AsyncResult<T, E, P>();\n }\n const state = ref<AsyncResult<T, E, P>>(asyncResult) as Ref<AsyncResult<T, E, P>>;\n\n const unsub = asyncResult.listen(() => {\n triggerRef(state);\n });\n\n onUnmounted(() => {\n unsub();\n });\n\n return state;\n}\n\n/**\n * Creates an AsyncResult ref from a promise returning a Result.\n * @param promise the promise returning a Result\n * @returns the ref to the AsyncResult\n */\nexport function useAsyncResultRefFromPromise<T, E extends ErrorBase = ErrorBase>(promise: Promise<Result<T, E>>) {\n return useAsyncResultRef(AsyncResult.fromResultPromise(promise));\n}\n\n\n\n// === Vue Composables for Chains ===\n\n/**\n * Watches a source, gives it as inputs to the function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param pipe the function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveChain<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, pipe: FlatChainStep<Inputs, T, E>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const result = new AsyncResult<T, E>();\n const resultRef = useAsyncResultRef(result);\n\n let unsub: (() => void) | null = null;\n\n watch(source, (newInputs) => {\n unsub?.();\n unsub = result.mirror(pipe(newInputs));\n }, { immediate: options.immediate });\n\n onUnmounted(() => {\n unsub?.();\n });\n\n return resultRef;\n}\n\n\n// === Vue Composables for Actions ===\n\n/**\n * The return type of useLazyAction.\n */\nexport interface LazyActionRef<T, E extends ErrorBase = ErrorBase, P = unknown> {\n resultRef: Ref<AsyncResult<T, E, P>>;\n trigger: () => void;\n}\n\n/**\n * Executes an action immediately and returns a ref to the AsyncResult representing the action's state.\n * \n * Same as useAsyncResultRefFromPromise(action()).\n * \n * @param action the action to execute immediately\n * @returns a ref to the AsyncResult representing the action's state\n */\nexport function useAction<T, E extends ErrorBase = ErrorBase, P = unknown>(action: Action<T, E, P>): Ref<AsyncResult<T, E, P>> {\n return useAsyncResultRef(AsyncResult.fromAction(action));\n}\n\n/**\n * Creates a lazy action that can be triggered manually.\n * \n * Same as AsyncResult.makeLazyAction(action), but the AsyncResult is wrapped in a ref for Vue reactivity.\n * \n * @param action the action to execute when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyAction<T, E extends ErrorBase = ErrorBase, P = unknown>(action: Action<T, E, P>): LazyActionRef<T, E, P> {\n const lazyAction = AsyncResult.makeLazyAction<T, E, P>(action);\n const resultRef = useAsyncResultRef(lazyAction.result);\n\n return { resultRef, trigger: lazyAction.trigger };\n}\n\n\n// === Vue Composables for Generators ===\n\n/**\n * Runs a generator function immediately and returns a ref to the AsyncResult representing the generator's state.\n * @param generatorFunc the generator function to run immediately\n * @returns a ref to the AsyncResult representing the generator's state\n */\nexport function useGenerator<T, P = unknown>(generatorFunc: (notifyProgress?: (progress: P) => void) => AsyncResultGenerator<T>): Ref<AsyncResult<T, any, P>> {\n const resultRef = useAsyncResultRef(AsyncResult.run(generatorFunc));\n return resultRef;\n}\n\n/**\n * Creates a lazy generator that can be triggered manually.\n * \n * @param generatorFunc the generator function to run when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyGenerator<T, P = unknown>(generatorFunc: (notifyProgress: (progress: P) => void) => AsyncResultGenerator<T>): { resultRef: Ref<AsyncResult<T, any, P>>, trigger: () => void } {\n const result = new AsyncResult<T, any, P>();\n const resultRef = useAsyncResultRef(result);\n\n const trigger = () => {\n result.runInPlace((notifyProgress) => generatorFunc(notifyProgress));\n }\n\n return { resultRef, trigger };\n}\n\n/**\n * Watches a source, gives it as inputs to the generator function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param generatorFunc the generator function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveGenerator<Inputs, T, E extends ErrorBase = ErrorBase, P = unknown>(source: WatchSource<Inputs>, generatorFunc: (args: Inputs, notifyProgress: (progress: P) => void) => AsyncResultGenerator<T>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E, P>> {\n const resultRef = useAsyncResultRef(new AsyncResult<T, E, P>());\n\n watch(source, (newInputs) => {\n resultRef.value.runInPlace((notifyProgress) => generatorFunc(newInputs, notifyProgress));\n }, { immediate: options.immediate });\n\n return resultRef;\n}\n\n\n// === Vue Composables for AsyncResultCollection ===\n\n/**\n * Creates a reactive AsyncResultCollection wrapped in a Vue ref.\n * The AsyncResultCollection notifies Vue's reactivity system whenever its state changes.\n * \n * @template T - The type of the values in the AsyncResultCollection.\n * @template E - The type of the error, extending ErrorBase (default is ErrorBase).\n * @returns a ref to the AsyncResultCollection\n */\nexport function useAsyncResultCollection<T = any, E extends ErrorBase = ErrorBase>() {\n const list = new AsyncResultCollection<T, E>();\n const listRef = ref(list);\n\n list.listen(() => {\n triggerRef(listRef);\n });\n\n return listRef;\n}","import { defineComponent, watch, h, type VNode, type SlotsType } from \"vue\"\nimport { ErrorBase, type AsyncResult } from \"unwrapped/core\"\nimport { useAsyncResultRef } from \"../composables\"\n\ninterface CustomSlots<E extends ErrorBase = ErrorBase, P = unknown> {\n loading?: (props: { progress?: P }) => VNode;\n error?: (props: { error: E }) => VNode;\n idle?: () => VNode;\n}\n\n/**\n * Factory function to create a component that displays different content based on an AsyncResult's state. Provides slots for loading, error, idle, and success states and passes the relevant data to each slot, and are typed appropriately.\n * @param slots predefined slots for loading, error, and idle states. Useful for not having to repeat the same template for displaying a framework-specific spinner while in loading state, or a custom error message.\n * @param name Optional internal name for the component\n * @returns An instantiable component that accepts an AsyncResult prop and a default slot for the success state.\n * \n * @example\n * // loaders.ts - Create reusable loader with default loading/error UI\n * const MyLoader = makeAsyncResultLoader({\n * loading: () => h(Spinner),\n * error: ({ error }) => h(ErrorDisplay, { error }),\n * idle: () => h('div', 'Ready')\n * });\n * \n * // MyPage.vue - Use the loader with custom success content\n * <MyLoader :result=\"myAsyncResult\">\n * <template #default=\"{ value }\">\n * <UserProfile :user=\"value\" />\n * </template>\n * </MyLoader>\n */\nexport function makeAsyncResultLoader<T, E extends ErrorBase = ErrorBase, P = unknown>(slots: CustomSlots<E, P>, name = \"AsyncResultLoader\") {\n return defineComponent({\n name,\n props: {\n result: {\n type: Object as () => AsyncResult<T, E, P>,\n required: true\n }\n },\n slots: Object as SlotsType<CustomSlots<E, P> & { default: { value: T } }>,\n setup(props, context) {\n let resultRef = useAsyncResultRef(props.result);\n\n // Watch for prop changes & update listener\n watch(\n () => props.result,\n (newResult, oldResult) => {\n if (newResult === oldResult) return;\n resultRef = useAsyncResultRef(newResult);\n },\n { immediate: true }\n )\n\n return () => {\n const s = resultRef.value.state;\n\n const renderDefault = context.slots.default ?? (() => h(\"div\", { class: \"success\" }, \"Success\"));\n const renderError = context.slots.error ?? slots.error ?? (() => h(\"div\", { class: \"error\" }, \"Error\"));\n const renderLoading = context.slots.loading ?? slots.loading ?? (() => h(\"div\", { class: \"loading\" }, \"Loading…\"));\n const renderIdle = context.slots.idle ?? slots.idle ?? (() => h(\"div\", { class: \"idle\" }, \"Idle\"));\n\n // Choose what to render based on status\n switch (s.status) {\n case \"loading\":\n return renderLoading({ progress: s.progress });\n\n case \"error\":\n return renderError({ error: s.error });\n\n case \"success\":\n return renderDefault({ value: s.value });\n\n default:\n return renderIdle();\n }\n }\n }\n })\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAAgF;AAChF,kBAAuI;AAoBhI,SAAS,kBAAmE,aAAoC;AACnH,MAAI,CAAC,aAAa;AACd,kBAAc,IAAI,wBAAqB;AAAA,EAC3C;AACA,QAAM,YAAQ,gBAA0B,WAAW;AAEnD,QAAM,QAAQ,YAAY,OAAO,MAAM;AACnC,+BAAW,KAAK;AAAA,EACpB,CAAC;AAED,8BAAY,MAAM;AACd,UAAM;AAAA,EACV,CAAC;AAED,SAAO;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,YAAY,EAAE,OAAO,EAAE,MAAM,CAAC;AAAA,UAEzC,KAAK;AACD,mBAAO,cAAc,EAAE,OAAO,EAAE,MAAM,CAAC;AAAA,UAE3C;AACI,mBAAO,WAAW;AAAA,QAC1B;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ,CAAC;AACL;","names":["import_vue","import_core"]}
|
|
1
|
+
{"version":3,"sources":["../../src/vue/index.ts","../../src/vue/composables.ts","../../src/vue/components/asyncResultLoader.ts"],"sourcesContent":["export * from \"./composables\"\nexport * from \"./components/asyncResultLoader\"","import { onUnmounted, ref, triggerRef, watch, type Ref, type WatchSource } from \"vue\";\nimport { AsyncResult, AsyncResultCollection, ErrorBase, type Action, type AsyncResultGenerator, type FlatChainStep, type Result } from \"unwrapped/core\";\n\n\n// === Vue specific types ===\n\ninterface ReactiveProcessOptions {\n immediate: boolean;\n}\n\n\n\n// === Vue Composables for AsyncResult ===\n\n/**\n * Makes a ref to the given AsyncResult. Whenever the state of the AsyncResult changes,\n * the ref gets retriggered, making those changes visible to Vue's reactivity system.\n * \n * @param asyncResult the result to make reactive\n * @returns the ref to the result\n */\nexport function useAsyncResultRef<T, E extends ErrorBase = ErrorBase, P = unknown>(asyncResult?: AsyncResult<T, E, P>) {\n if (!asyncResult) {\n asyncResult = new AsyncResult<T, E, P>();\n }\n const state = ref<AsyncResult<T, E, P>>(asyncResult) as Ref<AsyncResult<T, E, P>>;\n\n const unsub = asyncResult.listen(() => {\n triggerRef(state);\n });\n\n onUnmounted(() => {\n unsub();\n });\n\n return state;\n}\n\n/**\n * Creates an AsyncResult ref from a promise returning a Result.\n * @param promise the promise returning a Result\n * @returns the ref to the AsyncResult\n */\nexport function useAsyncResultRefFromPromise<T, E extends ErrorBase = ErrorBase>(promise: Promise<Result<T, E>>) {\n return useAsyncResultRef(AsyncResult.fromResultPromise(promise));\n}\n\n\n\n// === Vue Composables for Chains ===\n\n/**\n * Watches a source, gives it as inputs to the function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param pipe the function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveChain<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, pipe: FlatChainStep<Inputs, T, E>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const result = new AsyncResult<T, E>();\n const resultRef = useAsyncResultRef(result);\n\n let unsub: (() => void) | null = null;\n\n watch(source, (newInputs) => {\n unsub?.();\n unsub = result.mirror(pipe(newInputs));\n }, { immediate: options.immediate });\n\n onUnmounted(() => {\n unsub?.();\n });\n\n return resultRef;\n}\n\n\n// === Vue Composables for Actions ===\n\n/**\n * The return type of useLazyAction.\n */\nexport interface LazyActionRef<T, E extends ErrorBase = ErrorBase, P = unknown> {\n resultRef: Ref<AsyncResult<T, E, P>>;\n trigger: () => void;\n}\n\n/**\n * Executes an action immediately and returns a ref to the AsyncResult representing the action's state.\n * \n * Same as useAsyncResultRefFromPromise(action()).\n * \n * @param action the action to execute immediately\n * @returns a ref to the AsyncResult representing the action's state\n */\nexport function useAction<T, E extends ErrorBase = ErrorBase, P = unknown>(action: Action<T, E, P>): Ref<AsyncResult<T, E, P>> {\n return useAsyncResultRef(AsyncResult.fromAction(action));\n}\n\n/**\n * Creates a lazy action that can be triggered manually.\n * \n * Same as AsyncResult.makeLazyAction(action), but the AsyncResult is wrapped in a ref for Vue reactivity.\n * \n * @param action the action to execute when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyAction<T, E extends ErrorBase = ErrorBase, P = unknown>(action: Action<T, E, P>): LazyActionRef<T, E, P> {\n const lazyAction = AsyncResult.makeLazyAction<T, E, P>(action);\n const resultRef = useAsyncResultRef(lazyAction.result);\n\n return { resultRef, trigger: lazyAction.trigger };\n}\n\n\n// === Vue Composables for Generators ===\n\n/**\n * Runs a generator function immediately and returns a ref to the AsyncResult representing the generator's state.\n * @param generatorFunc the generator function to run immediately\n * @returns a ref to the AsyncResult representing the generator's state\n */\nexport function useGenerator<T, P = unknown>(generatorFunc: (notifyProgress?: (progress: P) => void) => AsyncResultGenerator<T>): Ref<AsyncResult<T, any, P>> {\n const resultRef = useAsyncResultRef(AsyncResult.run(generatorFunc));\n return resultRef;\n}\n\n/**\n * Creates a lazy generator that can be triggered manually.\n * \n * @param generatorFunc the generator function to run when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyGenerator<T, P = unknown>(generatorFunc: (notifyProgress: (progress: P) => void) => AsyncResultGenerator<T>): { resultRef: Ref<AsyncResult<T, any, P>>, trigger: () => void } {\n const result = new AsyncResult<T, any, P>();\n const resultRef = useAsyncResultRef(result);\n\n const trigger = () => {\n result.runInPlace((notifyProgress) => generatorFunc(notifyProgress));\n }\n\n return { resultRef, trigger };\n}\n\n/**\n * Watches a source, gives it as inputs to the generator function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param generatorFunc the generator function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveGenerator<Inputs, T, E extends ErrorBase = ErrorBase, P = unknown>(source: WatchSource<Inputs>, generatorFunc: (args: Inputs, notifyProgress: (progress: P) => void) => AsyncResultGenerator<T>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E, P>> {\n const resultRef = useAsyncResultRef(new AsyncResult<T, E, P>());\n\n watch(source, (newInputs) => {\n resultRef.value.runInPlace((notifyProgress) => {\n return generatorFunc(newInputs, notifyProgress)\n });\n }, { immediate: options.immediate });\n\n return resultRef;\n}\n\n\n// === Vue Composables for AsyncResultCollection ===\n\n/**\n * Creates a reactive AsyncResultCollection wrapped in a Vue ref.\n * The AsyncResultCollection notifies Vue's reactivity system whenever its state changes.\n * \n * @template T - The type of the values in the AsyncResultCollection.\n * @template E - The type of the error, extending ErrorBase (default is ErrorBase).\n * @returns a ref to the AsyncResultCollection\n */\nexport function useAsyncResultCollection<T = any, E extends ErrorBase = ErrorBase>() {\n const list = new AsyncResultCollection<T, E>();\n const listRef = ref(list);\n\n list.listen(() => {\n triggerRef(listRef);\n });\n\n return listRef;\n}","import { defineComponent, watch, h, type VNode, type SlotsType } from \"vue\"\nimport { ErrorBase, type AsyncResult } from \"unwrapped/core\"\nimport { useAsyncResultRef } from \"../composables\"\n\ninterface CustomSlots<E extends ErrorBase = ErrorBase, P = unknown> {\n loading?: (props: { progress?: P }) => VNode;\n error?: (props: { error: E }) => VNode;\n idle?: () => VNode;\n}\n\n/**\n * Factory function to create a component that displays different content based on an AsyncResult's state. Provides slots for loading, error, idle, and success states and passes the relevant data to each slot, and are typed appropriately.\n * @param slots predefined slots for loading, error, and idle states. Useful for not having to repeat the same template for displaying a framework-specific spinner while in loading state, or a custom error message.\n * @param name Optional internal name for the component\n * @returns An instantiable component that accepts an AsyncResult prop and a default slot for the success state.\n * \n * @example\n * // loaders.ts - Create reusable loader with default loading/error UI\n * const MyLoader = makeAsyncResultLoader({\n * loading: () => h(Spinner),\n * error: ({ error }) => h(ErrorDisplay, { error }),\n * idle: () => h('div', 'Ready')\n * });\n * \n * // MyPage.vue - Use the loader with custom success content\n * <MyLoader :result=\"myAsyncResult\">\n * <template #default=\"{ value }\">\n * <UserProfile :user=\"value\" />\n * </template>\n * </MyLoader>\n */\nexport function makeAsyncResultLoader<T, E extends ErrorBase = ErrorBase, P = unknown>(slots: CustomSlots<E, P>, name = \"AsyncResultLoader\") {\n return defineComponent({\n name,\n props: {\n result: {\n type: Object as () => AsyncResult<T, E, P>,\n required: true\n }\n },\n slots: Object as SlotsType<CustomSlots<E, P> & { default: { value: T } }>,\n setup(props, context) {\n let resultRef = useAsyncResultRef(props.result);\n\n // Watch for prop changes & update listener\n watch(\n () => props.result,\n (newResult, oldResult) => {\n if (newResult === oldResult) return;\n resultRef = useAsyncResultRef(newResult);\n },\n { immediate: true }\n )\n\n return () => {\n const s = resultRef.value.state;\n\n const renderDefault = context.slots.default ?? (() => h(\"div\", { class: \"success\" }, \"Success\"));\n const renderError = context.slots.error ?? slots.error ?? (() => h(\"div\", { class: \"error\" }, \"Error\"));\n const renderLoading = context.slots.loading ?? slots.loading ?? (() => h(\"div\", { class: \"loading\" }, \"Loading…\"));\n const renderIdle = context.slots.idle ?? slots.idle ?? (() => h(\"div\", { class: \"idle\" }, \"Idle\"));\n\n // Choose what to render based on status\n switch (s.status) {\n case \"loading\":\n return renderLoading({ progress: s.progress });\n\n case \"error\":\n return renderError({ error: s.error });\n\n case \"success\":\n return renderDefault({ value: s.value });\n\n default:\n return renderIdle();\n }\n }\n }\n })\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAAgF;AAChF,kBAAuI;AAoBhI,SAAS,kBAAmE,aAAoC;AACnH,MAAI,CAAC,aAAa;AACd,kBAAc,IAAI,wBAAqB;AAAA,EAC3C;AACA,QAAM,YAAQ,gBAA0B,WAAW;AAEnD,QAAM,QAAQ,YAAY,OAAO,MAAM;AACnC,+BAAW,KAAK;AAAA,EACpB,CAAC;AAED,8BAAY,MAAM;AACd,UAAM;AAAA,EACV,CAAC;AAED,SAAO;AACX;AAOO,SAAS,6BAAiE,SAAgC;AAC7G,SAAO,kBAAkB,wBAAY,kBAAkB,OAAO,CAAC;AACnE;AAcO,SAAS,iBAA6D,QAA6B,MAAmC,UAAkC,EAAE,WAAW,KAAK,GAA2B;AACxN,QAAM,SAAS,IAAI,wBAAkB;AACrC,QAAM,YAAY,kBAAkB,MAAM;AAE1C,MAAI,QAA6B;AAEjC,wBAAM,QAAQ,CAAC,cAAc;AACzB,YAAQ;AACR,YAAQ,OAAO,OAAO,KAAK,SAAS,CAAC;AAAA,EACzC,GAAG,EAAE,WAAW,QAAQ,UAAU,CAAC;AAEnC,8BAAY,MAAM;AACd,YAAQ;AAAA,EACZ,CAAC;AAED,SAAO;AACX;AAqBO,SAAS,UAA2D,QAAoD;AAC3H,SAA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,SAAS;AAClB,UAAI,YAAY,kBAAkB,MAAM,MAAM;AAG9C;AAAA,QACI,MAAM,MAAM;AAAA,QACZ,CAAC,WAAW,cAAc;AACtB,cAAI,cAAc,UAAW;AAC7B,sBAAY,kBAAkB,SAAS;AAAA,QAC3C;AAAA,QACA,EAAE,WAAW,KAAK;AAAA,MACtB;AAEA,aAAO,MAAM;AACT,cAAM,IAAI,UAAU,MAAM;AAE1B,cAAM,gBAAgB,QAAQ,MAAM,YAAY,UAAM,eAAE,OAAO,EAAE,OAAO,UAAU,GAAG,SAAS;AAC9F,cAAM,cAAc,QAAQ,MAAM,SAAS,MAAM,UAAU,UAAM,eAAE,OAAO,EAAE,OAAO,QAAQ,GAAG,OAAO;AACrG,cAAM,gBAAgB,QAAQ,MAAM,WAAW,MAAM,YAAY,UAAM,eAAE,OAAO,EAAE,OAAO,UAAU,GAAG,eAAU;AAChH,cAAM,aAAa,QAAQ,MAAM,QAAQ,MAAM,SAAS,UAAM,eAAE,OAAO,EAAE,OAAO,OAAO,GAAG,MAAM;AAGhG,gBAAQ,EAAE,QAAQ;AAAA,UACd,KAAK;AACD,mBAAO,cAAc,EAAE,UAAU,EAAE,SAAS,CAAC;AAAA,UAEjD,KAAK;AACD,mBAAO,YAAY,EAAE,OAAO,EAAE,MAAM,CAAC;AAAA,UAEzC,KAAK;AACD,mBAAO,cAAc,EAAE,OAAO,EAAE,MAAM,CAAC;AAAA,UAE3C;AACI,mBAAO,WAAW;AAAA,QAC1B;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ,CAAC;AACL;","names":["import_vue","import_core"]}
|
package/dist/vue/index.mjs
CHANGED
|
@@ -53,7 +53,9 @@ function useLazyGenerator(generatorFunc) {
|
|
|
53
53
|
function useReactiveGenerator(source, generatorFunc, options = { immediate: true }) {
|
|
54
54
|
const resultRef = useAsyncResultRef(new AsyncResult());
|
|
55
55
|
watch(source, (newInputs) => {
|
|
56
|
-
resultRef.value.runInPlace((notifyProgress) =>
|
|
56
|
+
resultRef.value.runInPlace((notifyProgress) => {
|
|
57
|
+
return generatorFunc(newInputs, notifyProgress);
|
|
58
|
+
});
|
|
57
59
|
}, { immediate: options.immediate });
|
|
58
60
|
return resultRef;
|
|
59
61
|
}
|
package/dist/vue/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/vue/composables.ts","../../src/vue/components/asyncResultLoader.ts"],"sourcesContent":["import { onUnmounted, ref, triggerRef, watch, type Ref, type WatchSource } from \"vue\";\nimport { AsyncResult, AsyncResultCollection, ErrorBase, type Action, type AsyncResultGenerator, type FlatChainStep, type Result } from \"unwrapped/core\";\n\n\n// === Vue specific types ===\n\ninterface ReactiveProcessOptions {\n immediate: boolean;\n}\n\n\n\n// === Vue Composables for AsyncResult ===\n\n/**\n * Makes a ref to the given AsyncResult. Whenever the state of the AsyncResult changes,\n * the ref gets retriggered, making those changes visible to Vue's reactivity system.\n * \n * @param asyncResult the result to make reactive\n * @returns the ref to the result\n */\nexport function useAsyncResultRef<T, E extends ErrorBase = ErrorBase, P = unknown>(asyncResult?: AsyncResult<T, E, P>) {\n if (!asyncResult) {\n asyncResult = new AsyncResult<T, E, P>();\n }\n const state = ref<AsyncResult<T, E, P>>(asyncResult) as Ref<AsyncResult<T, E, P>>;\n\n const unsub = asyncResult.listen(() => {\n triggerRef(state);\n });\n\n onUnmounted(() => {\n unsub();\n });\n\n return state;\n}\n\n/**\n * Creates an AsyncResult ref from a promise returning a Result.\n * @param promise the promise returning a Result\n * @returns the ref to the AsyncResult\n */\nexport function useAsyncResultRefFromPromise<T, E extends ErrorBase = ErrorBase>(promise: Promise<Result<T, E>>) {\n return useAsyncResultRef(AsyncResult.fromResultPromise(promise));\n}\n\n\n\n// === Vue Composables for Chains ===\n\n/**\n * Watches a source, gives it as inputs to the function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param pipe the function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveChain<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, pipe: FlatChainStep<Inputs, T, E>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const result = new AsyncResult<T, E>();\n const resultRef = useAsyncResultRef(result);\n\n let unsub: (() => void) | null = null;\n\n watch(source, (newInputs) => {\n unsub?.();\n unsub = result.mirror(pipe(newInputs));\n }, { immediate: options.immediate });\n\n onUnmounted(() => {\n unsub?.();\n });\n\n return resultRef;\n}\n\n\n// === Vue Composables for Actions ===\n\n/**\n * The return type of useLazyAction.\n */\nexport interface LazyActionRef<T, E extends ErrorBase = ErrorBase, P = unknown> {\n resultRef: Ref<AsyncResult<T, E, P>>;\n trigger: () => void;\n}\n\n/**\n * Executes an action immediately and returns a ref to the AsyncResult representing the action's state.\n * \n * Same as useAsyncResultRefFromPromise(action()).\n * \n * @param action the action to execute immediately\n * @returns a ref to the AsyncResult representing the action's state\n */\nexport function useAction<T, E extends ErrorBase = ErrorBase, P = unknown>(action: Action<T, E, P>): Ref<AsyncResult<T, E, P>> {\n return useAsyncResultRef(AsyncResult.fromAction(action));\n}\n\n/**\n * Creates a lazy action that can be triggered manually.\n * \n * Same as AsyncResult.makeLazyAction(action), but the AsyncResult is wrapped in a ref for Vue reactivity.\n * \n * @param action the action to execute when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyAction<T, E extends ErrorBase = ErrorBase, P = unknown>(action: Action<T, E, P>): LazyActionRef<T, E, P> {\n const lazyAction = AsyncResult.makeLazyAction<T, E, P>(action);\n const resultRef = useAsyncResultRef(lazyAction.result);\n\n return { resultRef, trigger: lazyAction.trigger };\n}\n\n\n// === Vue Composables for Generators ===\n\n/**\n * Runs a generator function immediately and returns a ref to the AsyncResult representing the generator's state.\n * @param generatorFunc the generator function to run immediately\n * @returns a ref to the AsyncResult representing the generator's state\n */\nexport function useGenerator<T, P = unknown>(generatorFunc: (notifyProgress?: (progress: P) => void) => AsyncResultGenerator<T>): Ref<AsyncResult<T, any, P>> {\n const resultRef = useAsyncResultRef(AsyncResult.run(generatorFunc));\n return resultRef;\n}\n\n/**\n * Creates a lazy generator that can be triggered manually.\n * \n * @param generatorFunc the generator function to run when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyGenerator<T, P = unknown>(generatorFunc: (notifyProgress: (progress: P) => void) => AsyncResultGenerator<T>): { resultRef: Ref<AsyncResult<T, any, P>>, trigger: () => void } {\n const result = new AsyncResult<T, any, P>();\n const resultRef = useAsyncResultRef(result);\n\n const trigger = () => {\n result.runInPlace((notifyProgress) => generatorFunc(notifyProgress));\n }\n\n return { resultRef, trigger };\n}\n\n/**\n * Watches a source, gives it as inputs to the generator function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param generatorFunc the generator function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveGenerator<Inputs, T, E extends ErrorBase = ErrorBase, P = unknown>(source: WatchSource<Inputs>, generatorFunc: (args: Inputs, notifyProgress: (progress: P) => void) => AsyncResultGenerator<T>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E, P>> {\n const resultRef = useAsyncResultRef(new AsyncResult<T, E, P>());\n\n watch(source, (newInputs) => {\n resultRef.value.runInPlace((notifyProgress) => generatorFunc(newInputs, notifyProgress));\n }, { immediate: options.immediate });\n\n return resultRef;\n}\n\n\n// === Vue Composables for AsyncResultCollection ===\n\n/**\n * Creates a reactive AsyncResultCollection wrapped in a Vue ref.\n * The AsyncResultCollection notifies Vue's reactivity system whenever its state changes.\n * \n * @template T - The type of the values in the AsyncResultCollection.\n * @template E - The type of the error, extending ErrorBase (default is ErrorBase).\n * @returns a ref to the AsyncResultCollection\n */\nexport function useAsyncResultCollection<T = any, E extends ErrorBase = ErrorBase>() {\n const list = new AsyncResultCollection<T, E>();\n const listRef = ref(list);\n\n list.listen(() => {\n triggerRef(listRef);\n });\n\n return listRef;\n}","import { defineComponent, watch, h, type VNode, type SlotsType } from \"vue\"\nimport { ErrorBase, type AsyncResult } from \"unwrapped/core\"\nimport { useAsyncResultRef } from \"../composables\"\n\ninterface CustomSlots<E extends ErrorBase = ErrorBase, P = unknown> {\n loading?: (props: { progress?: P }) => VNode;\n error?: (props: { error: E }) => VNode;\n idle?: () => VNode;\n}\n\n/**\n * Factory function to create a component that displays different content based on an AsyncResult's state. Provides slots for loading, error, idle, and success states and passes the relevant data to each slot, and are typed appropriately.\n * @param slots predefined slots for loading, error, and idle states. Useful for not having to repeat the same template for displaying a framework-specific spinner while in loading state, or a custom error message.\n * @param name Optional internal name for the component\n * @returns An instantiable component that accepts an AsyncResult prop and a default slot for the success state.\n * \n * @example\n * // loaders.ts - Create reusable loader with default loading/error UI\n * const MyLoader = makeAsyncResultLoader({\n * loading: () => h(Spinner),\n * error: ({ error }) => h(ErrorDisplay, { error }),\n * idle: () => h('div', 'Ready')\n * });\n * \n * // MyPage.vue - Use the loader with custom success content\n * <MyLoader :result=\"myAsyncResult\">\n * <template #default=\"{ value }\">\n * <UserProfile :user=\"value\" />\n * </template>\n * </MyLoader>\n */\nexport function makeAsyncResultLoader<T, E extends ErrorBase = ErrorBase, P = unknown>(slots: CustomSlots<E, P>, name = \"AsyncResultLoader\") {\n return defineComponent({\n name,\n props: {\n result: {\n type: Object as () => AsyncResult<T, E, P>,\n required: true\n }\n },\n slots: Object as SlotsType<CustomSlots<E, P> & { default: { value: T } }>,\n setup(props, context) {\n let resultRef = useAsyncResultRef(props.result);\n\n // Watch for prop changes & update listener\n watch(\n () => props.result,\n (newResult, oldResult) => {\n if (newResult === oldResult) return;\n resultRef = useAsyncResultRef(newResult);\n },\n { immediate: true }\n )\n\n return () => {\n const s = resultRef.value.state;\n\n const renderDefault = context.slots.default ?? (() => h(\"div\", { class: \"success\" }, \"Success\"));\n const renderError = context.slots.error ?? slots.error ?? (() => h(\"div\", { class: \"error\" }, \"Error\"));\n const renderLoading = context.slots.loading ?? slots.loading ?? (() => h(\"div\", { class: \"loading\" }, \"Loading…\"));\n const renderIdle = context.slots.idle ?? slots.idle ?? (() => h(\"div\", { class: \"idle\" }, \"Idle\"));\n\n // Choose what to render based on status\n switch (s.status) {\n case \"loading\":\n return renderLoading({ progress: s.progress });\n\n case \"error\":\n return renderError({ error: s.error });\n\n case \"success\":\n return renderDefault({ value: s.value });\n\n default:\n return renderIdle();\n }\n }\n }\n })\n}"],"mappings":";AAAA,SAAS,aAAa,KAAK,YAAY,aAAyC;AAChF,SAAS,aAAa,6BAAiH;AAoBhI,SAAS,kBAAmE,aAAoC;AACnH,MAAI,CAAC,aAAa;AACd,kBAAc,IAAI,YAAqB;AAAA,EAC3C;AACA,QAAM,QAAQ,IAA0B,WAAW;AAEnD,QAAM,QAAQ,YAAY,OAAO,MAAM;AACnC,eAAW,KAAK;AAAA,EACpB,CAAC;AAED,cAAY,MAAM;AACd,UAAM;AAAA,EACV,CAAC;AAED,SAAO;AACX;AAOO,SAAS,6BAAiE,SAAgC;AAC7G,SAAO,kBAAkB,YAAY,kBAAkB,OAAO,CAAC;AACnE;AAcO,SAAS,iBAA6D,QAA6B,MAAmC,UAAkC,EAAE,WAAW,KAAK,GAA2B;AACxN,QAAM,SAAS,IAAI,YAAkB;AACrC,QAAM,YAAY,kBAAkB,MAAM;AAE1C,MAAI,QAA6B;AAEjC,QAAM,QAAQ,CAAC,cAAc;AACzB,YAAQ;AACR,YAAQ,OAAO,OAAO,KAAK,SAAS,CAAC;AAAA,EACzC,GAAG,EAAE,WAAW,QAAQ,UAAU,CAAC;AAEnC,cAAY,MAAM;AACd,YAAQ;AAAA,EACZ,CAAC;AAED,SAAO;AACX;AAqBO,SAAS,UAA2D,QAAoD;AAC3H,SAAO,kBAAkB,YAAY,WAAW,MAAM,CAAC;AAC3D;AAUO,SAAS,cAA+D,QAAiD;AAC5H,QAAM,aAAa,YAAY,eAAwB,MAAM;AAC7D,QAAM,YAAY,kBAAkB,WAAW,MAAM;AAErD,SAAO,EAAE,WAA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A,EAAE,WAAW,KAAK;AAAA,MACtB;AAEA,aAAO,MAAM;AACT,cAAM,IAAI,UAAU,MAAM;AAE1B,cAAM,gBAAgB,QAAQ,MAAM,YAAY,MAAM,EAAE,OAAO,EAAE,OAAO,UAAU,GAAG,SAAS;AAC9F,cAAM,cAAc,QAAQ,MAAM,SAAS,MAAM,UAAU,MAAM,EAAE,OAAO,EAAE,OAAO,QAAQ,GAAG,OAAO;AACrG,cAAM,gBAAgB,QAAQ,MAAM,WAAW,MAAM,YAAY,MAAM,EAAE,OAAO,EAAE,OAAO,UAAU,GAAG,eAAU;AAChH,cAAM,aAAa,QAAQ,MAAM,QAAQ,MAAM,SAAS,MAAM,EAAE,OAAO,EAAE,OAAO,OAAO,GAAG,MAAM;AAGhG,gBAAQ,EAAE,QAAQ;AAAA,UACd,KAAK;AACD,mBAAO,cAAc,EAAE,UAAU,EAAE,SAAS,CAAC;AAAA,UAEjD,KAAK;AACD,mBAAO,YAAY,EAAE,OAAO,EAAE,MAAM,CAAC;AAAA,UAEzC,KAAK;AACD,mBAAO,cAAc,EAAE,OAAO,EAAE,MAAM,CAAC;AAAA,UAE3C;AACI,mBAAO,WAAW;AAAA,QAC1B;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ,CAAC;AACL;","names":["watch","watch"]}
|
|
1
|
+
{"version":3,"sources":["../../src/vue/composables.ts","../../src/vue/components/asyncResultLoader.ts"],"sourcesContent":["import { onUnmounted, ref, triggerRef, watch, type Ref, type WatchSource } from \"vue\";\nimport { AsyncResult, AsyncResultCollection, ErrorBase, type Action, type AsyncResultGenerator, type FlatChainStep, type Result } from \"unwrapped/core\";\n\n\n// === Vue specific types ===\n\ninterface ReactiveProcessOptions {\n immediate: boolean;\n}\n\n\n\n// === Vue Composables for AsyncResult ===\n\n/**\n * Makes a ref to the given AsyncResult. Whenever the state of the AsyncResult changes,\n * the ref gets retriggered, making those changes visible to Vue's reactivity system.\n * \n * @param asyncResult the result to make reactive\n * @returns the ref to the result\n */\nexport function useAsyncResultRef<T, E extends ErrorBase = ErrorBase, P = unknown>(asyncResult?: AsyncResult<T, E, P>) {\n if (!asyncResult) {\n asyncResult = new AsyncResult<T, E, P>();\n }\n const state = ref<AsyncResult<T, E, P>>(asyncResult) as Ref<AsyncResult<T, E, P>>;\n\n const unsub = asyncResult.listen(() => {\n triggerRef(state);\n });\n\n onUnmounted(() => {\n unsub();\n });\n\n return state;\n}\n\n/**\n * Creates an AsyncResult ref from a promise returning a Result.\n * @param promise the promise returning a Result\n * @returns the ref to the AsyncResult\n */\nexport function useAsyncResultRefFromPromise<T, E extends ErrorBase = ErrorBase>(promise: Promise<Result<T, E>>) {\n return useAsyncResultRef(AsyncResult.fromResultPromise(promise));\n}\n\n\n\n// === Vue Composables for Chains ===\n\n/**\n * Watches a source, gives it as inputs to the function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param pipe the function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveChain<Inputs, T, E extends ErrorBase = ErrorBase>(source: WatchSource<Inputs>, pipe: FlatChainStep<Inputs, T, E>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E>> {\n const result = new AsyncResult<T, E>();\n const resultRef = useAsyncResultRef(result);\n\n let unsub: (() => void) | null = null;\n\n watch(source, (newInputs) => {\n unsub?.();\n unsub = result.mirror(pipe(newInputs));\n }, { immediate: options.immediate });\n\n onUnmounted(() => {\n unsub?.();\n });\n\n return resultRef;\n}\n\n\n// === Vue Composables for Actions ===\n\n/**\n * The return type of useLazyAction.\n */\nexport interface LazyActionRef<T, E extends ErrorBase = ErrorBase, P = unknown> {\n resultRef: Ref<AsyncResult<T, E, P>>;\n trigger: () => void;\n}\n\n/**\n * Executes an action immediately and returns a ref to the AsyncResult representing the action's state.\n * \n * Same as useAsyncResultRefFromPromise(action()).\n * \n * @param action the action to execute immediately\n * @returns a ref to the AsyncResult representing the action's state\n */\nexport function useAction<T, E extends ErrorBase = ErrorBase, P = unknown>(action: Action<T, E, P>): Ref<AsyncResult<T, E, P>> {\n return useAsyncResultRef(AsyncResult.fromAction(action));\n}\n\n/**\n * Creates a lazy action that can be triggered manually.\n * \n * Same as AsyncResult.makeLazyAction(action), but the AsyncResult is wrapped in a ref for Vue reactivity.\n * \n * @param action the action to execute when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyAction<T, E extends ErrorBase = ErrorBase, P = unknown>(action: Action<T, E, P>): LazyActionRef<T, E, P> {\n const lazyAction = AsyncResult.makeLazyAction<T, E, P>(action);\n const resultRef = useAsyncResultRef(lazyAction.result);\n\n return { resultRef, trigger: lazyAction.trigger };\n}\n\n\n// === Vue Composables for Generators ===\n\n/**\n * Runs a generator function immediately and returns a ref to the AsyncResult representing the generator's state.\n * @param generatorFunc the generator function to run immediately\n * @returns a ref to the AsyncResult representing the generator's state\n */\nexport function useGenerator<T, P = unknown>(generatorFunc: (notifyProgress?: (progress: P) => void) => AsyncResultGenerator<T>): Ref<AsyncResult<T, any, P>> {\n const resultRef = useAsyncResultRef(AsyncResult.run(generatorFunc));\n return resultRef;\n}\n\n/**\n * Creates a lazy generator that can be triggered manually.\n * \n * @param generatorFunc the generator function to run when triggered\n * @returns an object containing a ref to the AsyncResult and a trigger function\n */\nexport function useLazyGenerator<T, P = unknown>(generatorFunc: (notifyProgress: (progress: P) => void) => AsyncResultGenerator<T>): { resultRef: Ref<AsyncResult<T, any, P>>, trigger: () => void } {\n const result = new AsyncResult<T, any, P>();\n const resultRef = useAsyncResultRef(result);\n\n const trigger = () => {\n result.runInPlace((notifyProgress) => generatorFunc(notifyProgress));\n }\n\n return { resultRef, trigger };\n}\n\n/**\n * Watches a source, gives it as inputs to the generator function provided, and updates the result contained in the ref accordingly.\n * \n * @param source the inputs to react to\n * @param generatorFunc the generator function to run when the inputs change\n * @param options optional settings\n * @returns ref to the result\n */\nexport function useReactiveGenerator<Inputs, T, E extends ErrorBase = ErrorBase, P = unknown>(source: WatchSource<Inputs>, generatorFunc: (args: Inputs, notifyProgress: (progress: P) => void) => AsyncResultGenerator<T>, options: ReactiveProcessOptions = { immediate: true }): Ref<AsyncResult<T, E, P>> {\n const resultRef = useAsyncResultRef(new AsyncResult<T, E, P>());\n\n watch(source, (newInputs) => {\n resultRef.value.runInPlace((notifyProgress) => {\n return generatorFunc(newInputs, notifyProgress)\n });\n }, { immediate: options.immediate });\n\n return resultRef;\n}\n\n\n// === Vue Composables for AsyncResultCollection ===\n\n/**\n * Creates a reactive AsyncResultCollection wrapped in a Vue ref.\n * The AsyncResultCollection notifies Vue's reactivity system whenever its state changes.\n * \n * @template T - The type of the values in the AsyncResultCollection.\n * @template E - The type of the error, extending ErrorBase (default is ErrorBase).\n * @returns a ref to the AsyncResultCollection\n */\nexport function useAsyncResultCollection<T = any, E extends ErrorBase = ErrorBase>() {\n const list = new AsyncResultCollection<T, E>();\n const listRef = ref(list);\n\n list.listen(() => {\n triggerRef(listRef);\n });\n\n return listRef;\n}","import { defineComponent, watch, h, type VNode, type SlotsType } from \"vue\"\nimport { ErrorBase, type AsyncResult } from \"unwrapped/core\"\nimport { useAsyncResultRef } from \"../composables\"\n\ninterface CustomSlots<E extends ErrorBase = ErrorBase, P = unknown> {\n loading?: (props: { progress?: P }) => VNode;\n error?: (props: { error: E }) => VNode;\n idle?: () => VNode;\n}\n\n/**\n * Factory function to create a component that displays different content based on an AsyncResult's state. Provides slots for loading, error, idle, and success states and passes the relevant data to each slot, and are typed appropriately.\n * @param slots predefined slots for loading, error, and idle states. Useful for not having to repeat the same template for displaying a framework-specific spinner while in loading state, or a custom error message.\n * @param name Optional internal name for the component\n * @returns An instantiable component that accepts an AsyncResult prop and a default slot for the success state.\n * \n * @example\n * // loaders.ts - Create reusable loader with default loading/error UI\n * const MyLoader = makeAsyncResultLoader({\n * loading: () => h(Spinner),\n * error: ({ error }) => h(ErrorDisplay, { error }),\n * idle: () => h('div', 'Ready')\n * });\n * \n * // MyPage.vue - Use the loader with custom success content\n * <MyLoader :result=\"myAsyncResult\">\n * <template #default=\"{ value }\">\n * <UserProfile :user=\"value\" />\n * </template>\n * </MyLoader>\n */\nexport function makeAsyncResultLoader<T, E extends ErrorBase = ErrorBase, P = unknown>(slots: CustomSlots<E, P>, name = \"AsyncResultLoader\") {\n return defineComponent({\n name,\n props: {\n result: {\n type: Object as () => AsyncResult<T, E, P>,\n required: true\n }\n },\n slots: Object as SlotsType<CustomSlots<E, P> & { default: { value: T } }>,\n setup(props, context) {\n let resultRef = useAsyncResultRef(props.result);\n\n // Watch for prop changes & update listener\n watch(\n () => props.result,\n (newResult, oldResult) => {\n if (newResult === oldResult) return;\n resultRef = useAsyncResultRef(newResult);\n },\n { immediate: true }\n )\n\n return () => {\n const s = resultRef.value.state;\n\n const renderDefault = context.slots.default ?? (() => h(\"div\", { class: \"success\" }, \"Success\"));\n const renderError = context.slots.error ?? slots.error ?? (() => h(\"div\", { class: \"error\" }, \"Error\"));\n const renderLoading = context.slots.loading ?? slots.loading ?? (() => h(\"div\", { class: \"loading\" }, \"Loading…\"));\n const renderIdle = context.slots.idle ?? slots.idle ?? (() => h(\"div\", { class: \"idle\" }, \"Idle\"));\n\n // Choose what to render based on status\n switch (s.status) {\n case \"loading\":\n return renderLoading({ progress: s.progress });\n\n case \"error\":\n return renderError({ error: s.error });\n\n case \"success\":\n return renderDefault({ value: s.value });\n\n default:\n return renderIdle();\n }\n }\n }\n })\n}"],"mappings":";AAAA,SAAS,aAAa,KAAK,YAAY,aAAyC;AAChF,SAAS,aAAa,6BAAiH;AAoBhI,SAAS,kBAAmE,aAAoC;AACnH,MAAI,CAAC,aAAa;AACd,kBAAc,IAAI,YAAqB;AAAA,EAC3C;AACA,QAAM,QAAQ,IAA0B,WAAW;AAEnD,QAAM,QAAQ,YAAY,OAAO,MAAM;AACnC,eAAW,KAAK;AAAA,EACpB,CAAC;AAED,cAAY,MAAM;AACd,UAAM;AAAA,EACV,CAAC;AAED,SAAO;AACX;AAOO,SAAS,6BAAiE,SAA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