|
| 1 | +import { createReducer } from '@data-client/core'; |
| 2 | +import type { State } from '@data-client/core'; |
| 3 | +import type { Middleware as GenericMiddleware } from '@data-client/use-enhanced-reducer'; |
| 4 | +import { |
| 5 | + defineComponent, |
| 6 | + h, |
| 7 | + onMounted, |
| 8 | + onUnmounted, |
| 9 | + provide, |
| 10 | + shallowRef, |
| 11 | +} from 'vue'; |
| 12 | + |
| 13 | +import { ControllerKey, StateKey } from '../context.js'; |
| 14 | + |
| 15 | +export interface StoreProps { |
| 16 | + mgrEffect: () => void; |
| 17 | + middlewares: GenericMiddleware[]; |
| 18 | + initialState: State<unknown>; |
| 19 | + controller: any; // Controller |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Vue counterpart to React DataStore: owns reducer state and exposes it via provide/inject. |
| 24 | + * Expects props to be referentially stable after mount. |
| 25 | + */ |
| 26 | +export default defineComponent<StoreProps>({ |
| 27 | + name: 'DataStore', |
| 28 | + props: ['mgrEffect', 'middlewares', 'initialState', 'controller'] as any, |
| 29 | + setup(props, { slots }) { |
| 30 | + // Create reducer bound to controller |
| 31 | + const masterReducer = createReducer(props.controller); |
| 32 | + |
| 33 | + // state ref holds current committed state; updates trigger reactive recompute |
| 34 | + const stateRef = shallowRef<State<unknown>>(props.initialState); |
| 35 | + |
| 36 | + // Build a redux-like dispatch chain with middlewares using controller.bindMiddleware |
| 37 | + // We emulate the use-enhanced-reducer behavior: dispatch returns a Promise that resolves when committed |
| 38 | + let resolveNext: (() => void) | null = null; |
| 39 | + const waitForCommit = () => |
| 40 | + new Promise<void>(resolve => { |
| 41 | + resolveNext = resolve; |
| 42 | + }); |
| 43 | + |
| 44 | + const realDispatch = async (action: any) => { |
| 45 | + // compute next state synchronously via reducer |
| 46 | + const next = masterReducer(stateRef.value, action); |
| 47 | + stateRef.value = next; |
| 48 | + // resolve pending promise after state commit microtask |
| 49 | + if (resolveNext) { |
| 50 | + const r = resolveNext; |
| 51 | + resolveNext = null; |
| 52 | + r(); |
| 53 | + } |
| 54 | + return Promise.resolve(); |
| 55 | + }; |
| 56 | + |
| 57 | + const getState = () => stateRef.value; |
| 58 | + |
| 59 | + // compose middlewares |
| 60 | + const chain = props.middlewares.map(mw => |
| 61 | + mw({ |
| 62 | + getState, |
| 63 | + dispatch: (a: any) => outerDispatch(a), |
| 64 | + } as any), |
| 65 | + ); |
| 66 | + const compose = (fns: ((arg: any) => any)[]) => (initial: any) => |
| 67 | + fns.reduceRight((v, f) => f(v), initial); |
| 68 | + const outerDispatch = compose(chain)(async (action: any) => { |
| 69 | + const promise = waitForCommit(); |
| 70 | + await realDispatch(action); |
| 71 | + return promise; |
| 72 | + }); |
| 73 | + |
| 74 | + // Bind controller with middleware API |
| 75 | + props.controller.bindMiddleware({ |
| 76 | + getState, |
| 77 | + dispatch: (a: any) => outerDispatch(a), |
| 78 | + } as any); |
| 79 | + |
| 80 | + // provide state and controller |
| 81 | + provide(StateKey, stateRef); |
| 82 | + provide(ControllerKey, props.controller); |
| 83 | + |
| 84 | + // run managers' init/cleanup after mount via mgrEffect() |
| 85 | + let cleanup: void | (() => void); |
| 86 | + onMounted(() => { |
| 87 | + cleanup = props.mgrEffect(); |
| 88 | + }); |
| 89 | + onUnmounted(() => { |
| 90 | + if (cleanup) cleanup(); |
| 91 | + }); |
| 92 | + |
| 93 | + return () => (slots.default ? slots.default() : h('div')); |
| 94 | + }, |
| 95 | +}); |
0 commit comments