@rxova/journey-react 1.0.0-rc.1 → 1.0.0-rc.2
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/README.md +12 -4
- package/dist/client.cjs +1 -1
- package/dist/client.cjs.map +3 -3
- package/dist/client.js +1 -1
- package/dist/client.js.map +3 -3
- package/dist/createJourney.d.cts +4 -4
- package/dist/createJourney.d.ts +4 -4
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +3 -3
- package/dist/provider.d.cts +1 -1
- package/dist/provider.d.ts +1 -1
- package/dist/runtime-hooks.d.cts +4 -3
- package/dist/runtime-hooks.d.ts +4 -3
- package/dist/types.d.cts +34 -12
- package/dist/types.d.ts +34 -12
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Typed React bindings for multi-step UI flows.
|
|
|
6
6
|
<a href="https://www.npmjs.com/package/@rxova/journey-react">
|
|
7
7
|
<img src="https://img.shields.io/npm/v/@rxova/journey-react?color=0f8f6a" alt="npm" />
|
|
8
8
|
</a>
|
|
9
|
-
<img src="https://img.shields.io/badge/
|
|
9
|
+
<img src="https://img.shields.io/badge/1.33%20kB-brotli-0f8f6a" alt="size" />
|
|
10
10
|
<img src="https://img.shields.io/badge/React%2018+-black" alt="React 18+" />
|
|
11
11
|
<img src="https://img.shields.io/badge/TypeScript-strict-3178C6?logo=typescript&logoColor=white" alt="TypeScript" />
|
|
12
12
|
</p>
|
|
@@ -74,7 +74,8 @@ export const App = () => (
|
|
|
74
74
|
`createJourney()` returns a runtime with bound hooks:
|
|
75
75
|
|
|
76
76
|
- **`useJourneySnapshot()`** — full snapshot: `currentStepId`, `context`, `history`, `status`, `async`
|
|
77
|
-
- **`useJourneyApi()`** — runtime commands: `
|
|
77
|
+
- **`useJourneyApi()`** — runtime commands: `startJourney`, `goToNextStep`, `goToPreviousStep`, `completeJourney`, `send`, etc.
|
|
78
|
+
- **`useStepApi(stepId)`** — step-scoped command surface with `send(...)` narrowed to custom events handled by that step or `global`
|
|
78
79
|
- **`useJourneyComputed()`** — derived state: `mode`, `activeStepId`, `isLoading`, `isFirstStep`, `isLastStep`
|
|
79
80
|
- **`useJourneySelector(selector, eq?)`** — subscribe to a slice of the snapshot
|
|
80
81
|
- **`useJourneyEvent(listener)`** — stream lifecycle events for analytics
|
|
@@ -84,7 +85,7 @@ export const App = () => (
|
|
|
84
85
|
```ts
|
|
85
86
|
const api = signup.useJourneyApi();
|
|
86
87
|
|
|
87
|
-
await api.
|
|
88
|
+
await api.startJourney();
|
|
88
89
|
await api.goToNextStep();
|
|
89
90
|
await api.goToPreviousStep();
|
|
90
91
|
await api.goToLastVisitedStep();
|
|
@@ -98,6 +99,13 @@ api.resetJourney();
|
|
|
98
99
|
|
|
99
100
|
Transition failures resolve through `result.error` instead of rejecting, so `void api.goToNextStep()` is safe from unhandled promise rejections.
|
|
100
101
|
|
|
102
|
+
For step components, `useStepApi(stepId)` returns the same commands but narrows `send(...)` to custom events handled by that step or by `global` transitions:
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
const api = signup.useStepApi("start");
|
|
106
|
+
void api.send({ type: "submit" });
|
|
107
|
+
```
|
|
108
|
+
|
|
101
109
|
## Custom Step Renderer
|
|
102
110
|
|
|
103
111
|
`StepRenderer` is a convenience — it just looks up the current step's view from the `views` record and renders it. You can build your own if you need transitions, animations, or a different rendering strategy:
|
|
@@ -128,7 +136,7 @@ Each `createJourney()` call creates one machine instance. The returned hooks are
|
|
|
128
136
|
|
|
129
137
|
- Rendering multiple providers from the same runtime shares one journey state
|
|
130
138
|
- `JourneyProvider` auto-starts an `idled` runtime, but does not dispose it by default
|
|
131
|
-
- Provider-free flows can start manually through `useJourneyApi().
|
|
139
|
+
- Provider-free flows can start manually through `useJourneyApi().startJourney()` or `machine.startJourney()`
|
|
132
140
|
- Provider-owned startup failures are reported through `onError(error, { phase: "start" })`
|
|
133
141
|
- Set `disposeOnUnmount` when a provider fully owns a component-scoped runtime
|
|
134
142
|
- Independent instances require separate `createJourney()` calls
|
package/dist/client.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";"use client";var
|
|
1
|
+
"use strict";"use client";var w=Object.create;var x=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var H=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var j=(n,r)=>{for(var s in r)x(n,s,{get:r[s],enumerable:!0})},M=(n,r,s,y)=>{if(r&&typeof r=="object"||typeof r=="function")for(let c of F(r))!D.call(n,c)&&c!==s&&x(n,c,{get:()=>r[c],enumerable:!(y=k(r,c))||y.enumerable});return n};var E=(n,r,s)=>(s=n!=null?w(H(n)):{},M(r||!n||!n.__esModule?x(s,"default",{value:n,enumerable:!0}):s,n)),A=n=>M(x({},"__esModule",{value:!0}),n);var B={};j(B,{createJourney:()=>b,createJourneyFactory:()=>I});module.exports=A(B);var C=require("@rxova/journey-core");var d=E(require("react"),1),l=require("react/jsx-runtime"),m=typeof window>"u"?d.default.useEffect:d.default.useLayoutEffect,L=(n,r,s)=>{if(s){s(n,r);return}console.error(`JourneyProvider ${r.phase} failed.`,n)},h=(n,r)=>{let s=d.default.createContext(null),y=(a="hook")=>{let e=d.default.useContext(s);if(!e)throw new Error(`${a} must be used within JourneyProvider.`);return e},c=({runtimeMachine:a,onError:e,disposeOnUnmount:t})=>{let o=d.default.useRef(e),u=d.default.useRef(null);o.current=e,m(()=>{u.current!==null&&(globalThis.clearTimeout(u.current),u.current=null)});let J=d.default.useCallback(i=>i.status,[a]),S=d.default.useCallback(i=>a.subscribeSelector(J,()=>{i()}),[a,J]),v=d.default.useCallback(()=>a.getSnapshot().status,[a]),T=d.default.useSyncExternalStore(S,v,v);return m(()=>{T==="idled"&&a.startJourney().catch(i=>{L(i,{phase:"start"},o.current)})},[a,T]),m(()=>{if(t)return()=>{u.current=globalThis.setTimeout(()=>{u.current=null,a.dispose()},0)}},[a,t]),null};return{JourneyProvider:({views:a,onError:e,disposeOnUnmount:t=!1,children:o})=>{let u=n;return(0,l.jsxs)(s.Provider,{value:a,children:[o,(0,l.jsx)(c,{runtimeMachine:u,onError:e,disposeOnUnmount:t})]})},StepRenderer:({fallback:a=null})=>{let e=r(J=>J.currentStepId),o=y("StepRenderer")[e];if(!o)return(0,l.jsx)(l.Fragment,{children:a});let u=o;return(0,l.jsx)(d.default.Fragment,{children:(0,l.jsx)(u,{})},e)}}};var p=E(require("react"),1),q=typeof window>"u"?p.default.useEffect:p.default.useLayoutEffect,P=n=>{let r=()=>{let e=n,t=p.default.useCallback(()=>e.getSnapshot(),[e]),o=p.default.useCallback(u=>e.subscribe(u),[e]);return p.default.useSyncExternalStore(o,t,t)},s=()=>{let e=r(),t=n;return p.default.useMemo(()=>t.getComputed(),[t,e])},y=(e,t)=>{let o=n,u=t??Object.is,J=p.default.useRef(null),S=p.default.useCallback(()=>{let T=o.getSnapshot(),i=J.current;if(i&&Object.is(i.machine,o)&&Object.is(i.selector,e)&&Object.is(i.isEqual,u)&&Object.is(i.snapshot,T))return i.selected;let f=e(T);return i&&Object.is(i.machine,o)&&Object.is(i.selector,e)&&Object.is(i.isEqual,u)&&u(i.selected,f)?(J.current={machine:o,snapshot:T,selected:i.selected,selector:e,isEqual:u},i.selected):(J.current={machine:o,snapshot:T,selected:f,selector:e,isEqual:u},f)},[o,u,e]),v=p.default.useCallback(T=>o.subscribeSelector(e,()=>{T()},u),[o,u,e]);return p.default.useSyncExternalStore(v,S,S)},c=e=>{let t=n,o=p.default.useRef(e);o.current=e,q(()=>t.subscribeEvent(u=>{o.current(u)}),[t])};return{useJourneySnapshot:r,useJourneyComputed:s,useJourneySelector:y,useJourneyApi:()=>{let e=n;return p.default.useMemo(()=>({startJourney:e.startJourney,send:e.send,goToNextStep:e.goToNextStep,goToStepById:e.goToStepById,terminateJourney:e.terminateJourney,completeJourney:e.completeJourney,goToPreviousStep:e.goToPreviousStep,goToLastVisitedStep:e.goToLastVisitedStep,clearStepError:e.clearStepError,updateContext:e.updateContext,getStepMeta:e.getStepMeta,resetJourney:()=>e.resetJourney()}),[e])},useStepApi:e=>{let t=n;return p.default.useMemo(()=>({startJourney:t.startJourney,send:t.send,goToNextStep:t.goToNextStep,goToStepById:t.goToStepById,terminateJourney:t.terminateJourney,completeJourney:t.completeJourney,goToPreviousStep:t.goToPreviousStep,goToLastVisitedStep:t.goToLastVisitedStep,clearStepError:t.clearStepError,updateContext:t.updateContext,getStepMeta:t.getStepMeta,resetJourney:()=>t.resetJourney()}),[t])},useJourneyEvent:c,useJourneyStepLifecycle:(e,t)=>{c(o=>{o.type==="step.enter"&&o.stepId===e?t.onEnter?.({context:n.getSnapshot().context}):o.type==="step.exit"&&o.stepId===e&&t.onLeave?.({context:n.getSnapshot().context})})}}};var R=(n,r)=>{let y=(0,C.createJourneyMachine)(n,r),c=P(y),g=h(y,c.useJourneySelector);return{machine:y,dispose:()=>y.dispose(),...c,...g}};function b(n,r){return R(n,r)}function I(n,r){let s=n;return()=>R(s,r)}0&&(module.exports={createJourney,createJourneyFactory});
|
|
2
2
|
//# sourceMappingURL=client.cjs.map
|
package/dist/client.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/client.ts", "../src/createJourney.tsx", "../src/provider.tsx", "../src/runtime-hooks.tsx"],
|
|
4
|
-
"sourcesContent": ["\"use client\";\n\nexport * from \"./index\";\n", "import { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyDefinition,\n JourneyJsonObject,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins\n} from \"@rxova/journey-core\";\nimport { createJourneyProviderArtifacts } from \"./provider\";\nimport { createJourneyHooks } from \"./runtime-hooks\";\nimport type { JourneyRuntime, JourneyRuntimeFactory } from \"./types\";\n\ntype JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<\n TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins\n>;\n\n/**\n * Creates a journey machine and returns React hooks/components bound to that machine.\n * Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.\n */\nexport const createJourney = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n const machineOptions = options as JourneyMachineOptions<TPlugins> | undefined;\n const machine = createJourneyMachine<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(definition, machineOptions) as JourneyMachineWithPlugins<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >;\n const hooks = createJourneyHooks<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n machine\n );\n const providerArtifacts = createJourneyProviderArtifacts<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(machine, hooks.useJourneySelector);\n\n return {\n machine,\n dispose: () => machine.dispose(),\n ...hooks,\n ...providerArtifacts\n };\n};\n\n/**\n * Creates a typed factory for producing fresh React-bound journey runtimes.\n * Use this when a component or route boundary needs independent instances\n * from the same definition/options pair.\n */\nexport const createJourneyFactory = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactory<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n return () =>\n createJourney<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n definition,\n options\n );\n};\n", "import React from \"react\";\n\nimport type {\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneySelector,\n JourneyMachineWithPlugins,\n JourneyMachinePlugin\n} from \"@rxova/journey-core\";\nimport type { JourneyProviderErrorContext, JourneyProviderProps, JourneyViews } from \"./types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nconst reportProviderError = (\n error: unknown,\n context: JourneyProviderErrorContext,\n listener?: ((error: unknown, context: JourneyProviderErrorContext) => void) | undefined\n) => {\n if (listener) {\n listener(error, context);\n return;\n }\n\n console.error(`JourneyProvider ${context.phase} failed.`, error);\n};\n\nexport const createJourneyProviderArtifacts = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>,\n useJourneySelector: <TSelected>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ) => TSelected\n) => {\n const ViewsContext = React.createContext<JourneyViews<TStepId> | null>(null);\n\n const useJourneyViews = (hookName = \"hook\") => {\n const views = React.useContext(ViewsContext);\n if (!views) {\n throw new Error(`${hookName} must be used within JourneyProvider.`);\n }\n return views;\n };\n\n const ProviderController = ({\n runtimeMachine,\n onStart,\n onComplete,\n onTerminate,\n onError,\n disposeOnUnmount\n }: {\n runtimeMachine: typeof machine;\n onStart: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onStart\"] | undefined;\n onComplete: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onComplete\"] | undefined;\n onTerminate: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onTerminate\"] | undefined;\n onError: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onError\"] | undefined;\n disposeOnUnmount: boolean;\n }) => {\n const onStartRef = React.useRef(onStart);\n const onCompleteRef = React.useRef(onComplete);\n const onTerminateRef = React.useRef(onTerminate);\n const onErrorRef = React.useRef(onError);\n const scheduledDisposeRef = React.useRef<ReturnType<typeof globalThis.setTimeout> | null>(null);\n const hasOnStart = onStart !== undefined;\n const hasOnComplete = onComplete !== undefined;\n const hasOnTerminate = onTerminate !== undefined;\n\n onStartRef.current = onStart;\n onCompleteRef.current = onComplete;\n onTerminateRef.current = onTerminate;\n onErrorRef.current = onError;\n\n useSafeLayoutEffect(() => {\n if (scheduledDisposeRef.current === null) {\n return;\n }\n\n globalThis.clearTimeout(scheduledDisposeRef.current);\n scheduledDisposeRef.current = null;\n });\n\n const selectStatus = React.useCallback(\n (snapshot: ReturnType<typeof runtimeMachine.getSnapshot>) => snapshot.status,\n [runtimeMachine]\n );\n const subscribeToStatus = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(selectStatus, () => {\n onStoreChange();\n }),\n [runtimeMachine, selectStatus]\n );\n const getStatus = React.useCallback(\n () => runtimeMachine.getSnapshot().status,\n [runtimeMachine]\n );\n const status = React.useSyncExternalStore(subscribeToStatus, getStatus, getStatus);\n\n useSafeLayoutEffect(() => {\n if (!hasOnStart && !hasOnComplete && !hasOnTerminate) {\n return;\n }\n\n const unsubStart = hasOnStart\n ? runtimeMachine.subscribeStart((event) => {\n onStartRef.current?.(event);\n })\n : undefined;\n\n const unsubComplete = hasOnComplete\n ? runtimeMachine.subscribeComplete((event) => {\n onCompleteRef.current?.(event);\n })\n : undefined;\n\n const unsubTerminate = hasOnTerminate\n ? runtimeMachine.subscribeTerminate((event) => {\n onTerminateRef.current?.(event);\n })\n : undefined;\n\n return () => {\n unsubStart?.();\n unsubComplete?.();\n unsubTerminate?.();\n };\n }, [runtimeMachine, hasOnComplete, hasOnStart, hasOnTerminate]);\n\n useSafeLayoutEffect(() => {\n if (status === \"idled\") {\n void runtimeMachine.start().catch((error) => {\n reportProviderError(error, { phase: \"start\" }, onErrorRef.current);\n });\n }\n }, [runtimeMachine, status]);\n\n useSafeLayoutEffect(() => {\n if (!disposeOnUnmount) {\n return;\n }\n\n return () => {\n scheduledDisposeRef.current = globalThis.setTimeout(() => {\n scheduledDisposeRef.current = null;\n runtimeMachine.dispose();\n }, 0);\n };\n }, [runtimeMachine, disposeOnUnmount]);\n\n return null;\n };\n\n const JourneyProvider = ({\n views,\n onStart,\n onComplete,\n onTerminate,\n onError,\n disposeOnUnmount = false,\n children\n }: JourneyProviderProps<TStepId, TEventMap, TStepMeta>) => {\n const runtimeMachine = machine;\n\n return (\n <ViewsContext.Provider value={views}>\n {children}\n <ProviderController\n runtimeMachine={runtimeMachine}\n onStart={onStart}\n onComplete={onComplete}\n onTerminate={onTerminate}\n onError={onError}\n disposeOnUnmount={disposeOnUnmount}\n />\n </ViewsContext.Provider>\n );\n };\n\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const currentStepId = useJourneySelector((snapshot) => snapshot.currentStepId);\n const views = useJourneyViews(\"StepRenderer\");\n const StepComponent = views[currentStepId];\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n const StepView = StepComponent as React.ComponentType;\n\n return (\n <React.Fragment key={currentStepId}>\n <StepView />\n </React.Fragment>\n );\n };\n\n return {\n JourneyProvider,\n StepRenderer\n };\n};\n", "import React from \"react\";\n\nimport type {\n JourneyComputed,\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneySelector,\n JourneySnapshot\n} from \"@rxova/journey-core\";\nimport type { JourneyApi } from \"./types\";\n\ntype SelectorCache<TContext extends JourneyJsonObject, TStepId extends string, TSelected> = {\n machine: unknown;\n snapshot: JourneySnapshot<TContext, TStepId>;\n selected: TSelected;\n selector: unknown;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nexport const createJourneyHooks = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>\n) => {\n const useJourneySnapshot = (): JourneySnapshot<TContext, TStepId> => {\n const runtimeMachine = machine;\n const getSnapshot = React.useCallback(() => runtimeMachine.getSnapshot(), [runtimeMachine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => runtimeMachine.subscribe(onStoreChange),\n [runtimeMachine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n\n const useJourneyComputed = (): JourneyComputed<TStepId> => {\n const snapshot = useJourneySnapshot();\n const runtimeMachine = machine;\n return React.useMemo(() => {\n void snapshot;\n return runtimeMachine.getComputed();\n }, [runtimeMachine, snapshot]);\n };\n\n const useJourneySelector = <TSelected,>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const runtimeMachine = machine;\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TSelected> | null>(null);\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = runtimeMachine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n isEqual(cached.selected, nextSelected)\n ) {\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [runtimeMachine, isEqual, selector]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [runtimeMachine, isEqual, selector]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n\n const useJourneyEvent = (\n listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void\n ): void => {\n const runtimeMachine = machine;\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n useSafeLayoutEffect(() => {\n return runtimeMachine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [runtimeMachine]);\n };\n\n const useJourneyStepLifecycle = (\n stepId: TStepId,\n callbacks: {\n onEnter?: (args: { context: TContext }) => void;\n onLeave?: (args: { context: TContext }) => void;\n }\n ): void => {\n useJourneyEvent((event) => {\n if (event.type === \"step.enter\" && event.stepId === stepId) {\n callbacks.onEnter?.({ context: machine.getSnapshot().context });\n } else if (event.type === \"step.exit\" && event.stepId === stepId) {\n callbacks.onLeave?.({ context: machine.getSnapshot().context });\n }\n });\n };\n\n const useJourneyApi = (): JourneyApi<TContext, TStepId, TEventMap, TStepMeta> => {\n const runtimeMachine = machine;\n return React.useMemo(\n () => ({\n start: runtimeMachine.start,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n );\n };\n\n return {\n useJourneySnapshot,\n useJourneyComputed,\n useJourneySelector,\n useJourneyApi,\n useJourneyEvent,\n useJourneyStepLifecycle\n };\n};\n"],
|
|
5
|
-
"mappings": "ukBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,mBAAAE,EAAA,yBAAAC,IAAA,eAAAC,EAAAJ,
|
|
6
|
-
"names": ["client_exports", "__export", "createJourney", "createJourneyFactory", "__toCommonJS", "import_journey_core", "import_react", "import_jsx_runtime", "useSafeLayoutEffect", "React", "reportProviderError", "error", "context", "listener", "createJourneyProviderArtifacts", "machine", "useJourneySelector", "ViewsContext", "useJourneyViews", "hookName", "views", "ProviderController", "runtimeMachine", "
|
|
4
|
+
"sourcesContent": ["\"use client\";\n\nexport * from \"./index\";\n", "/* eslint-disable no-redeclare */\nimport { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyDefinition,\n JourneyJsonObject,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins\n} from \"@rxova/journey-core\";\nimport { createJourneyProviderArtifacts } from \"./provider\";\nimport { createJourneyHooks } from \"./runtime-hooks\";\nimport type {\n JourneyRuntime,\n JourneyRuntimeFactoryFromDefinition,\n JourneyRuntimeFromDefinition,\n JourneyRuntimeFactory\n} from \"./types\";\n\ntype JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<\n TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins\n>;\n\nconst createJourneyMachineRuntime = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n const machineOptions = options as JourneyMachineOptions<TPlugins> | undefined;\n const machine = createJourneyMachine<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(definition, machineOptions) as JourneyMachineWithPlugins<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >;\n const hooks = createJourneyHooks<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n machine\n );\n const providerArtifacts = createJourneyProviderArtifacts<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(machine, hooks.useJourneySelector);\n\n return {\n machine,\n dispose: () => machine.dispose(),\n ...hooks,\n ...providerArtifacts\n };\n};\n\n/**\n * Creates a journey machine and returns React hooks/components bound to that machine.\n * Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.\n */\nexport function createJourney<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFromDefinition<TDefinition, TPlugins>;\nexport function createJourney<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n return createJourneyMachineRuntime(definition, options);\n}\n\n/**\n * Creates a typed factory for producing fresh React-bound journey runtimes.\n * Use this when a component or route boundary needs independent instances\n * from the same definition/options pair.\n */\nexport function createJourneyFactory<\n TDefinition,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;\nexport function createJourneyFactory<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactory<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n const runtimeDefinition = definition as JourneyDefinition<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers\n >;\n\n return () =>\n createJourneyMachineRuntime(\n runtimeDefinition,\n options as JourneyOptionsInput<TPlugins> | undefined\n );\n}\n", "import React from \"react\";\n\nimport type {\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneySelector,\n JourneyMachineWithPlugins,\n JourneyMachinePlugin\n} from \"@rxova/journey-core\";\nimport type { JourneyProviderErrorContext, JourneyProviderProps, JourneyViews } from \"./types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nconst reportProviderError = (\n error: unknown,\n context: JourneyProviderErrorContext,\n listener?: ((error: unknown, context: JourneyProviderErrorContext) => void) | undefined\n) => {\n if (listener) {\n listener(error, context);\n return;\n }\n\n console.error(`JourneyProvider ${context.phase} failed.`, error);\n};\n\nexport const createJourneyProviderArtifacts = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>,\n useJourneySelector: <TSelected>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ) => TSelected\n) => {\n const ViewsContext = React.createContext<JourneyViews<TStepId> | null>(null);\n\n const useJourneyViews = (hookName = \"hook\") => {\n const views = React.useContext(ViewsContext);\n if (!views) {\n throw new Error(`${hookName} must be used within JourneyProvider.`);\n }\n return views;\n };\n\n const ProviderController = ({\n runtimeMachine,\n onError,\n disposeOnUnmount\n }: {\n runtimeMachine: typeof machine;\n onError: JourneyProviderProps<TStepId>[\"onError\"] | undefined;\n disposeOnUnmount: boolean;\n }) => {\n const onErrorRef = React.useRef(onError);\n const scheduledDisposeRef = React.useRef<ReturnType<typeof globalThis.setTimeout> | null>(null);\n onErrorRef.current = onError;\n\n useSafeLayoutEffect(() => {\n if (scheduledDisposeRef.current === null) {\n return;\n }\n\n globalThis.clearTimeout(scheduledDisposeRef.current);\n scheduledDisposeRef.current = null;\n });\n\n const selectStatus = React.useCallback(\n (snapshot: ReturnType<typeof runtimeMachine.getSnapshot>) => snapshot.status,\n [runtimeMachine]\n );\n const subscribeToStatus = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(selectStatus, () => {\n onStoreChange();\n }),\n [runtimeMachine, selectStatus]\n );\n const getStatus = React.useCallback(\n () => runtimeMachine.getSnapshot().status,\n [runtimeMachine]\n );\n const status = React.useSyncExternalStore(subscribeToStatus, getStatus, getStatus);\n\n useSafeLayoutEffect(() => {\n if (status === \"idled\") {\n void runtimeMachine.startJourney().catch((error) => {\n reportProviderError(error, { phase: \"start\" }, onErrorRef.current);\n });\n }\n }, [runtimeMachine, status]);\n\n useSafeLayoutEffect(() => {\n if (!disposeOnUnmount) {\n return;\n }\n\n return () => {\n scheduledDisposeRef.current = globalThis.setTimeout(() => {\n scheduledDisposeRef.current = null;\n runtimeMachine.dispose();\n }, 0);\n };\n }, [runtimeMachine, disposeOnUnmount]);\n\n return null;\n };\n\n const JourneyProvider = ({\n views,\n onError,\n disposeOnUnmount = false,\n children\n }: JourneyProviderProps<TStepId>) => {\n const runtimeMachine = machine;\n\n return (\n <ViewsContext.Provider value={views}>\n {children}\n <ProviderController\n runtimeMachine={runtimeMachine}\n onError={onError}\n disposeOnUnmount={disposeOnUnmount}\n />\n </ViewsContext.Provider>\n );\n };\n\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const currentStepId = useJourneySelector((snapshot) => snapshot.currentStepId);\n const views = useJourneyViews(\"StepRenderer\");\n const StepComponent = views[currentStepId];\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n const StepView = StepComponent as React.ComponentType;\n\n return (\n <React.Fragment key={currentStepId}>\n <StepView />\n </React.Fragment>\n );\n };\n\n return {\n JourneyProvider,\n StepRenderer\n };\n};\n", "import React from \"react\";\n\nimport type {\n JourneyBuilderCustomEventKey,\n JourneyComputed,\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneySelector,\n JourneySnapshot\n} from \"@rxova/journey-core\";\nimport type { JourneyApi, StepScopedJourneyApi } from \"./types\";\n\ntype SelectorCache<TContext extends JourneyJsonObject, TStepId extends string, TSelected> = {\n machine: unknown;\n snapshot: JourneySnapshot<TContext, TStepId>;\n selected: TSelected;\n selector: unknown;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nexport const createJourneyHooks = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = [],\n TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> =\n Record<TStepId, never>,\n TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>\n) => {\n const useJourneySnapshot = (): JourneySnapshot<TContext, TStepId> => {\n const runtimeMachine = machine;\n const getSnapshot = React.useCallback(() => runtimeMachine.getSnapshot(), [runtimeMachine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => runtimeMachine.subscribe(onStoreChange),\n [runtimeMachine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n\n const useJourneyComputed = (): JourneyComputed<TStepId> => {\n const snapshot = useJourneySnapshot();\n const runtimeMachine = machine;\n return React.useMemo(() => {\n void snapshot;\n return runtimeMachine.getComputed();\n }, [runtimeMachine, snapshot]);\n };\n\n const useJourneySelector = <TSelected,>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const runtimeMachine = machine;\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TSelected> | null>(null);\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = runtimeMachine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n isEqual(cached.selected, nextSelected)\n ) {\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [runtimeMachine, isEqual, selector]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [runtimeMachine, isEqual, selector]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n\n const useJourneyEvent = (\n listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void\n ): void => {\n const runtimeMachine = machine;\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n useSafeLayoutEffect(() => {\n return runtimeMachine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [runtimeMachine]);\n };\n\n const useJourneyStepLifecycle = (\n stepId: TStepId,\n callbacks: {\n onEnter?: (args: { context: TContext }) => void;\n onLeave?: (args: { context: TContext }) => void;\n }\n ): void => {\n useJourneyEvent((event) => {\n if (event.type === \"step.enter\" && event.stepId === stepId) {\n callbacks.onEnter?.({ context: machine.getSnapshot().context });\n } else if (event.type === \"step.exit\" && event.stepId === stepId) {\n callbacks.onLeave?.({ context: machine.getSnapshot().context });\n }\n });\n };\n\n const useJourneyApi = (): JourneyApi<TContext, TStepId, TEventMap, TStepMeta> => {\n const runtimeMachine = machine;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n );\n };\n\n const useStepApi = <TStepKey extends TStepId>(\n stepId: TStepKey\n ): StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n > => {\n const runtimeMachine = machine;\n void stepId;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n ) as StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n >;\n };\n\n return {\n useJourneySnapshot,\n useJourneyComputed,\n useJourneySelector,\n useJourneyApi,\n useStepApi,\n useJourneyEvent,\n useJourneyStepLifecycle\n };\n};\n"],
|
|
5
|
+
"mappings": "ukBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,mBAAAE,EAAA,yBAAAC,IAAA,eAAAC,EAAAJ,GCCA,IAAAK,EAAqC,+BCDrC,IAAAC,EAAkB,sBA0HZC,EAAA,6BA/GAC,EAAsB,OAAO,OAAW,IAAc,EAAAC,QAAM,UAAY,EAAAA,QAAM,gBAE9EC,EAAsB,CAC1BC,EACAC,EACAC,IACG,CACH,GAAIA,EAAU,CACZA,EAASF,EAAOC,CAAO,EACvB,MACF,CAEA,QAAQ,MAAM,mBAAmBA,EAAQ,KAAK,WAAYD,CAAK,CACjE,EAEaG,EAAiC,CAQ5CC,EACAC,IAIG,CACH,IAAMC,EAAe,EAAAR,QA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Q,EAC/B,GAAGC,EACH,GAAGE,CACL,CACF,EAUO,SAASE,EAQdP,EACAC,EAC8E,CAC9E,OAAOF,EAA4BC,EAAYC,CAAO,CACxD,CAcO,SAASO,EAQdR,EACAC,EACqF,CACrF,IAAMQ,EAAoBT,EAQ1B,MAAO,IACLD,EACEU,EACAR,CACF,CACJ",
|
|
6
|
+
"names": ["client_exports", "__export", "createJourney", "createJourneyFactory", "__toCommonJS", "import_journey_core", "import_react", "import_jsx_runtime", "useSafeLayoutEffect", "React", "reportProviderError", "error", "context", "listener", "createJourneyProviderArtifacts", "machine", "useJourneySelector", "ViewsContext", "useJourneyViews", "hookName", "views", "ProviderController", "runtimeMachine", "onError", "disposeOnUnmount", "onErrorRef", "scheduledDisposeRef", "selectStatus", "snapshot", "subscribeToStatus", "onStoreChange", "getStatus", "status", "children", "fallback", "currentStepId", "StepComponent", "StepView", "import_react", "useSafeLayoutEffect", "React", "createJourneyHooks", "machine", "useJourneySnapshot", "runtimeMachine", "getSnapshot", "subscribe", "onStoreChange", "useJourneyComputed", "snapshot", "useJourneySelector", "selector", "equalityFn", "isEqual", "cacheRef", "getSelectedSnapshot", "nextSnapshot", "cached", "nextSelected", "subscribeToSelectedSnapshot", "useJourneyEvent", "listener", "listenerRef", "event", "stepId", "callbacks", "createJourneyMachineRuntime", "definition", "options", "machine", "hooks", "createJourneyHooks", "providerArtifacts", "createJourneyProviderArtifacts", "createJourney", "createJourneyFactory", "runtimeDefinition"]
|
|
7
7
|
}
|
package/dist/client.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use client";import{createJourneyMachine as
|
|
1
|
+
"use client";import{createJourneyMachine as I}from"@rxova/journey-core";import p from"react";import{Fragment as R,jsx as v,jsxs as C}from"react/jsx-runtime";var f=typeof window>"u"?p.useEffect:p.useLayoutEffect,P=(r,i,c)=>{if(c){c(r,i);return}console.error(`JourneyProvider ${i.phase} failed.`,r)},m=(r,i)=>{let c=p.createContext(null),y=(s="hook")=>{let e=p.useContext(c);if(!e)throw new Error(`${s} must be used within JourneyProvider.`);return e},l=({runtimeMachine:s,onError:e,disposeOnUnmount:n})=>{let t=p.useRef(e),o=p.useRef(null);t.current=e,f(()=>{o.current!==null&&(globalThis.clearTimeout(o.current),o.current=null)});let T=p.useCallback(u=>u.status,[s]),J=p.useCallback(u=>s.subscribeSelector(T,()=>{u()}),[s,T]),S=p.useCallback(()=>s.getSnapshot().status,[s]),d=p.useSyncExternalStore(J,S,S);return f(()=>{d==="idled"&&s.startJourney().catch(u=>{P(u,{phase:"start"},t.current)})},[s,d]),f(()=>{if(n)return()=>{o.current=globalThis.setTimeout(()=>{o.current=null,s.dispose()},0)}},[s,n]),null};return{JourneyProvider:({views:s,onError:e,disposeOnUnmount:n=!1,children:t})=>{let o=r;return C(c.Provider,{value:s,children:[t,v(l,{runtimeMachine:o,onError:e,disposeOnUnmount:n})]})},StepRenderer:({fallback:s=null})=>{let e=i(T=>T.currentStepId),t=y("StepRenderer")[e];if(!t)return v(R,{children:s});let o=t;return v(p.Fragment,{children:v(o,{})},e)}}};import a from"react";var b=typeof window>"u"?a.useEffect:a.useLayoutEffect,M=r=>{let i=()=>{let e=r,n=a.useCallback(()=>e.getSnapshot(),[e]),t=a.useCallback(o=>e.subscribe(o),[e]);return a.useSyncExternalStore(t,n,n)},c=()=>{let e=i(),n=r;return a.useMemo(()=>n.getComputed(),[n,e])},y=(e,n)=>{let t=r,o=n??Object.is,T=a.useRef(null),J=a.useCallback(()=>{let d=t.getSnapshot(),u=T.current;if(u&&Object.is(u.machine,t)&&Object.is(u.selector,e)&&Object.is(u.isEqual,o)&&Object.is(u.snapshot,d))return u.selected;let g=e(d);return u&&Object.is(u.machine,t)&&Object.is(u.selector,e)&&Object.is(u.isEqual,o)&&o(u.selected,g)?(T.current={machine:t,snapshot:d,selected:u.selected,selector:e,isEqual:o},u.selected):(T.current={machine:t,snapshot:d,selected:g,selector:e,isEqual:o},g)},[t,o,e]),S=a.useCallback(d=>t.subscribeSelector(e,()=>{d()},o),[t,o,e]);return a.useSyncExternalStore(S,J,J)},l=e=>{let n=r,t=a.useRef(e);t.current=e,b(()=>n.subscribeEvent(o=>{t.current(o)}),[n])};return{useJourneySnapshot:i,useJourneyComputed:c,useJourneySelector:y,useJourneyApi:()=>{let e=r;return a.useMemo(()=>({startJourney:e.startJourney,send:e.send,goToNextStep:e.goToNextStep,goToStepById:e.goToStepById,terminateJourney:e.terminateJourney,completeJourney:e.completeJourney,goToPreviousStep:e.goToPreviousStep,goToLastVisitedStep:e.goToLastVisitedStep,clearStepError:e.clearStepError,updateContext:e.updateContext,getStepMeta:e.getStepMeta,resetJourney:()=>e.resetJourney()}),[e])},useStepApi:e=>{let n=r;return a.useMemo(()=>({startJourney:n.startJourney,send:n.send,goToNextStep:n.goToNextStep,goToStepById:n.goToStepById,terminateJourney:n.terminateJourney,completeJourney:n.completeJourney,goToPreviousStep:n.goToPreviousStep,goToLastVisitedStep:n.goToLastVisitedStep,clearStepError:n.clearStepError,updateContext:n.updateContext,getStepMeta:n.getStepMeta,resetJourney:()=>n.resetJourney()}),[n])},useJourneyEvent:l,useJourneyStepLifecycle:(e,n)=>{l(t=>{t.type==="step.enter"&&t.stepId===e?n.onEnter?.({context:r.getSnapshot().context}):t.type==="step.exit"&&t.stepId===e&&n.onLeave?.({context:r.getSnapshot().context})})}}};var E=(r,i)=>{let y=I(r,i),l=M(y),x=m(y,l.useJourneySelector);return{machine:y,dispose:()=>y.dispose(),...l,...x}};function O(r,i){return E(r,i)}function w(r,i){let c=r;return()=>E(c,i)}export{O as createJourney,w as createJourneyFactory};
|
|
2
2
|
//# sourceMappingURL=client.js.map
|
package/dist/client.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/createJourney.tsx", "../src/provider.tsx", "../src/runtime-hooks.tsx"],
|
|
4
|
-
"sourcesContent": ["import { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyDefinition,\n JourneyJsonObject,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins\n} from \"@rxova/journey-core\";\nimport { createJourneyProviderArtifacts } from \"./provider\";\nimport { createJourneyHooks } from \"./runtime-hooks\";\nimport type { JourneyRuntime, JourneyRuntimeFactory } from \"./types\";\n\ntype JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<\n TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins\n>;\n\n/**\n * Creates a journey machine and returns React hooks/components bound to that machine.\n * Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.\n */\nexport const createJourney = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n const machineOptions = options as JourneyMachineOptions<TPlugins> | undefined;\n const machine = createJourneyMachine<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(definition, machineOptions) as JourneyMachineWithPlugins<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >;\n const hooks = createJourneyHooks<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n machine\n );\n const providerArtifacts = createJourneyProviderArtifacts<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(machine, hooks.useJourneySelector);\n\n return {\n machine,\n dispose: () => machine.dispose(),\n ...hooks,\n ...providerArtifacts\n };\n};\n\n/**\n * Creates a typed factory for producing fresh React-bound journey runtimes.\n * Use this when a component or route boundary needs independent instances\n * from the same definition/options pair.\n */\nexport const createJourneyFactory = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactory<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n return () =>\n createJourney<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n definition,\n options\n );\n};\n", "import React from \"react\";\n\nimport type {\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneySelector,\n JourneyMachineWithPlugins,\n JourneyMachinePlugin\n} from \"@rxova/journey-core\";\nimport type { JourneyProviderErrorContext, JourneyProviderProps, JourneyViews } from \"./types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nconst reportProviderError = (\n error: unknown,\n context: JourneyProviderErrorContext,\n listener?: ((error: unknown, context: JourneyProviderErrorContext) => void) | undefined\n) => {\n if (listener) {\n listener(error, context);\n return;\n }\n\n console.error(`JourneyProvider ${context.phase} failed.`, error);\n};\n\nexport const createJourneyProviderArtifacts = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>,\n useJourneySelector: <TSelected>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ) => TSelected\n) => {\n const ViewsContext = React.createContext<JourneyViews<TStepId> | null>(null);\n\n const useJourneyViews = (hookName = \"hook\") => {\n const views = React.useContext(ViewsContext);\n if (!views) {\n throw new Error(`${hookName} must be used within JourneyProvider.`);\n }\n return views;\n };\n\n const ProviderController = ({\n runtimeMachine,\n onStart,\n onComplete,\n onTerminate,\n onError,\n disposeOnUnmount\n }: {\n runtimeMachine: typeof machine;\n onStart: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onStart\"] | undefined;\n onComplete: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onComplete\"] | undefined;\n onTerminate: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onTerminate\"] | undefined;\n onError: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onError\"] | undefined;\n disposeOnUnmount: boolean;\n }) => {\n const onStartRef = React.useRef(onStart);\n const onCompleteRef = React.useRef(onComplete);\n const onTerminateRef = React.useRef(onTerminate);\n const onErrorRef = React.useRef(onError);\n const scheduledDisposeRef = React.useRef<ReturnType<typeof globalThis.setTimeout> | null>(null);\n const hasOnStart = onStart !== undefined;\n const hasOnComplete = onComplete !== undefined;\n const hasOnTerminate = onTerminate !== undefined;\n\n onStartRef.current = onStart;\n onCompleteRef.current = onComplete;\n onTerminateRef.current = onTerminate;\n onErrorRef.current = onError;\n\n useSafeLayoutEffect(() => {\n if (scheduledDisposeRef.current === null) {\n return;\n }\n\n globalThis.clearTimeout(scheduledDisposeRef.current);\n scheduledDisposeRef.current = null;\n });\n\n const selectStatus = React.useCallback(\n (snapshot: ReturnType<typeof runtimeMachine.getSnapshot>) => snapshot.status,\n [runtimeMachine]\n );\n const subscribeToStatus = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(selectStatus, () => {\n onStoreChange();\n }),\n [runtimeMachine, selectStatus]\n );\n const getStatus = React.useCallback(\n () => runtimeMachine.getSnapshot().status,\n [runtimeMachine]\n );\n const status = React.useSyncExternalStore(subscribeToStatus, getStatus, getStatus);\n\n useSafeLayoutEffect(() => {\n if (!hasOnStart && !hasOnComplete && !hasOnTerminate) {\n return;\n }\n\n const unsubStart = hasOnStart\n ? runtimeMachine.subscribeStart((event) => {\n onStartRef.current?.(event);\n })\n : undefined;\n\n const unsubComplete = hasOnComplete\n ? runtimeMachine.subscribeComplete((event) => {\n onCompleteRef.current?.(event);\n })\n : undefined;\n\n const unsubTerminate = hasOnTerminate\n ? runtimeMachine.subscribeTerminate((event) => {\n onTerminateRef.current?.(event);\n })\n : undefined;\n\n return () => {\n unsubStart?.();\n unsubComplete?.();\n unsubTerminate?.();\n };\n }, [runtimeMachine, hasOnComplete, hasOnStart, hasOnTerminate]);\n\n useSafeLayoutEffect(() => {\n if (status === \"idled\") {\n void runtimeMachine.start().catch((error) => {\n reportProviderError(error, { phase: \"start\" }, onErrorRef.current);\n });\n }\n }, [runtimeMachine, status]);\n\n useSafeLayoutEffect(() => {\n if (!disposeOnUnmount) {\n return;\n }\n\n return () => {\n scheduledDisposeRef.current = globalThis.setTimeout(() => {\n scheduledDisposeRef.current = null;\n runtimeMachine.dispose();\n }, 0);\n };\n }, [runtimeMachine, disposeOnUnmount]);\n\n return null;\n };\n\n const JourneyProvider = ({\n views,\n onStart,\n onComplete,\n onTerminate,\n onError,\n disposeOnUnmount = false,\n children\n }: JourneyProviderProps<TStepId, TEventMap, TStepMeta>) => {\n const runtimeMachine = machine;\n\n return (\n <ViewsContext.Provider value={views}>\n {children}\n <ProviderController\n runtimeMachine={runtimeMachine}\n onStart={onStart}\n onComplete={onComplete}\n onTerminate={onTerminate}\n onError={onError}\n disposeOnUnmount={disposeOnUnmount}\n />\n </ViewsContext.Provider>\n );\n };\n\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const currentStepId = useJourneySelector((snapshot) => snapshot.currentStepId);\n const views = useJourneyViews(\"StepRenderer\");\n const StepComponent = views[currentStepId];\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n const StepView = StepComponent as React.ComponentType;\n\n return (\n <React.Fragment key={currentStepId}>\n <StepView />\n </React.Fragment>\n );\n };\n\n return {\n JourneyProvider,\n StepRenderer\n };\n};\n", "import React from \"react\";\n\nimport type {\n JourneyComputed,\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneySelector,\n JourneySnapshot\n} from \"@rxova/journey-core\";\nimport type { JourneyApi } from \"./types\";\n\ntype SelectorCache<TContext extends JourneyJsonObject, TStepId extends string, TSelected> = {\n machine: unknown;\n snapshot: JourneySnapshot<TContext, TStepId>;\n selected: TSelected;\n selector: unknown;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nexport const createJourneyHooks = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>\n) => {\n const useJourneySnapshot = (): JourneySnapshot<TContext, TStepId> => {\n const runtimeMachine = machine;\n const getSnapshot = React.useCallback(() => runtimeMachine.getSnapshot(), [runtimeMachine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => runtimeMachine.subscribe(onStoreChange),\n [runtimeMachine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n\n const useJourneyComputed = (): JourneyComputed<TStepId> => {\n const snapshot = useJourneySnapshot();\n const runtimeMachine = machine;\n return React.useMemo(() => {\n void snapshot;\n return runtimeMachine.getComputed();\n }, [runtimeMachine, snapshot]);\n };\n\n const useJourneySelector = <TSelected,>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const runtimeMachine = machine;\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TSelected> | null>(null);\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = runtimeMachine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n isEqual(cached.selected, nextSelected)\n ) {\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [runtimeMachine, isEqual, selector]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [runtimeMachine, isEqual, selector]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n\n const useJourneyEvent = (\n listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void\n ): void => {\n const runtimeMachine = machine;\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n useSafeLayoutEffect(() => {\n return runtimeMachine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [runtimeMachine]);\n };\n\n const useJourneyStepLifecycle = (\n stepId: TStepId,\n callbacks: {\n onEnter?: (args: { context: TContext }) => void;\n onLeave?: (args: { context: TContext }) => void;\n }\n ): void => {\n useJourneyEvent((event) => {\n if (event.type === \"step.enter\" && event.stepId === stepId) {\n callbacks.onEnter?.({ context: machine.getSnapshot().context });\n } else if (event.type === \"step.exit\" && event.stepId === stepId) {\n callbacks.onLeave?.({ context: machine.getSnapshot().context });\n }\n });\n };\n\n const useJourneyApi = (): JourneyApi<TContext, TStepId, TEventMap, TStepMeta> => {\n const runtimeMachine = machine;\n return React.useMemo(\n () => ({\n start: runtimeMachine.start,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n );\n };\n\n return {\n useJourneySnapshot,\n useJourneyComputed,\n useJourneySelector,\n useJourneyApi,\n useJourneyEvent,\n useJourneyStepLifecycle\n };\n};\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["createJourneyMachine", "React", "Fragment", "jsx", "jsxs", "useSafeLayoutEffect", "reportProviderError", "error", "context", "listener", "createJourneyProviderArtifacts", "machine", "useJourneySelector", "ViewsContext", "useJourneyViews", "hookName", "views", "ProviderController", "runtimeMachine", "
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-redeclare */\nimport { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyDefinition,\n JourneyJsonObject,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins\n} from \"@rxova/journey-core\";\nimport { createJourneyProviderArtifacts } from \"./provider\";\nimport { createJourneyHooks } from \"./runtime-hooks\";\nimport type {\n JourneyRuntime,\n JourneyRuntimeFactoryFromDefinition,\n JourneyRuntimeFromDefinition,\n JourneyRuntimeFactory\n} from \"./types\";\n\ntype JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<\n TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins\n>;\n\nconst createJourneyMachineRuntime = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n const machineOptions = options as JourneyMachineOptions<TPlugins> | undefined;\n const machine = createJourneyMachine<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(definition, machineOptions) as JourneyMachineWithPlugins<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >;\n const hooks = createJourneyHooks<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n machine\n );\n const providerArtifacts = createJourneyProviderArtifacts<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(machine, hooks.useJourneySelector);\n\n return {\n machine,\n dispose: () => machine.dispose(),\n ...hooks,\n ...providerArtifacts\n };\n};\n\n/**\n * Creates a journey machine and returns React hooks/components bound to that machine.\n * Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.\n */\nexport function createJourney<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFromDefinition<TDefinition, TPlugins>;\nexport function createJourney<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n return createJourneyMachineRuntime(definition, options);\n}\n\n/**\n * Creates a typed factory for producing fresh React-bound journey runtimes.\n * Use this when a component or route boundary needs independent instances\n * from the same definition/options pair.\n */\nexport function createJourneyFactory<\n TDefinition,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;\nexport function createJourneyFactory<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactory<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n const runtimeDefinition = definition as JourneyDefinition<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers\n >;\n\n return () =>\n createJourneyMachineRuntime(\n runtimeDefinition,\n options as JourneyOptionsInput<TPlugins> | undefined\n );\n}\n", "import React from \"react\";\n\nimport type {\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneySelector,\n JourneyMachineWithPlugins,\n JourneyMachinePlugin\n} from \"@rxova/journey-core\";\nimport type { JourneyProviderErrorContext, JourneyProviderProps, JourneyViews } from \"./types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nconst reportProviderError = (\n error: unknown,\n context: JourneyProviderErrorContext,\n listener?: ((error: unknown, context: JourneyProviderErrorContext) => void) | undefined\n) => {\n if (listener) {\n listener(error, context);\n return;\n }\n\n console.error(`JourneyProvider ${context.phase} failed.`, error);\n};\n\nexport const createJourneyProviderArtifacts = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>,\n useJourneySelector: <TSelected>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ) => TSelected\n) => {\n const ViewsContext = React.createContext<JourneyViews<TStepId> | null>(null);\n\n const useJourneyViews = (hookName = \"hook\") => {\n const views = React.useContext(ViewsContext);\n if (!views) {\n throw new Error(`${hookName} must be used within JourneyProvider.`);\n }\n return views;\n };\n\n const ProviderController = ({\n runtimeMachine,\n onError,\n disposeOnUnmount\n }: {\n runtimeMachine: typeof machine;\n onError: JourneyProviderProps<TStepId>[\"onError\"] | undefined;\n disposeOnUnmount: boolean;\n }) => {\n const onErrorRef = React.useRef(onError);\n const scheduledDisposeRef = React.useRef<ReturnType<typeof globalThis.setTimeout> | null>(null);\n onErrorRef.current = onError;\n\n useSafeLayoutEffect(() => {\n if (scheduledDisposeRef.current === null) {\n return;\n }\n\n globalThis.clearTimeout(scheduledDisposeRef.current);\n scheduledDisposeRef.current = null;\n });\n\n const selectStatus = React.useCallback(\n (snapshot: ReturnType<typeof runtimeMachine.getSnapshot>) => snapshot.status,\n [runtimeMachine]\n );\n const subscribeToStatus = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(selectStatus, () => {\n onStoreChange();\n }),\n [runtimeMachine, selectStatus]\n );\n const getStatus = React.useCallback(\n () => runtimeMachine.getSnapshot().status,\n [runtimeMachine]\n );\n const status = React.useSyncExternalStore(subscribeToStatus, getStatus, getStatus);\n\n useSafeLayoutEffect(() => {\n if (status === \"idled\") {\n void runtimeMachine.startJourney().catch((error) => {\n reportProviderError(error, { phase: \"start\" }, onErrorRef.current);\n });\n }\n }, [runtimeMachine, status]);\n\n useSafeLayoutEffect(() => {\n if (!disposeOnUnmount) {\n return;\n }\n\n return () => {\n scheduledDisposeRef.current = globalThis.setTimeout(() => {\n scheduledDisposeRef.current = null;\n runtimeMachine.dispose();\n }, 0);\n };\n }, [runtimeMachine, disposeOnUnmount]);\n\n return null;\n };\n\n const JourneyProvider = ({\n views,\n onError,\n disposeOnUnmount = false,\n children\n }: JourneyProviderProps<TStepId>) => {\n const runtimeMachine = machine;\n\n return (\n <ViewsContext.Provider value={views}>\n {children}\n <ProviderController\n runtimeMachine={runtimeMachine}\n onError={onError}\n disposeOnUnmount={disposeOnUnmount}\n />\n </ViewsContext.Provider>\n );\n };\n\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const currentStepId = useJourneySelector((snapshot) => snapshot.currentStepId);\n const views = useJourneyViews(\"StepRenderer\");\n const StepComponent = views[currentStepId];\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n const StepView = StepComponent as React.ComponentType;\n\n return (\n <React.Fragment key={currentStepId}>\n <StepView />\n </React.Fragment>\n );\n };\n\n return {\n JourneyProvider,\n StepRenderer\n };\n};\n", "import React from \"react\";\n\nimport type {\n JourneyBuilderCustomEventKey,\n JourneyComputed,\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneySelector,\n JourneySnapshot\n} from \"@rxova/journey-core\";\nimport type { JourneyApi, StepScopedJourneyApi } from \"./types\";\n\ntype SelectorCache<TContext extends JourneyJsonObject, TStepId extends string, TSelected> = {\n machine: unknown;\n snapshot: JourneySnapshot<TContext, TStepId>;\n selected: TSelected;\n selector: unknown;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nexport const createJourneyHooks = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = [],\n TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> =\n Record<TStepId, never>,\n TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>\n) => {\n const useJourneySnapshot = (): JourneySnapshot<TContext, TStepId> => {\n const runtimeMachine = machine;\n const getSnapshot = React.useCallback(() => runtimeMachine.getSnapshot(), [runtimeMachine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => runtimeMachine.subscribe(onStoreChange),\n [runtimeMachine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n\n const useJourneyComputed = (): JourneyComputed<TStepId> => {\n const snapshot = useJourneySnapshot();\n const runtimeMachine = machine;\n return React.useMemo(() => {\n void snapshot;\n return runtimeMachine.getComputed();\n }, [runtimeMachine, snapshot]);\n };\n\n const useJourneySelector = <TSelected,>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const runtimeMachine = machine;\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TSelected> | null>(null);\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = runtimeMachine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n isEqual(cached.selected, nextSelected)\n ) {\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [runtimeMachine, isEqual, selector]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [runtimeMachine, isEqual, selector]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n\n const useJourneyEvent = (\n listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void\n ): void => {\n const runtimeMachine = machine;\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n useSafeLayoutEffect(() => {\n return runtimeMachine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [runtimeMachine]);\n };\n\n const useJourneyStepLifecycle = (\n stepId: TStepId,\n callbacks: {\n onEnter?: (args: { context: TContext }) => void;\n onLeave?: (args: { context: TContext }) => void;\n }\n ): void => {\n useJourneyEvent((event) => {\n if (event.type === \"step.enter\" && event.stepId === stepId) {\n callbacks.onEnter?.({ context: machine.getSnapshot().context });\n } else if (event.type === \"step.exit\" && event.stepId === stepId) {\n callbacks.onLeave?.({ context: machine.getSnapshot().context });\n }\n });\n };\n\n const useJourneyApi = (): JourneyApi<TContext, TStepId, TEventMap, TStepMeta> => {\n const runtimeMachine = machine;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n );\n };\n\n const useStepApi = <TStepKey extends TStepId>(\n stepId: TStepKey\n ): StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n > => {\n const runtimeMachine = machine;\n void stepId;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n ) as StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n >;\n };\n\n return {\n useJourneySnapshot,\n useJourneyComputed,\n useJourneySelector,\n useJourneyApi,\n useStepApi,\n useJourneyEvent,\n useJourneyStepLifecycle\n };\n};\n"],
|
|
5
|
+
"mappings": "aACA,OAAS,wBAAAA,MAA4B,sBCDrC,OAAOC,MAAW,QA0HZ,OAiBO,YAAAC,EAfL,OAAAC,EAFF,QAAAC,MAAA,oBA/GN,IAAMC,EAAsB,OAAO,OAAW,IAAcJ,EAAM,UAAYA,EAAM,gBAE9EK,EAAsB,CAC1BC,EACAC,EACAC,IACG,CACH,GAAIA,EAAU,CACZA,EAASF,EAAOC,CAAO,EACvB,MACF,CAEA,QAAQ,MAAM,mBAAmBA,EAAQ,KAAK,WAAYD,CAAK,CACjE,EAEaG,EAAiC,CAQ5CC,EACAC,IAIG,CACH,IAAMC,EAAeZ,EAAM,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YC,CAAO,CACxD,CAcO,SAASQ,EAQdT,EACAC,EACqF,CACrF,IAAMS,EAAoBV,EAQ1B,MAAO,IACLD,EACEW,EACAT,CACF,CACJ",
|
|
6
|
+
"names": ["createJourneyMachine", "React", "Fragment", "jsx", "jsxs", "useSafeLayoutEffect", "reportProviderError", "error", "context", "listener", "createJourneyProviderArtifacts", "machine", "useJourneySelector", "ViewsContext", "useJourneyViews", "hookName", "views", "ProviderController", "runtimeMachine", "onError", "disposeOnUnmount", "onErrorRef", "scheduledDisposeRef", "selectStatus", "snapshot", "subscribeToStatus", "onStoreChange", "getStatus", "status", "children", "fallback", "currentStepId", "StepComponent", "StepView", "React", "useSafeLayoutEffect", "createJourneyHooks", "machine", "useJourneySnapshot", "runtimeMachine", "getSnapshot", "subscribe", "onStoreChange", "useJourneyComputed", "snapshot", "useJourneySelector", "selector", "equalityFn", "isEqual", "cacheRef", "getSelectedSnapshot", "nextSnapshot", "cached", "nextSelected", "subscribeToSelectedSnapshot", "useJourneyEvent", "listener", "listenerRef", "event", "stepId", "callbacks", "createJourneyMachineRuntime", "definition", "options", "machine", "createJourneyMachine", "hooks", "createJourneyHooks", "providerArtifacts", "createJourneyProviderArtifacts", "createJourney", "createJourneyFactory", "runtimeDefinition"]
|
|
7
7
|
}
|
package/dist/createJourney.d.cts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { JourneyMachineOptions, JourneyMachinePlugin } from "@rxova/journey-core";
|
|
2
|
+
import type { JourneyRuntimeFactoryFromDefinition, JourneyRuntimeFromDefinition } from "./types";
|
|
3
3
|
type JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins>;
|
|
4
4
|
/**
|
|
5
5
|
* Creates a journey machine and returns React hooks/components bound to that machine.
|
|
6
6
|
* Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.
|
|
7
7
|
*/
|
|
8
|
-
export declare
|
|
8
|
+
export declare function createJourney<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(definition: TDefinition, options?: JourneyOptionsInput<TPlugins>): JourneyRuntimeFromDefinition<TDefinition, TPlugins>;
|
|
9
9
|
/**
|
|
10
10
|
* Creates a typed factory for producing fresh React-bound journey runtimes.
|
|
11
11
|
* Use this when a component or route boundary needs independent instances
|
|
12
12
|
* from the same definition/options pair.
|
|
13
13
|
*/
|
|
14
|
-
export declare
|
|
14
|
+
export declare function createJourneyFactory<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(definition: TDefinition, options?: JourneyOptionsInput<TPlugins>): JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;
|
|
15
15
|
export {};
|
package/dist/createJourney.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { JourneyMachineOptions, JourneyMachinePlugin } from "@rxova/journey-core";
|
|
2
|
+
import type { JourneyRuntimeFactoryFromDefinition, JourneyRuntimeFromDefinition } from "./types";
|
|
3
3
|
type JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins>;
|
|
4
4
|
/**
|
|
5
5
|
* Creates a journey machine and returns React hooks/components bound to that machine.
|
|
6
6
|
* Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.
|
|
7
7
|
*/
|
|
8
|
-
export declare
|
|
8
|
+
export declare function createJourney<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(definition: TDefinition, options?: JourneyOptionsInput<TPlugins>): JourneyRuntimeFromDefinition<TDefinition, TPlugins>;
|
|
9
9
|
/**
|
|
10
10
|
* Creates a typed factory for producing fresh React-bound journey runtimes.
|
|
11
11
|
* Use this when a component or route boundary needs independent instances
|
|
12
12
|
* from the same definition/options pair.
|
|
13
13
|
*/
|
|
14
|
-
export declare
|
|
14
|
+
export declare function createJourneyFactory<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(definition: TDefinition, options?: JourneyOptionsInput<TPlugins>): JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;
|
|
15
15
|
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var w=Object.create;var x=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var H=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var j=(n,r)=>{for(var s in r)x(n,s,{get:r[s],enumerable:!0})},M=(n,r,s,y)=>{if(r&&typeof r=="object"||typeof r=="function")for(let c of F(r))!D.call(n,c)&&c!==s&&x(n,c,{get:()=>r[c],enumerable:!(y=k(r,c))||y.enumerable});return n};var E=(n,r,s)=>(s=n!=null?w(H(n)):{},M(r||!n||!n.__esModule?x(s,"default",{value:n,enumerable:!0}):s,n)),A=n=>M(x({},"__esModule",{value:!0}),n);var B={};j(B,{createJourney:()=>b,createJourneyFactory:()=>I});module.exports=A(B);var C=require("@rxova/journey-core");var d=E(require("react"),1),l=require("react/jsx-runtime"),m=typeof window>"u"?d.default.useEffect:d.default.useLayoutEffect,L=(n,r,s)=>{if(s){s(n,r);return}console.error(`JourneyProvider ${r.phase} failed.`,n)},h=(n,r)=>{let s=d.default.createContext(null),y=(a="hook")=>{let e=d.default.useContext(s);if(!e)throw new Error(`${a} must be used within JourneyProvider.`);return e},c=({runtimeMachine:a,onError:e,disposeOnUnmount:t})=>{let o=d.default.useRef(e),u=d.default.useRef(null);o.current=e,m(()=>{u.current!==null&&(globalThis.clearTimeout(u.current),u.current=null)});let J=d.default.useCallback(i=>i.status,[a]),S=d.default.useCallback(i=>a.subscribeSelector(J,()=>{i()}),[a,J]),v=d.default.useCallback(()=>a.getSnapshot().status,[a]),T=d.default.useSyncExternalStore(S,v,v);return m(()=>{T==="idled"&&a.startJourney().catch(i=>{L(i,{phase:"start"},o.current)})},[a,T]),m(()=>{if(t)return()=>{u.current=globalThis.setTimeout(()=>{u.current=null,a.dispose()},0)}},[a,t]),null};return{JourneyProvider:({views:a,onError:e,disposeOnUnmount:t=!1,children:o})=>{let u=n;return(0,l.jsxs)(s.Provider,{value:a,children:[o,(0,l.jsx)(c,{runtimeMachine:u,onError:e,disposeOnUnmount:t})]})},StepRenderer:({fallback:a=null})=>{let e=r(J=>J.currentStepId),o=y("StepRenderer")[e];if(!o)return(0,l.jsx)(l.Fragment,{children:a});let u=o;return(0,l.jsx)(d.default.Fragment,{children:(0,l.jsx)(u,{})},e)}}};var p=E(require("react"),1),q=typeof window>"u"?p.default.useEffect:p.default.useLayoutEffect,P=n=>{let r=()=>{let e=n,t=p.default.useCallback(()=>e.getSnapshot(),[e]),o=p.default.useCallback(u=>e.subscribe(u),[e]);return p.default.useSyncExternalStore(o,t,t)},s=()=>{let e=r(),t=n;return p.default.useMemo(()=>t.getComputed(),[t,e])},y=(e,t)=>{let o=n,u=t??Object.is,J=p.default.useRef(null),S=p.default.useCallback(()=>{let T=o.getSnapshot(),i=J.current;if(i&&Object.is(i.machine,o)&&Object.is(i.selector,e)&&Object.is(i.isEqual,u)&&Object.is(i.snapshot,T))return i.selected;let f=e(T);return i&&Object.is(i.machine,o)&&Object.is(i.selector,e)&&Object.is(i.isEqual,u)&&u(i.selected,f)?(J.current={machine:o,snapshot:T,selected:i.selected,selector:e,isEqual:u},i.selected):(J.current={machine:o,snapshot:T,selected:f,selector:e,isEqual:u},f)},[o,u,e]),v=p.default.useCallback(T=>o.subscribeSelector(e,()=>{T()},u),[o,u,e]);return p.default.useSyncExternalStore(v,S,S)},c=e=>{let t=n,o=p.default.useRef(e);o.current=e,q(()=>t.subscribeEvent(u=>{o.current(u)}),[t])};return{useJourneySnapshot:r,useJourneyComputed:s,useJourneySelector:y,useJourneyApi:()=>{let e=n;return p.default.useMemo(()=>({startJourney:e.startJourney,send:e.send,goToNextStep:e.goToNextStep,goToStepById:e.goToStepById,terminateJourney:e.terminateJourney,completeJourney:e.completeJourney,goToPreviousStep:e.goToPreviousStep,goToLastVisitedStep:e.goToLastVisitedStep,clearStepError:e.clearStepError,updateContext:e.updateContext,getStepMeta:e.getStepMeta,resetJourney:()=>e.resetJourney()}),[e])},useStepApi:e=>{let t=n;return p.default.useMemo(()=>({startJourney:t.startJourney,send:t.send,goToNextStep:t.goToNextStep,goToStepById:t.goToStepById,terminateJourney:t.terminateJourney,completeJourney:t.completeJourney,goToPreviousStep:t.goToPreviousStep,goToLastVisitedStep:t.goToLastVisitedStep,clearStepError:t.clearStepError,updateContext:t.updateContext,getStepMeta:t.getStepMeta,resetJourney:()=>t.resetJourney()}),[t])},useJourneyEvent:c,useJourneyStepLifecycle:(e,t)=>{c(o=>{o.type==="step.enter"&&o.stepId===e?t.onEnter?.({context:n.getSnapshot().context}):o.type==="step.exit"&&o.stepId===e&&t.onLeave?.({context:n.getSnapshot().context})})}}};var R=(n,r)=>{let y=(0,C.createJourneyMachine)(n,r),c=P(y),g=h(y,c.useJourneySelector);return{machine:y,dispose:()=>y.dispose(),...c,...g}};function b(n,r){return R(n,r)}function I(n,r){let s=n;return()=>R(s,r)}0&&(module.exports={createJourney,createJourneyFactory});
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts", "../src/createJourney.tsx", "../src/provider.tsx", "../src/runtime-hooks.tsx"],
|
|
4
|
-
"sourcesContent": ["export { createJourney, createJourneyFactory } from \"./createJourney\";\nexport type {\n JourneyCompleteObservationEvent,\n JourneyComputed,\n JourneyDefinition,\n JourneyEqualityFn,\n JourneyMachine,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneySelector,\n JourneySendResult,\n JourneyStartObservationEvent,\n JourneySnapshot,\n JourneyLifecycleArgs\n} from \"@rxova/journey-core\";\nexport type {\n JourneyApi,\n JourneyCompleteEvent,\n JourneyDefaultEvent,\n JourneyProviderErrorContext,\n JourneyProviderProps,\n JourneyRuntime,\n JourneyRuntimeFactory,\n JourneyStartEvent,\n JourneyTerminateEvent,\n JourneyViews\n} from \"./types\";\n", "import { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyDefinition,\n JourneyJsonObject,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins\n} from \"@rxova/journey-core\";\nimport { createJourneyProviderArtifacts } from \"./provider\";\nimport { createJourneyHooks } from \"./runtime-hooks\";\nimport type { JourneyRuntime, JourneyRuntimeFactory } from \"./types\";\n\ntype JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<\n TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins\n>;\n\n/**\n * Creates a journey machine and returns React hooks/components bound to that machine.\n * Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.\n */\nexport const createJourney = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n const machineOptions = options as JourneyMachineOptions<TPlugins> | undefined;\n const machine = createJourneyMachine<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(definition, machineOptions) as JourneyMachineWithPlugins<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >;\n const hooks = createJourneyHooks<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n machine\n );\n const providerArtifacts = createJourneyProviderArtifacts<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(machine, hooks.useJourneySelector);\n\n return {\n machine,\n dispose: () => machine.dispose(),\n ...hooks,\n ...providerArtifacts\n };\n};\n\n/**\n * Creates a typed factory for producing fresh React-bound journey runtimes.\n * Use this when a component or route boundary needs independent instances\n * from the same definition/options pair.\n */\nexport const createJourneyFactory = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactory<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n return () =>\n createJourney<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n definition,\n options\n );\n};\n", "import React from \"react\";\n\nimport type {\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneySelector,\n JourneyMachineWithPlugins,\n JourneyMachinePlugin\n} from \"@rxova/journey-core\";\nimport type { JourneyProviderErrorContext, JourneyProviderProps, JourneyViews } from \"./types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nconst reportProviderError = (\n error: unknown,\n context: JourneyProviderErrorContext,\n listener?: ((error: unknown, context: JourneyProviderErrorContext) => void) | undefined\n) => {\n if (listener) {\n listener(error, context);\n return;\n }\n\n console.error(`JourneyProvider ${context.phase} failed.`, error);\n};\n\nexport const createJourneyProviderArtifacts = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>,\n useJourneySelector: <TSelected>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ) => TSelected\n) => {\n const ViewsContext = React.createContext<JourneyViews<TStepId> | null>(null);\n\n const useJourneyViews = (hookName = \"hook\") => {\n const views = React.useContext(ViewsContext);\n if (!views) {\n throw new Error(`${hookName} must be used within JourneyProvider.`);\n }\n return views;\n };\n\n const ProviderController = ({\n runtimeMachine,\n onStart,\n onComplete,\n onTerminate,\n onError,\n disposeOnUnmount\n }: {\n runtimeMachine: typeof machine;\n onStart: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onStart\"] | undefined;\n onComplete: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onComplete\"] | undefined;\n onTerminate: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onTerminate\"] | undefined;\n onError: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onError\"] | undefined;\n disposeOnUnmount: boolean;\n }) => {\n const onStartRef = React.useRef(onStart);\n const onCompleteRef = React.useRef(onComplete);\n const onTerminateRef = React.useRef(onTerminate);\n const onErrorRef = React.useRef(onError);\n const scheduledDisposeRef = React.useRef<ReturnType<typeof globalThis.setTimeout> | null>(null);\n const hasOnStart = onStart !== undefined;\n const hasOnComplete = onComplete !== undefined;\n const hasOnTerminate = onTerminate !== undefined;\n\n onStartRef.current = onStart;\n onCompleteRef.current = onComplete;\n onTerminateRef.current = onTerminate;\n onErrorRef.current = onError;\n\n useSafeLayoutEffect(() => {\n if (scheduledDisposeRef.current === null) {\n return;\n }\n\n globalThis.clearTimeout(scheduledDisposeRef.current);\n scheduledDisposeRef.current = null;\n });\n\n const selectStatus = React.useCallback(\n (snapshot: ReturnType<typeof runtimeMachine.getSnapshot>) => snapshot.status,\n [runtimeMachine]\n );\n const subscribeToStatus = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(selectStatus, () => {\n onStoreChange();\n }),\n [runtimeMachine, selectStatus]\n );\n const getStatus = React.useCallback(\n () => runtimeMachine.getSnapshot().status,\n [runtimeMachine]\n );\n const status = React.useSyncExternalStore(subscribeToStatus, getStatus, getStatus);\n\n useSafeLayoutEffect(() => {\n if (!hasOnStart && !hasOnComplete && !hasOnTerminate) {\n return;\n }\n\n const unsubStart = hasOnStart\n ? runtimeMachine.subscribeStart((event) => {\n onStartRef.current?.(event);\n })\n : undefined;\n\n const unsubComplete = hasOnComplete\n ? runtimeMachine.subscribeComplete((event) => {\n onCompleteRef.current?.(event);\n })\n : undefined;\n\n const unsubTerminate = hasOnTerminate\n ? runtimeMachine.subscribeTerminate((event) => {\n onTerminateRef.current?.(event);\n })\n : undefined;\n\n return () => {\n unsubStart?.();\n unsubComplete?.();\n unsubTerminate?.();\n };\n }, [runtimeMachine, hasOnComplete, hasOnStart, hasOnTerminate]);\n\n useSafeLayoutEffect(() => {\n if (status === \"idled\") {\n void runtimeMachine.start().catch((error) => {\n reportProviderError(error, { phase: \"start\" }, onErrorRef.current);\n });\n }\n }, [runtimeMachine, status]);\n\n useSafeLayoutEffect(() => {\n if (!disposeOnUnmount) {\n return;\n }\n\n return () => {\n scheduledDisposeRef.current = globalThis.setTimeout(() => {\n scheduledDisposeRef.current = null;\n runtimeMachine.dispose();\n }, 0);\n };\n }, [runtimeMachine, disposeOnUnmount]);\n\n return null;\n };\n\n const JourneyProvider = ({\n views,\n onStart,\n onComplete,\n onTerminate,\n onError,\n disposeOnUnmount = false,\n children\n }: JourneyProviderProps<TStepId, TEventMap, TStepMeta>) => {\n const runtimeMachine = machine;\n\n return (\n <ViewsContext.Provider value={views}>\n {children}\n <ProviderController\n runtimeMachine={runtimeMachine}\n onStart={onStart}\n onComplete={onComplete}\n onTerminate={onTerminate}\n onError={onError}\n disposeOnUnmount={disposeOnUnmount}\n />\n </ViewsContext.Provider>\n );\n };\n\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const currentStepId = useJourneySelector((snapshot) => snapshot.currentStepId);\n const views = useJourneyViews(\"StepRenderer\");\n const StepComponent = views[currentStepId];\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n const StepView = StepComponent as React.ComponentType;\n\n return (\n <React.Fragment key={currentStepId}>\n <StepView />\n </React.Fragment>\n );\n };\n\n return {\n JourneyProvider,\n StepRenderer\n };\n};\n", "import React from \"react\";\n\nimport type {\n JourneyComputed,\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneySelector,\n JourneySnapshot\n} from \"@rxova/journey-core\";\nimport type { JourneyApi } from \"./types\";\n\ntype SelectorCache<TContext extends JourneyJsonObject, TStepId extends string, TSelected> = {\n machine: unknown;\n snapshot: JourneySnapshot<TContext, TStepId>;\n selected: TSelected;\n selector: unknown;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nexport const createJourneyHooks = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>\n) => {\n const useJourneySnapshot = (): JourneySnapshot<TContext, TStepId> => {\n const runtimeMachine = machine;\n const getSnapshot = React.useCallback(() => runtimeMachine.getSnapshot(), [runtimeMachine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => runtimeMachine.subscribe(onStoreChange),\n [runtimeMachine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n\n const useJourneyComputed = (): JourneyComputed<TStepId> => {\n const snapshot = useJourneySnapshot();\n const runtimeMachine = machine;\n return React.useMemo(() => {\n void snapshot;\n return runtimeMachine.getComputed();\n }, [runtimeMachine, snapshot]);\n };\n\n const useJourneySelector = <TSelected,>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const runtimeMachine = machine;\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TSelected> | null>(null);\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = runtimeMachine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n isEqual(cached.selected, nextSelected)\n ) {\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [runtimeMachine, isEqual, selector]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [runtimeMachine, isEqual, selector]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n\n const useJourneyEvent = (\n listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void\n ): void => {\n const runtimeMachine = machine;\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n useSafeLayoutEffect(() => {\n return runtimeMachine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [runtimeMachine]);\n };\n\n const useJourneyStepLifecycle = (\n stepId: TStepId,\n callbacks: {\n onEnter?: (args: { context: TContext }) => void;\n onLeave?: (args: { context: TContext }) => void;\n }\n ): void => {\n useJourneyEvent((event) => {\n if (event.type === \"step.enter\" && event.stepId === stepId) {\n callbacks.onEnter?.({ context: machine.getSnapshot().context });\n } else if (event.type === \"step.exit\" && event.stepId === stepId) {\n callbacks.onLeave?.({ context: machine.getSnapshot().context });\n }\n });\n };\n\n const useJourneyApi = (): JourneyApi<TContext, TStepId, TEventMap, TStepMeta> => {\n const runtimeMachine = machine;\n return React.useMemo(\n () => ({\n start: runtimeMachine.start,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n );\n };\n\n return {\n useJourneySnapshot,\n useJourneyComputed,\n useJourneySelector,\n useJourneyApi,\n useJourneyEvent,\n useJourneyStepLifecycle\n };\n};\n"],
|
|
5
|
-
"mappings": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,mBAAAE,EAAA,yBAAAC,IAAA,eAAAC,EAAAJ,
|
|
6
|
-
"names": ["index_exports", "__export", "createJourney", "createJourneyFactory", "__toCommonJS", "import_journey_core", "import_react", "import_jsx_runtime", "useSafeLayoutEffect", "React", "reportProviderError", "error", "context", "listener", "createJourneyProviderArtifacts", "machine", "useJourneySelector", "ViewsContext", "useJourneyViews", "hookName", "views", "ProviderController", "runtimeMachine", "
|
|
4
|
+
"sourcesContent": ["export { createJourney, createJourneyFactory } from \"./createJourney\";\nexport type {\n JourneyCompleteObservationEvent,\n JourneyComputed,\n JourneyDefinition,\n JourneyEqualityFn,\n JourneyMachine,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneyResetObservationEvent,\n JourneySelector,\n JourneySendResult,\n JourneyStartObservationEvent,\n JourneySnapshot,\n JourneyLifecycleArgs\n} from \"@rxova/journey-core\";\nexport type {\n JourneyApi,\n JourneyBuilderRuntime,\n JourneyBuilderRuntimeFactory,\n JourneyBuilderRuntimeFactoryFromDefinition,\n JourneyBuilderRuntimeFromDefinition,\n JourneyDefaultEvent,\n JourneyProviderErrorContext,\n JourneyProviderProps,\n JourneyRuntime,\n JourneyRuntimeFactory,\n JourneyRuntimeFactoryFromDefinition,\n JourneyRuntimeFromDefinition,\n StepScopedJourneyApi,\n JourneyRuntimeWithStepApi,\n JourneyViews\n} from \"./types\";\n", "/* eslint-disable no-redeclare */\nimport { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyDefinition,\n JourneyJsonObject,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins\n} from \"@rxova/journey-core\";\nimport { createJourneyProviderArtifacts } from \"./provider\";\nimport { createJourneyHooks } from \"./runtime-hooks\";\nimport type {\n JourneyRuntime,\n JourneyRuntimeFactoryFromDefinition,\n JourneyRuntimeFromDefinition,\n JourneyRuntimeFactory\n} from \"./types\";\n\ntype JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<\n TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins\n>;\n\nconst createJourneyMachineRuntime = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n const machineOptions = options as JourneyMachineOptions<TPlugins> | undefined;\n const machine = createJourneyMachine<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(definition, machineOptions) as JourneyMachineWithPlugins<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >;\n const hooks = createJourneyHooks<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n machine\n );\n const providerArtifacts = createJourneyProviderArtifacts<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(machine, hooks.useJourneySelector);\n\n return {\n machine,\n dispose: () => machine.dispose(),\n ...hooks,\n ...providerArtifacts\n };\n};\n\n/**\n * Creates a journey machine and returns React hooks/components bound to that machine.\n * Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.\n */\nexport function createJourney<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFromDefinition<TDefinition, TPlugins>;\nexport function createJourney<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n return createJourneyMachineRuntime(definition, options);\n}\n\n/**\n * Creates a typed factory for producing fresh React-bound journey runtimes.\n * Use this when a component or route boundary needs independent instances\n * from the same definition/options pair.\n */\nexport function createJourneyFactory<\n TDefinition,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;\nexport function createJourneyFactory<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactory<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n const runtimeDefinition = definition as JourneyDefinition<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers\n >;\n\n return () =>\n createJourneyMachineRuntime(\n runtimeDefinition,\n options as JourneyOptionsInput<TPlugins> | undefined\n );\n}\n", "import React from \"react\";\n\nimport type {\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneySelector,\n JourneyMachineWithPlugins,\n JourneyMachinePlugin\n} from \"@rxova/journey-core\";\nimport type { JourneyProviderErrorContext, JourneyProviderProps, JourneyViews } from \"./types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nconst reportProviderError = (\n error: unknown,\n context: JourneyProviderErrorContext,\n listener?: ((error: unknown, context: JourneyProviderErrorContext) => void) | undefined\n) => {\n if (listener) {\n listener(error, context);\n return;\n }\n\n console.error(`JourneyProvider ${context.phase} failed.`, error);\n};\n\nexport const createJourneyProviderArtifacts = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>,\n useJourneySelector: <TSelected>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ) => TSelected\n) => {\n const ViewsContext = React.createContext<JourneyViews<TStepId> | null>(null);\n\n const useJourneyViews = (hookName = \"hook\") => {\n const views = React.useContext(ViewsContext);\n if (!views) {\n throw new Error(`${hookName} must be used within JourneyProvider.`);\n }\n return views;\n };\n\n const ProviderController = ({\n runtimeMachine,\n onError,\n disposeOnUnmount\n }: {\n runtimeMachine: typeof machine;\n onError: JourneyProviderProps<TStepId>[\"onError\"] | undefined;\n disposeOnUnmount: boolean;\n }) => {\n const onErrorRef = React.useRef(onError);\n const scheduledDisposeRef = React.useRef<ReturnType<typeof globalThis.setTimeout> | null>(null);\n onErrorRef.current = onError;\n\n useSafeLayoutEffect(() => {\n if (scheduledDisposeRef.current === null) {\n return;\n }\n\n globalThis.clearTimeout(scheduledDisposeRef.current);\n scheduledDisposeRef.current = null;\n });\n\n const selectStatus = React.useCallback(\n (snapshot: ReturnType<typeof runtimeMachine.getSnapshot>) => snapshot.status,\n [runtimeMachine]\n );\n const subscribeToStatus = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(selectStatus, () => {\n onStoreChange();\n }),\n [runtimeMachine, selectStatus]\n );\n const getStatus = React.useCallback(\n () => runtimeMachine.getSnapshot().status,\n [runtimeMachine]\n );\n const status = React.useSyncExternalStore(subscribeToStatus, getStatus, getStatus);\n\n useSafeLayoutEffect(() => {\n if (status === \"idled\") {\n void runtimeMachine.startJourney().catch((error) => {\n reportProviderError(error, { phase: \"start\" }, onErrorRef.current);\n });\n }\n }, [runtimeMachine, status]);\n\n useSafeLayoutEffect(() => {\n if (!disposeOnUnmount) {\n return;\n }\n\n return () => {\n scheduledDisposeRef.current = globalThis.setTimeout(() => {\n scheduledDisposeRef.current = null;\n runtimeMachine.dispose();\n }, 0);\n };\n }, [runtimeMachine, disposeOnUnmount]);\n\n return null;\n };\n\n const JourneyProvider = ({\n views,\n onError,\n disposeOnUnmount = false,\n children\n }: JourneyProviderProps<TStepId>) => {\n const runtimeMachine = machine;\n\n return (\n <ViewsContext.Provider value={views}>\n {children}\n <ProviderController\n runtimeMachine={runtimeMachine}\n onError={onError}\n disposeOnUnmount={disposeOnUnmount}\n />\n </ViewsContext.Provider>\n );\n };\n\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const currentStepId = useJourneySelector((snapshot) => snapshot.currentStepId);\n const views = useJourneyViews(\"StepRenderer\");\n const StepComponent = views[currentStepId];\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n const StepView = StepComponent as React.ComponentType;\n\n return (\n <React.Fragment key={currentStepId}>\n <StepView />\n </React.Fragment>\n );\n };\n\n return {\n JourneyProvider,\n StepRenderer\n };\n};\n", "import React from \"react\";\n\nimport type {\n JourneyBuilderCustomEventKey,\n JourneyComputed,\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneySelector,\n JourneySnapshot\n} from \"@rxova/journey-core\";\nimport type { JourneyApi, StepScopedJourneyApi } from \"./types\";\n\ntype SelectorCache<TContext extends JourneyJsonObject, TStepId extends string, TSelected> = {\n machine: unknown;\n snapshot: JourneySnapshot<TContext, TStepId>;\n selected: TSelected;\n selector: unknown;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nexport const createJourneyHooks = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = [],\n TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> =\n Record<TStepId, never>,\n TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>\n) => {\n const useJourneySnapshot = (): JourneySnapshot<TContext, TStepId> => {\n const runtimeMachine = machine;\n const getSnapshot = React.useCallback(() => runtimeMachine.getSnapshot(), [runtimeMachine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => runtimeMachine.subscribe(onStoreChange),\n [runtimeMachine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n\n const useJourneyComputed = (): JourneyComputed<TStepId> => {\n const snapshot = useJourneySnapshot();\n const runtimeMachine = machine;\n return React.useMemo(() => {\n void snapshot;\n return runtimeMachine.getComputed();\n }, [runtimeMachine, snapshot]);\n };\n\n const useJourneySelector = <TSelected,>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const runtimeMachine = machine;\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TSelected> | null>(null);\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = runtimeMachine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n isEqual(cached.selected, nextSelected)\n ) {\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [runtimeMachine, isEqual, selector]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [runtimeMachine, isEqual, selector]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n\n const useJourneyEvent = (\n listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void\n ): void => {\n const runtimeMachine = machine;\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n useSafeLayoutEffect(() => {\n return runtimeMachine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [runtimeMachine]);\n };\n\n const useJourneyStepLifecycle = (\n stepId: TStepId,\n callbacks: {\n onEnter?: (args: { context: TContext }) => void;\n onLeave?: (args: { context: TContext }) => void;\n }\n ): void => {\n useJourneyEvent((event) => {\n if (event.type === \"step.enter\" && event.stepId === stepId) {\n callbacks.onEnter?.({ context: machine.getSnapshot().context });\n } else if (event.type === \"step.exit\" && event.stepId === stepId) {\n callbacks.onLeave?.({ context: machine.getSnapshot().context });\n }\n });\n };\n\n const useJourneyApi = (): JourneyApi<TContext, TStepId, TEventMap, TStepMeta> => {\n const runtimeMachine = machine;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n );\n };\n\n const useStepApi = <TStepKey extends TStepId>(\n stepId: TStepKey\n ): StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n > => {\n const runtimeMachine = machine;\n void stepId;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n ) as StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n >;\n };\n\n return {\n useJourneySnapshot,\n useJourneyComputed,\n useJourneySelector,\n useJourneyApi,\n useStepApi,\n useJourneyEvent,\n useJourneyStepLifecycle\n };\n};\n"],
|
|
5
|
+
"mappings": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,mBAAAE,EAAA,yBAAAC,IAAA,eAAAC,EAAAJ,GCCA,IAAAK,EAAqC,+BCDrC,IAAAC,EAAkB,sBA0HZC,EAAA,6BA/GAC,EAAsB,OAAO,OAAW,IAAc,EAAAC,QAAM,UAAY,EAAAA,QAAM,gBAE9EC,EAAsB,CAC1BC,EACAC,EACAC,IACG,CACH,GAAIA,EAAU,CACZA,EAASF,EAAOC,CAAO,EACvB,MACF,CAEA,QAAQ,MAAM,mBAAmBA,EAAQ,KAAK,WAAYD,CAAK,CACjE,EAEaG,EAAiC,CAQ5CC,EACAC,IAIG,CACH,IAAMC,EAAe,EAAAR,QAAM,cAA4C,IAAI,EAErES,EAAkB,CAACC,EAAW,SAAW,CAC7C,IAAMC,EAAQ,EAAAX,QAAM,WAAWQ,CAAY,EAC3C,GAAI,CAACG,EACH,MAAM,IAAI,MAAM,GAAGD,CAAQ,uCAAuC,EAEpE,OAAOC,CACT,EAEMC,EAAqB,CAAC,CAC1B,e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",
|
|
6
|
+
"names": ["index_exports", "__export", "createJourney", "createJourneyFactory", "__toCommonJS", "import_journey_core", "import_react", "import_jsx_runtime", "useSafeLayoutEffect", "React", "reportProviderError", "error", "context", "listener", "createJourneyProviderArtifacts", "machine", "useJourneySelector", "ViewsContext", "useJourneyViews", "hookName", "views", "ProviderController", "runtimeMachine", "onError", "disposeOnUnmount", "onErrorRef", "scheduledDisposeRef", "selectStatus", "snapshot", "subscribeToStatus", "onStoreChange", "getStatus", "status", "children", "fallback", "currentStepId", "StepComponent", "StepView", "import_react", "useSafeLayoutEffect", "React", "createJourneyHooks", "machine", "useJourneySnapshot", "runtimeMachine", "getSnapshot", "subscribe", "onStoreChange", "useJourneyComputed", "snapshot", "useJourneySelector", "selector", "equalityFn", "isEqual", "cacheRef", "getSelectedSnapshot", "nextSnapshot", "cached", "nextSelected", "subscribeToSelectedSnapshot", "useJourneyEvent", "listener", "listenerRef", "event", "stepId", "callbacks", "createJourneyMachineRuntime", "definition", "options", "machine", "hooks", "createJourneyHooks", "providerArtifacts", "createJourneyProviderArtifacts", "createJourney", "createJourneyFactory", "runtimeDefinition"]
|
|
7
7
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { createJourney, createJourneyFactory } from "./createJourney";
|
|
2
|
-
export type { JourneyCompleteObservationEvent, JourneyComputed, JourneyDefinition, JourneyEqualityFn, JourneyMachine, JourneyMachineOptions, JourneyMachinePlugin, JourneyMachineWithPlugins, JourneyObservationEvent, JourneySelector, JourneySendResult, JourneyStartObservationEvent, JourneySnapshot, JourneyLifecycleArgs } from "@rxova/journey-core";
|
|
3
|
-
export type { JourneyApi,
|
|
2
|
+
export type { JourneyCompleteObservationEvent, JourneyComputed, JourneyDefinition, JourneyEqualityFn, JourneyMachine, JourneyMachineOptions, JourneyMachinePlugin, JourneyMachineWithPlugins, JourneyObservationEvent, JourneyResetObservationEvent, JourneySelector, JourneySendResult, JourneyStartObservationEvent, JourneySnapshot, JourneyLifecycleArgs } from "@rxova/journey-core";
|
|
3
|
+
export type { JourneyApi, JourneyBuilderRuntime, JourneyBuilderRuntimeFactory, JourneyBuilderRuntimeFactoryFromDefinition, JourneyBuilderRuntimeFromDefinition, JourneyDefaultEvent, JourneyProviderErrorContext, JourneyProviderProps, JourneyRuntime, JourneyRuntimeFactory, JourneyRuntimeFactoryFromDefinition, JourneyRuntimeFromDefinition, StepScopedJourneyApi, JourneyRuntimeWithStepApi, JourneyViews } from "./types";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { createJourney, createJourneyFactory } from "./createJourney";
|
|
2
|
-
export type { JourneyCompleteObservationEvent, JourneyComputed, JourneyDefinition, JourneyEqualityFn, JourneyMachine, JourneyMachineOptions, JourneyMachinePlugin, JourneyMachineWithPlugins, JourneyObservationEvent, JourneySelector, JourneySendResult, JourneyStartObservationEvent, JourneySnapshot, JourneyLifecycleArgs } from "@rxova/journey-core";
|
|
3
|
-
export type { JourneyApi,
|
|
2
|
+
export type { JourneyCompleteObservationEvent, JourneyComputed, JourneyDefinition, JourneyEqualityFn, JourneyMachine, JourneyMachineOptions, JourneyMachinePlugin, JourneyMachineWithPlugins, JourneyObservationEvent, JourneyResetObservationEvent, JourneySelector, JourneySendResult, JourneyStartObservationEvent, JourneySnapshot, JourneyLifecycleArgs } from "@rxova/journey-core";
|
|
3
|
+
export type { JourneyApi, JourneyBuilderRuntime, JourneyBuilderRuntimeFactory, JourneyBuilderRuntimeFactoryFromDefinition, JourneyBuilderRuntimeFromDefinition, JourneyDefaultEvent, JourneyProviderErrorContext, JourneyProviderProps, JourneyRuntime, JourneyRuntimeFactory, JourneyRuntimeFactoryFromDefinition, JourneyRuntimeFromDefinition, StepScopedJourneyApi, JourneyRuntimeWithStepApi, JourneyViews } from "./types";
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{createJourneyMachine as
|
|
1
|
+
import{createJourneyMachine as I}from"@rxova/journey-core";import p from"react";import{Fragment as R,jsx as v,jsxs as C}from"react/jsx-runtime";var f=typeof window>"u"?p.useEffect:p.useLayoutEffect,P=(r,i,c)=>{if(c){c(r,i);return}console.error(`JourneyProvider ${i.phase} failed.`,r)},m=(r,i)=>{let c=p.createContext(null),y=(s="hook")=>{let e=p.useContext(c);if(!e)throw new Error(`${s} must be used within JourneyProvider.`);return e},l=({runtimeMachine:s,onError:e,disposeOnUnmount:n})=>{let t=p.useRef(e),o=p.useRef(null);t.current=e,f(()=>{o.current!==null&&(globalThis.clearTimeout(o.current),o.current=null)});let T=p.useCallback(u=>u.status,[s]),J=p.useCallback(u=>s.subscribeSelector(T,()=>{u()}),[s,T]),S=p.useCallback(()=>s.getSnapshot().status,[s]),d=p.useSyncExternalStore(J,S,S);return f(()=>{d==="idled"&&s.startJourney().catch(u=>{P(u,{phase:"start"},t.current)})},[s,d]),f(()=>{if(n)return()=>{o.current=globalThis.setTimeout(()=>{o.current=null,s.dispose()},0)}},[s,n]),null};return{JourneyProvider:({views:s,onError:e,disposeOnUnmount:n=!1,children:t})=>{let o=r;return C(c.Provider,{value:s,children:[t,v(l,{runtimeMachine:o,onError:e,disposeOnUnmount:n})]})},StepRenderer:({fallback:s=null})=>{let e=i(T=>T.currentStepId),t=y("StepRenderer")[e];if(!t)return v(R,{children:s});let o=t;return v(p.Fragment,{children:v(o,{})},e)}}};import a from"react";var b=typeof window>"u"?a.useEffect:a.useLayoutEffect,M=r=>{let i=()=>{let e=r,n=a.useCallback(()=>e.getSnapshot(),[e]),t=a.useCallback(o=>e.subscribe(o),[e]);return a.useSyncExternalStore(t,n,n)},c=()=>{let e=i(),n=r;return a.useMemo(()=>n.getComputed(),[n,e])},y=(e,n)=>{let t=r,o=n??Object.is,T=a.useRef(null),J=a.useCallback(()=>{let d=t.getSnapshot(),u=T.current;if(u&&Object.is(u.machine,t)&&Object.is(u.selector,e)&&Object.is(u.isEqual,o)&&Object.is(u.snapshot,d))return u.selected;let g=e(d);return u&&Object.is(u.machine,t)&&Object.is(u.selector,e)&&Object.is(u.isEqual,o)&&o(u.selected,g)?(T.current={machine:t,snapshot:d,selected:u.selected,selector:e,isEqual:o},u.selected):(T.current={machine:t,snapshot:d,selected:g,selector:e,isEqual:o},g)},[t,o,e]),S=a.useCallback(d=>t.subscribeSelector(e,()=>{d()},o),[t,o,e]);return a.useSyncExternalStore(S,J,J)},l=e=>{let n=r,t=a.useRef(e);t.current=e,b(()=>n.subscribeEvent(o=>{t.current(o)}),[n])};return{useJourneySnapshot:i,useJourneyComputed:c,useJourneySelector:y,useJourneyApi:()=>{let e=r;return a.useMemo(()=>({startJourney:e.startJourney,send:e.send,goToNextStep:e.goToNextStep,goToStepById:e.goToStepById,terminateJourney:e.terminateJourney,completeJourney:e.completeJourney,goToPreviousStep:e.goToPreviousStep,goToLastVisitedStep:e.goToLastVisitedStep,clearStepError:e.clearStepError,updateContext:e.updateContext,getStepMeta:e.getStepMeta,resetJourney:()=>e.resetJourney()}),[e])},useStepApi:e=>{let n=r;return a.useMemo(()=>({startJourney:n.startJourney,send:n.send,goToNextStep:n.goToNextStep,goToStepById:n.goToStepById,terminateJourney:n.terminateJourney,completeJourney:n.completeJourney,goToPreviousStep:n.goToPreviousStep,goToLastVisitedStep:n.goToLastVisitedStep,clearStepError:n.clearStepError,updateContext:n.updateContext,getStepMeta:n.getStepMeta,resetJourney:()=>n.resetJourney()}),[n])},useJourneyEvent:l,useJourneyStepLifecycle:(e,n)=>{l(t=>{t.type==="step.enter"&&t.stepId===e?n.onEnter?.({context:r.getSnapshot().context}):t.type==="step.exit"&&t.stepId===e&&n.onLeave?.({context:r.getSnapshot().context})})}}};var E=(r,i)=>{let y=I(r,i),l=M(y),x=m(y,l.useJourneySelector);return{machine:y,dispose:()=>y.dispose(),...l,...x}};function O(r,i){return E(r,i)}function w(r,i){let c=r;return()=>E(c,i)}export{O as createJourney,w as createJourneyFactory};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/createJourney.tsx", "../src/provider.tsx", "../src/runtime-hooks.tsx"],
|
|
4
|
-
"sourcesContent": ["import { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyDefinition,\n JourneyJsonObject,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins\n} from \"@rxova/journey-core\";\nimport { createJourneyProviderArtifacts } from \"./provider\";\nimport { createJourneyHooks } from \"./runtime-hooks\";\nimport type { JourneyRuntime, JourneyRuntimeFactory } from \"./types\";\n\ntype JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<\n TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins\n>;\n\n/**\n * Creates a journey machine and returns React hooks/components bound to that machine.\n * Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.\n */\nexport const createJourney = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n const machineOptions = options as JourneyMachineOptions<TPlugins> | undefined;\n const machine = createJourneyMachine<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(definition, machineOptions) as JourneyMachineWithPlugins<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >;\n const hooks = createJourneyHooks<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n machine\n );\n const providerArtifacts = createJourneyProviderArtifacts<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(machine, hooks.useJourneySelector);\n\n return {\n machine,\n dispose: () => machine.dispose(),\n ...hooks,\n ...providerArtifacts\n };\n};\n\n/**\n * Creates a typed factory for producing fresh React-bound journey runtimes.\n * Use this when a component or route boundary needs independent instances\n * from the same definition/options pair.\n */\nexport const createJourneyFactory = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactory<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n return () =>\n createJourney<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n definition,\n options\n );\n};\n", "import React from \"react\";\n\nimport type {\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneySelector,\n JourneyMachineWithPlugins,\n JourneyMachinePlugin\n} from \"@rxova/journey-core\";\nimport type { JourneyProviderErrorContext, JourneyProviderProps, JourneyViews } from \"./types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nconst reportProviderError = (\n error: unknown,\n context: JourneyProviderErrorContext,\n listener?: ((error: unknown, context: JourneyProviderErrorContext) => void) | undefined\n) => {\n if (listener) {\n listener(error, context);\n return;\n }\n\n console.error(`JourneyProvider ${context.phase} failed.`, error);\n};\n\nexport const createJourneyProviderArtifacts = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>,\n useJourneySelector: <TSelected>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ) => TSelected\n) => {\n const ViewsContext = React.createContext<JourneyViews<TStepId> | null>(null);\n\n const useJourneyViews = (hookName = \"hook\") => {\n const views = React.useContext(ViewsContext);\n if (!views) {\n throw new Error(`${hookName} must be used within JourneyProvider.`);\n }\n return views;\n };\n\n const ProviderController = ({\n runtimeMachine,\n onStart,\n onComplete,\n onTerminate,\n onError,\n disposeOnUnmount\n }: {\n runtimeMachine: typeof machine;\n onStart: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onStart\"] | undefined;\n onComplete: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onComplete\"] | undefined;\n onTerminate: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onTerminate\"] | undefined;\n onError: JourneyProviderProps<TStepId, TEventMap, TStepMeta>[\"onError\"] | undefined;\n disposeOnUnmount: boolean;\n }) => {\n const onStartRef = React.useRef(onStart);\n const onCompleteRef = React.useRef(onComplete);\n const onTerminateRef = React.useRef(onTerminate);\n const onErrorRef = React.useRef(onError);\n const scheduledDisposeRef = React.useRef<ReturnType<typeof globalThis.setTimeout> | null>(null);\n const hasOnStart = onStart !== undefined;\n const hasOnComplete = onComplete !== undefined;\n const hasOnTerminate = onTerminate !== undefined;\n\n onStartRef.current = onStart;\n onCompleteRef.current = onComplete;\n onTerminateRef.current = onTerminate;\n onErrorRef.current = onError;\n\n useSafeLayoutEffect(() => {\n if (scheduledDisposeRef.current === null) {\n return;\n }\n\n globalThis.clearTimeout(scheduledDisposeRef.current);\n scheduledDisposeRef.current = null;\n });\n\n const selectStatus = React.useCallback(\n (snapshot: ReturnType<typeof runtimeMachine.getSnapshot>) => snapshot.status,\n [runtimeMachine]\n );\n const subscribeToStatus = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(selectStatus, () => {\n onStoreChange();\n }),\n [runtimeMachine, selectStatus]\n );\n const getStatus = React.useCallback(\n () => runtimeMachine.getSnapshot().status,\n [runtimeMachine]\n );\n const status = React.useSyncExternalStore(subscribeToStatus, getStatus, getStatus);\n\n useSafeLayoutEffect(() => {\n if (!hasOnStart && !hasOnComplete && !hasOnTerminate) {\n return;\n }\n\n const unsubStart = hasOnStart\n ? runtimeMachine.subscribeStart((event) => {\n onStartRef.current?.(event);\n })\n : undefined;\n\n const unsubComplete = hasOnComplete\n ? runtimeMachine.subscribeComplete((event) => {\n onCompleteRef.current?.(event);\n })\n : undefined;\n\n const unsubTerminate = hasOnTerminate\n ? runtimeMachine.subscribeTerminate((event) => {\n onTerminateRef.current?.(event);\n })\n : undefined;\n\n return () => {\n unsubStart?.();\n unsubComplete?.();\n unsubTerminate?.();\n };\n }, [runtimeMachine, hasOnComplete, hasOnStart, hasOnTerminate]);\n\n useSafeLayoutEffect(() => {\n if (status === \"idled\") {\n void runtimeMachine.start().catch((error) => {\n reportProviderError(error, { phase: \"start\" }, onErrorRef.current);\n });\n }\n }, [runtimeMachine, status]);\n\n useSafeLayoutEffect(() => {\n if (!disposeOnUnmount) {\n return;\n }\n\n return () => {\n scheduledDisposeRef.current = globalThis.setTimeout(() => {\n scheduledDisposeRef.current = null;\n runtimeMachine.dispose();\n }, 0);\n };\n }, [runtimeMachine, disposeOnUnmount]);\n\n return null;\n };\n\n const JourneyProvider = ({\n views,\n onStart,\n onComplete,\n onTerminate,\n onError,\n disposeOnUnmount = false,\n children\n }: JourneyProviderProps<TStepId, TEventMap, TStepMeta>) => {\n const runtimeMachine = machine;\n\n return (\n <ViewsContext.Provider value={views}>\n {children}\n <ProviderController\n runtimeMachine={runtimeMachine}\n onStart={onStart}\n onComplete={onComplete}\n onTerminate={onTerminate}\n onError={onError}\n disposeOnUnmount={disposeOnUnmount}\n />\n </ViewsContext.Provider>\n );\n };\n\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const currentStepId = useJourneySelector((snapshot) => snapshot.currentStepId);\n const views = useJourneyViews(\"StepRenderer\");\n const StepComponent = views[currentStepId];\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n const StepView = StepComponent as React.ComponentType;\n\n return (\n <React.Fragment key={currentStepId}>\n <StepView />\n </React.Fragment>\n );\n };\n\n return {\n JourneyProvider,\n StepRenderer\n };\n};\n", "import React from \"react\";\n\nimport type {\n JourneyComputed,\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneySelector,\n JourneySnapshot\n} from \"@rxova/journey-core\";\nimport type { JourneyApi } from \"./types\";\n\ntype SelectorCache<TContext extends JourneyJsonObject, TStepId extends string, TSelected> = {\n machine: unknown;\n snapshot: JourneySnapshot<TContext, TStepId>;\n selected: TSelected;\n selector: unknown;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nexport const createJourneyHooks = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>\n) => {\n const useJourneySnapshot = (): JourneySnapshot<TContext, TStepId> => {\n const runtimeMachine = machine;\n const getSnapshot = React.useCallback(() => runtimeMachine.getSnapshot(), [runtimeMachine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => runtimeMachine.subscribe(onStoreChange),\n [runtimeMachine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n\n const useJourneyComputed = (): JourneyComputed<TStepId> => {\n const snapshot = useJourneySnapshot();\n const runtimeMachine = machine;\n return React.useMemo(() => {\n void snapshot;\n return runtimeMachine.getComputed();\n }, [runtimeMachine, snapshot]);\n };\n\n const useJourneySelector = <TSelected,>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const runtimeMachine = machine;\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TSelected> | null>(null);\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = runtimeMachine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n isEqual(cached.selected, nextSelected)\n ) {\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [runtimeMachine, isEqual, selector]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [runtimeMachine, isEqual, selector]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n\n const useJourneyEvent = (\n listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void\n ): void => {\n const runtimeMachine = machine;\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n useSafeLayoutEffect(() => {\n return runtimeMachine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [runtimeMachine]);\n };\n\n const useJourneyStepLifecycle = (\n stepId: TStepId,\n callbacks: {\n onEnter?: (args: { context: TContext }) => void;\n onLeave?: (args: { context: TContext }) => void;\n }\n ): void => {\n useJourneyEvent((event) => {\n if (event.type === \"step.enter\" && event.stepId === stepId) {\n callbacks.onEnter?.({ context: machine.getSnapshot().context });\n } else if (event.type === \"step.exit\" && event.stepId === stepId) {\n callbacks.onLeave?.({ context: machine.getSnapshot().context });\n }\n });\n };\n\n const useJourneyApi = (): JourneyApi<TContext, TStepId, TEventMap, TStepMeta> => {\n const runtimeMachine = machine;\n return React.useMemo(\n () => ({\n start: runtimeMachine.start,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n );\n };\n\n return {\n useJourneySnapshot,\n useJourneyComputed,\n useJourneySelector,\n useJourneyApi,\n useJourneyEvent,\n useJourneyStepLifecycle\n };\n};\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["createJourneyMachine", "React", "Fragment", "jsx", "jsxs", "useSafeLayoutEffect", "reportProviderError", "error", "context", "listener", "createJourneyProviderArtifacts", "machine", "useJourneySelector", "ViewsContext", "useJourneyViews", "hookName", "views", "ProviderController", "runtimeMachine", "
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-redeclare */\nimport { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyDefinition,\n JourneyJsonObject,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins\n} from \"@rxova/journey-core\";\nimport { createJourneyProviderArtifacts } from \"./provider\";\nimport { createJourneyHooks } from \"./runtime-hooks\";\nimport type {\n JourneyRuntime,\n JourneyRuntimeFactoryFromDefinition,\n JourneyRuntimeFromDefinition,\n JourneyRuntimeFactory\n} from \"./types\";\n\ntype JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<\n TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins\n>;\n\nconst createJourneyMachineRuntime = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n const machineOptions = options as JourneyMachineOptions<TPlugins> | undefined;\n const machine = createJourneyMachine<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(definition, machineOptions) as JourneyMachineWithPlugins<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >;\n const hooks = createJourneyHooks<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n machine\n );\n const providerArtifacts = createJourneyProviderArtifacts<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(machine, hooks.useJourneySelector);\n\n return {\n machine,\n dispose: () => machine.dispose(),\n ...hooks,\n ...providerArtifacts\n };\n};\n\n/**\n * Creates a journey machine and returns React hooks/components bound to that machine.\n * Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.\n */\nexport function createJourney<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFromDefinition<TDefinition, TPlugins>;\nexport function createJourney<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n return createJourneyMachineRuntime(definition, options);\n}\n\n/**\n * Creates a typed factory for producing fresh React-bound journey runtimes.\n * Use this when a component or route boundary needs independent instances\n * from the same definition/options pair.\n */\nexport function createJourneyFactory<\n TDefinition,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;\nexport function createJourneyFactory<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactory<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n const runtimeDefinition = definition as JourneyDefinition<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers\n >;\n\n return () =>\n createJourneyMachineRuntime(\n runtimeDefinition,\n options as JourneyOptionsInput<TPlugins> | undefined\n );\n}\n", "import React from \"react\";\n\nimport type {\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneySelector,\n JourneyMachineWithPlugins,\n JourneyMachinePlugin\n} from \"@rxova/journey-core\";\nimport type { JourneyProviderErrorContext, JourneyProviderProps, JourneyViews } from \"./types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nconst reportProviderError = (\n error: unknown,\n context: JourneyProviderErrorContext,\n listener?: ((error: unknown, context: JourneyProviderErrorContext) => void) | undefined\n) => {\n if (listener) {\n listener(error, context);\n return;\n }\n\n console.error(`JourneyProvider ${context.phase} failed.`, error);\n};\n\nexport const createJourneyProviderArtifacts = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>,\n useJourneySelector: <TSelected>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ) => TSelected\n) => {\n const ViewsContext = React.createContext<JourneyViews<TStepId> | null>(null);\n\n const useJourneyViews = (hookName = \"hook\") => {\n const views = React.useContext(ViewsContext);\n if (!views) {\n throw new Error(`${hookName} must be used within JourneyProvider.`);\n }\n return views;\n };\n\n const ProviderController = ({\n runtimeMachine,\n onError,\n disposeOnUnmount\n }: {\n runtimeMachine: typeof machine;\n onError: JourneyProviderProps<TStepId>[\"onError\"] | undefined;\n disposeOnUnmount: boolean;\n }) => {\n const onErrorRef = React.useRef(onError);\n const scheduledDisposeRef = React.useRef<ReturnType<typeof globalThis.setTimeout> | null>(null);\n onErrorRef.current = onError;\n\n useSafeLayoutEffect(() => {\n if (scheduledDisposeRef.current === null) {\n return;\n }\n\n globalThis.clearTimeout(scheduledDisposeRef.current);\n scheduledDisposeRef.current = null;\n });\n\n const selectStatus = React.useCallback(\n (snapshot: ReturnType<typeof runtimeMachine.getSnapshot>) => snapshot.status,\n [runtimeMachine]\n );\n const subscribeToStatus = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(selectStatus, () => {\n onStoreChange();\n }),\n [runtimeMachine, selectStatus]\n );\n const getStatus = React.useCallback(\n () => runtimeMachine.getSnapshot().status,\n [runtimeMachine]\n );\n const status = React.useSyncExternalStore(subscribeToStatus, getStatus, getStatus);\n\n useSafeLayoutEffect(() => {\n if (status === \"idled\") {\n void runtimeMachine.startJourney().catch((error) => {\n reportProviderError(error, { phase: \"start\" }, onErrorRef.current);\n });\n }\n }, [runtimeMachine, status]);\n\n useSafeLayoutEffect(() => {\n if (!disposeOnUnmount) {\n return;\n }\n\n return () => {\n scheduledDisposeRef.current = globalThis.setTimeout(() => {\n scheduledDisposeRef.current = null;\n runtimeMachine.dispose();\n }, 0);\n };\n }, [runtimeMachine, disposeOnUnmount]);\n\n return null;\n };\n\n const JourneyProvider = ({\n views,\n onError,\n disposeOnUnmount = false,\n children\n }: JourneyProviderProps<TStepId>) => {\n const runtimeMachine = machine;\n\n return (\n <ViewsContext.Provider value={views}>\n {children}\n <ProviderController\n runtimeMachine={runtimeMachine}\n onError={onError}\n disposeOnUnmount={disposeOnUnmount}\n />\n </ViewsContext.Provider>\n );\n };\n\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const currentStepId = useJourneySelector((snapshot) => snapshot.currentStepId);\n const views = useJourneyViews(\"StepRenderer\");\n const StepComponent = views[currentStepId];\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n const StepView = StepComponent as React.ComponentType;\n\n return (\n <React.Fragment key={currentStepId}>\n <StepView />\n </React.Fragment>\n );\n };\n\n return {\n JourneyProvider,\n StepRenderer\n };\n};\n", "import React from \"react\";\n\nimport type {\n JourneyBuilderCustomEventKey,\n JourneyComputed,\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneySelector,\n JourneySnapshot\n} from \"@rxova/journey-core\";\nimport type { JourneyApi, StepScopedJourneyApi } from \"./types\";\n\ntype SelectorCache<TContext extends JourneyJsonObject, TStepId extends string, TSelected> = {\n machine: unknown;\n snapshot: JourneySnapshot<TContext, TStepId>;\n selected: TSelected;\n selector: unknown;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nexport const createJourneyHooks = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = [],\n TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> =\n Record<TStepId, never>,\n TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>\n) => {\n const useJourneySnapshot = (): JourneySnapshot<TContext, TStepId> => {\n const runtimeMachine = machine;\n const getSnapshot = React.useCallback(() => runtimeMachine.getSnapshot(), [runtimeMachine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => runtimeMachine.subscribe(onStoreChange),\n [runtimeMachine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n\n const useJourneyComputed = (): JourneyComputed<TStepId> => {\n const snapshot = useJourneySnapshot();\n const runtimeMachine = machine;\n return React.useMemo(() => {\n void snapshot;\n return runtimeMachine.getComputed();\n }, [runtimeMachine, snapshot]);\n };\n\n const useJourneySelector = <TSelected,>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const runtimeMachine = machine;\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TSelected> | null>(null);\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = runtimeMachine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n isEqual(cached.selected, nextSelected)\n ) {\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [runtimeMachine, isEqual, selector]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [runtimeMachine, isEqual, selector]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n\n const useJourneyEvent = (\n listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void\n ): void => {\n const runtimeMachine = machine;\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n useSafeLayoutEffect(() => {\n return runtimeMachine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [runtimeMachine]);\n };\n\n const useJourneyStepLifecycle = (\n stepId: TStepId,\n callbacks: {\n onEnter?: (args: { context: TContext }) => void;\n onLeave?: (args: { context: TContext }) => void;\n }\n ): void => {\n useJourneyEvent((event) => {\n if (event.type === \"step.enter\" && event.stepId === stepId) {\n callbacks.onEnter?.({ context: machine.getSnapshot().context });\n } else if (event.type === \"step.exit\" && event.stepId === stepId) {\n callbacks.onLeave?.({ context: machine.getSnapshot().context });\n }\n });\n };\n\n const useJourneyApi = (): JourneyApi<TContext, TStepId, TEventMap, TStepMeta> => {\n const runtimeMachine = machine;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n );\n };\n\n const useStepApi = <TStepKey extends TStepId>(\n stepId: TStepKey\n ): StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n > => {\n const runtimeMachine = machine;\n void stepId;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n ) as StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n >;\n };\n\n return {\n useJourneySnapshot,\n useJourneyComputed,\n useJourneySelector,\n useJourneyApi,\n useStepApi,\n useJourneyEvent,\n useJourneyStepLifecycle\n };\n};\n"],
|
|
5
|
+
"mappings": "AACA,OAAS,wBAAAA,MAA4B,sBCDrC,OAAOC,MAAW,QA0HZ,OAiBO,YAAAC,EAfL,OAAAC,EAFF,QAAAC,MAAA,oBA/GN,IAAMC,EAAsB,OAAO,OAAW,IAAcJ,EAAM,UAAYA,EAAM,gBAE9EK,EAAsB,CA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CACF,EACMI,EAAoBC,EAOxBL,EAASE,EAAM,kBAAkB,EAEnC,MAAO,CACL,QAAAF,EACA,QAAS,IAAMA,EAAQ,QAAQ,EAC/B,GAAGE,EACH,GAAGE,CACL,CACF,EAUO,SAASE,EAQdR,EACAC,EAC8E,CAC9E,OAAOF,EAA4BC,EAAYC,CAAO,CACxD,CAcO,SAASQ,EAQdT,EACAC,EACqF,CACrF,IAAMS,EAAoBV,EAQ1B,MAAO,IACLD,EACEW,EACAT,CACF,CACJ",
|
|
6
|
+
"names": ["createJourneyMachine", "React", "Fragment", "jsx", "jsxs", "useSafeLayoutEffect", "reportProviderError", "error", "context", "listener", "createJourneyProviderArtifacts", "machine", "useJourneySelector", "ViewsContext", "useJourneyViews", "hookName", "views", "ProviderController", "runtimeMachine", "onError", "disposeOnUnmount", "onErrorRef", "scheduledDisposeRef", "selectStatus", "snapshot", "subscribeToStatus", "onStoreChange", "getStatus", "status", "children", "fallback", "currentStepId", "StepComponent", "StepView", "React", "useSafeLayoutEffect", "createJourneyHooks", "machine", "useJourneySnapshot", "runtimeMachine", "getSnapshot", "subscribe", "onStoreChange", "useJourneyComputed", "snapshot", "useJourneySelector", "selector", "equalityFn", "isEqual", "cacheRef", "getSelectedSnapshot", "nextSnapshot", "cached", "nextSelected", "subscribeToSelectedSnapshot", "useJourneyEvent", "listener", "listenerRef", "event", "stepId", "callbacks", "createJourneyMachineRuntime", "definition", "options", "machine", "createJourneyMachine", "hooks", "createJourneyHooks", "providerArtifacts", "createJourneyProviderArtifacts", "createJourney", "createJourneyFactory", "runtimeDefinition"]
|
|
7
7
|
}
|
package/dist/provider.d.cts
CHANGED
|
@@ -2,7 +2,7 @@ import React from "react";
|
|
|
2
2
|
import type { JourneyEqualityFn, JourneyJsonObject, JourneySelector, JourneyMachineWithPlugins, JourneyMachinePlugin } from "@rxova/journey-core";
|
|
3
3
|
import type { JourneyProviderProps } from "./types";
|
|
4
4
|
export declare const createJourneyProviderArtifacts: <TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>, TPlugins extends readonly JourneyMachinePlugin[] = []>(machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>, useJourneySelector: <TSelected>(selector: JourneySelector<TContext, TStepId, TSelected>, equalityFn?: JourneyEqualityFn<TSelected>) => TSelected) => {
|
|
5
|
-
JourneyProvider: ({ views,
|
|
5
|
+
JourneyProvider: ({ views, onError, disposeOnUnmount, children }: JourneyProviderProps<TStepId>) => import("react/jsx-runtime").JSX.Element;
|
|
6
6
|
StepRenderer: ({ fallback }: {
|
|
7
7
|
fallback?: React.ReactNode;
|
|
8
8
|
}) => import("react/jsx-runtime").JSX.Element;
|
package/dist/provider.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import React from "react";
|
|
|
2
2
|
import type { JourneyEqualityFn, JourneyJsonObject, JourneySelector, JourneyMachineWithPlugins, JourneyMachinePlugin } from "@rxova/journey-core";
|
|
3
3
|
import type { JourneyProviderProps } from "./types";
|
|
4
4
|
export declare const createJourneyProviderArtifacts: <TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>, TPlugins extends readonly JourneyMachinePlugin[] = []>(machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>, useJourneySelector: <TSelected>(selector: JourneySelector<TContext, TStepId, TSelected>, equalityFn?: JourneyEqualityFn<TSelected>) => TSelected) => {
|
|
5
|
-
JourneyProvider: ({ views,
|
|
5
|
+
JourneyProvider: ({ views, onError, disposeOnUnmount, children }: JourneyProviderProps<TStepId>) => import("react/jsx-runtime").JSX.Element;
|
|
6
6
|
StepRenderer: ({ fallback }: {
|
|
7
7
|
fallback?: React.ReactNode;
|
|
8
8
|
}) => import("react/jsx-runtime").JSX.Element;
|
package/dist/runtime-hooks.d.cts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type { JourneyComputed, JourneyEqualityFn, JourneyJsonObject, JourneyMachinePlugin, JourneyMachineWithPlugins, JourneyObservationEvent, JourneySelector, JourneySnapshot } from "@rxova/journey-core";
|
|
2
|
-
import type { JourneyApi } from "./types";
|
|
3
|
-
export declare const createJourneyHooks: <TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>, TPlugins extends readonly JourneyMachinePlugin[] = []>(machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>) => {
|
|
1
|
+
import type { JourneyBuilderCustomEventKey, JourneyComputed, JourneyEqualityFn, JourneyJsonObject, JourneyMachinePlugin, JourneyMachineWithPlugins, JourneyObservationEvent, JourneySelector, JourneySnapshot } from "@rxova/journey-core";
|
|
2
|
+
import type { JourneyApi, StepScopedJourneyApi } from "./types";
|
|
3
|
+
export declare const createJourneyHooks: <TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>, TPlugins extends readonly JourneyMachinePlugin[] = [], TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> = Record<TStepId, never>, TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never>(machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>) => {
|
|
4
4
|
useJourneySnapshot: () => JourneySnapshot<TContext, TStepId>;
|
|
5
5
|
useJourneyComputed: () => JourneyComputed<TStepId>;
|
|
6
6
|
useJourneySelector: <TSelected>(selector: JourneySelector<TContext, TStepId, TSelected>, equalityFn?: JourneyEqualityFn<TSelected>) => TSelected;
|
|
7
7
|
useJourneyApi: () => JourneyApi<TContext, TStepId, TEventMap, TStepMeta>;
|
|
8
|
+
useStepApi: <TStepKey extends TStepId>(stepId: TStepKey) => StepScopedJourneyApi<TContext, TStepId, TEventMap, Extract<TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType, keyof TEventMap & string>, TStepMeta>;
|
|
8
9
|
useJourneyEvent: (listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void) => void;
|
|
9
10
|
useJourneyStepLifecycle: (stepId: TStepId, callbacks: {
|
|
10
11
|
onEnter?: (args: {
|
package/dist/runtime-hooks.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type { JourneyComputed, JourneyEqualityFn, JourneyJsonObject, JourneyMachinePlugin, JourneyMachineWithPlugins, JourneyObservationEvent, JourneySelector, JourneySnapshot } from "@rxova/journey-core";
|
|
2
|
-
import type { JourneyApi } from "./types";
|
|
3
|
-
export declare const createJourneyHooks: <TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>, TPlugins extends readonly JourneyMachinePlugin[] = []>(machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>) => {
|
|
1
|
+
import type { JourneyBuilderCustomEventKey, JourneyComputed, JourneyEqualityFn, JourneyJsonObject, JourneyMachinePlugin, JourneyMachineWithPlugins, JourneyObservationEvent, JourneySelector, JourneySnapshot } from "@rxova/journey-core";
|
|
2
|
+
import type { JourneyApi, StepScopedJourneyApi } from "./types";
|
|
3
|
+
export declare const createJourneyHooks: <TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, THandlers extends Record<string, unknown> = Record<never, never>, TPlugins extends readonly JourneyMachinePlugin[] = [], TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> = Record<TStepId, never>, TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never>(machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>) => {
|
|
4
4
|
useJourneySnapshot: () => JourneySnapshot<TContext, TStepId>;
|
|
5
5
|
useJourneyComputed: () => JourneyComputed<TStepId>;
|
|
6
6
|
useJourneySelector: <TSelected>(selector: JourneySelector<TContext, TStepId, TSelected>, equalityFn?: JourneyEqualityFn<TSelected>) => TSelected;
|
|
7
7
|
useJourneyApi: () => JourneyApi<TContext, TStepId, TEventMap, TStepMeta>;
|
|
8
|
+
useStepApi: <TStepKey extends TStepId>(stepId: TStepKey) => StepScopedJourneyApi<TContext, TStepId, TEventMap, Extract<TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType, keyof TEventMap & string>, TStepMeta>;
|
|
8
9
|
useJourneyEvent: (listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void) => void;
|
|
9
10
|
useJourneyStepLifecycle: (stepId: TStepId, callbacks: {
|
|
10
11
|
onEnter?: (args: {
|
package/dist/types.d.cts
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
1
|
import type React from "react";
|
|
2
|
-
import type {
|
|
2
|
+
import type { JourneyBuilderDefinitionMetadata, JourneyBuilderCustomEventKey, JourneyComputed, JourneyDefaultEventType, JourneyDefinition, JourneyEqualityFn, JourneyJsonObject, JourneyMachinePlugin, JourneyMachineWithPlugins, JourneyObservationEvent, JourneyPayloadFor, JourneySelector, JourneySendEvent, JourneySendResult, JourneySnapshot } from "@rxova/journey-core";
|
|
3
3
|
export type JourneyDefaultEvent = JourneyDefaultEventType;
|
|
4
4
|
export type JourneyViews<TStepId extends string> = Record<TStepId, React.ComponentType>;
|
|
5
|
-
type
|
|
6
|
-
|
|
5
|
+
type JourneyCustomSendEventForKeys<TEventMap extends Record<string, unknown>, TAllowedEventType extends keyof TEventMap & string> = {
|
|
6
|
+
[TCurrentEventType in TAllowedEventType]: {
|
|
7
|
+
type: TCurrentEventType;
|
|
8
|
+
payload?: JourneyPayloadFor<TEventMap, TCurrentEventType>;
|
|
9
|
+
};
|
|
10
|
+
}[TAllowedEventType];
|
|
11
|
+
type JourneyTransitionsFromDefinition<TDefinition> = TDefinition extends {
|
|
12
|
+
transitions?: infer TTransitions;
|
|
13
|
+
} ? TTransitions : never;
|
|
14
|
+
type JourneyCustomEventKeysFromTransitionEntry<TEventMap extends Record<string, unknown>, TEntry> = Extract<keyof NonNullable<TEntry>, JourneyBuilderCustomEventKey<TEventMap>>;
|
|
15
|
+
type JourneyStepHandledCustomEventMapFromTransitions<TStepId extends string, TEventMap extends Record<string, unknown>, TTransitions> = {
|
|
16
|
+
[TCurrentStepId in TStepId]: JourneyCustomEventKeysFromTransitionEntry<TEventMap, TTransitions extends readonly unknown[] ? never : TTransitions extends Record<string, unknown> ? TCurrentStepId extends keyof TTransitions ? TTransitions[TCurrentStepId] : never : never>;
|
|
17
|
+
};
|
|
18
|
+
type JourneyGlobalHandledCustomEventTypeFromTransitions<TEventMap extends Record<string, unknown>, TTransitions> = JourneyCustomEventKeysFromTransitionEntry<TEventMap, TTransitions extends readonly unknown[] ? never : TTransitions extends {
|
|
19
|
+
global?: infer TGlobalTransitions;
|
|
20
|
+
} ? TGlobalTransitions : never>;
|
|
21
|
+
type JourneyStepHandledCustomEventMapFromDefinition<TDefinition, TStepId extends string, TEventMap extends Record<string, unknown>> = TDefinition extends JourneyBuilderDefinitionMetadata<TStepId, TEventMap, infer TStepHandledMap, infer TGlobalHandledCustomEventType> ? Extract<TStepHandledMap, Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>>> & (Extract<TGlobalHandledCustomEventType, JourneyBuilderCustomEventKey<TEventMap>> extends never ? unknown : unknown) : JourneyStepHandledCustomEventMapFromTransitions<TStepId, TEventMap, JourneyTransitionsFromDefinition<TDefinition>>;
|
|
22
|
+
type JourneyGlobalHandledCustomEventTypeFromDefinition<TDefinition, TStepId extends string, TEventMap extends Record<string, unknown>> = TDefinition extends JourneyBuilderDefinitionMetadata<TStepId, TEventMap, Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>>, infer TGlobalHandledCustomEventType> ? Extract<TGlobalHandledCustomEventType, JourneyBuilderCustomEventKey<TEventMap>> : JourneyGlobalHandledCustomEventTypeFromTransitions<TEventMap, JourneyTransitionsFromDefinition<TDefinition>>;
|
|
7
23
|
export type JourneyApi<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown> = {
|
|
8
|
-
|
|
24
|
+
startJourney: () => Promise<JourneySnapshot<TContext, TStepId>>;
|
|
9
25
|
send: (event: JourneySendEvent<TStepId, TEventMap>) => Promise<JourneySendResult<TContext, TStepId>>;
|
|
10
26
|
goToNextStep: () => Promise<JourneySendResult<TContext, TStepId>>;
|
|
11
27
|
goToStepById: (stepId: TStepId) => Promise<JourneySendResult<TContext, TStepId>>;
|
|
@@ -18,17 +34,14 @@ export type JourneyApi<TContext extends JourneyJsonObject, TStepId extends strin
|
|
|
18
34
|
getStepMeta: (stepId: TStepId) => TStepMeta | undefined;
|
|
19
35
|
resetJourney: () => Promise<JourneySnapshot<TContext, TStepId>>;
|
|
20
36
|
};
|
|
21
|
-
export type
|
|
22
|
-
|
|
23
|
-
|
|
37
|
+
export type StepScopedJourneyApi<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TAllowedEventType extends keyof TEventMap & string = never, TStepMeta = unknown> = Omit<JourneyApi<TContext, TStepId, TEventMap, TStepMeta>, "send"> & {
|
|
38
|
+
send: (event: JourneyCustomSendEventForKeys<TEventMap, TAllowedEventType>) => Promise<JourneySendResult<TContext, TStepId>>;
|
|
39
|
+
};
|
|
24
40
|
export type JourneyProviderErrorContext = {
|
|
25
41
|
phase: "start";
|
|
26
42
|
};
|
|
27
|
-
export type JourneyProviderProps<TStepId extends string
|
|
43
|
+
export type JourneyProviderProps<TStepId extends string> = {
|
|
28
44
|
views: JourneyViews<TStepId>;
|
|
29
|
-
onStart?: (event: JourneyStartEvent<TStepId, TEventMap, TStepMeta>) => void;
|
|
30
|
-
onComplete?: (event: JourneyCompleteEvent<TStepId, TEventMap, TStepMeta>) => void;
|
|
31
|
-
onTerminate?: (event: JourneyTerminateEvent<TStepId, TEventMap, TStepMeta>) => void;
|
|
32
45
|
onError?: (error: unknown, context: JourneyProviderErrorContext) => void;
|
|
33
46
|
disposeOnUnmount?: boolean;
|
|
34
47
|
children: React.ReactNode;
|
|
@@ -49,10 +62,19 @@ export type JourneyRuntime<TContext extends JourneyJsonObject, TStepId extends s
|
|
|
49
62
|
context: TContext;
|
|
50
63
|
}) => void;
|
|
51
64
|
}) => void;
|
|
52
|
-
JourneyProvider: React.ComponentType<JourneyProviderProps<TStepId
|
|
65
|
+
JourneyProvider: React.ComponentType<JourneyProviderProps<TStepId>>;
|
|
53
66
|
StepRenderer: React.ComponentType<{
|
|
54
67
|
fallback?: React.ReactNode;
|
|
55
68
|
}>;
|
|
56
69
|
};
|
|
70
|
+
export type JourneyRuntimeWithStepApi<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, TPlugins extends readonly JourneyMachinePlugin[] = [], THandlers extends Record<string, unknown> = Record<never, never>, TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> = Record<TStepId, never>, TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never> = JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> & {
|
|
71
|
+
useStepApi: <TStepKey extends TStepId>(stepId: TStepKey) => StepScopedJourneyApi<TContext, TStepId, TEventMap, Extract<TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType, keyof TEventMap & string>, TStepMeta>;
|
|
72
|
+
};
|
|
73
|
+
export type JourneyBuilderRuntime<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, TPlugins extends readonly JourneyMachinePlugin[] = [], THandlers extends Record<string, unknown> = Record<never, never>, TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> = Record<TStepId, never>, TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never> = JourneyRuntimeWithStepApi<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers, TStepHandledCustomEventMap, TGlobalHandledCustomEventType>;
|
|
74
|
+
export type JourneyBuilderRuntimeFactory<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, TPlugins extends readonly JourneyMachinePlugin[] = [], THandlers extends Record<string, unknown> = Record<never, never>, TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> = Record<TStepId, never>, TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never> = () => JourneyBuilderRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers, TStepHandledCustomEventMap, TGlobalHandledCustomEventType>;
|
|
75
|
+
export type JourneyRuntimeFromDefinition<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []> = TDefinition extends JourneyDefinition<infer TContext, infer TStepId, infer TEventMap, infer TStepMeta, infer THandlers> ? JourneyRuntimeWithStepApi<Extract<TContext, JourneyJsonObject>, Extract<TStepId, string>, Extract<TEventMap, Record<string, unknown>>, TStepMeta, TPlugins, Extract<THandlers, Record<string, unknown>>, JourneyStepHandledCustomEventMapFromDefinition<TDefinition, Extract<TStepId, string>, Extract<TEventMap, Record<string, unknown>>>, JourneyGlobalHandledCustomEventTypeFromDefinition<TDefinition, Extract<TStepId, string>, Extract<TEventMap, Record<string, unknown>>>> : never;
|
|
76
|
+
export type JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []> = () => JourneyRuntimeFromDefinition<TDefinition, TPlugins>;
|
|
77
|
+
export type JourneyBuilderRuntimeFromDefinition<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []> = JourneyRuntimeFromDefinition<TDefinition, TPlugins>;
|
|
78
|
+
export type JourneyBuilderRuntimeFactoryFromDefinition<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []> = JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;
|
|
57
79
|
export type JourneyRuntimeFactory<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, TPlugins extends readonly JourneyMachinePlugin[] = [], THandlers extends Record<string, unknown> = Record<never, never>> = () => JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers>;
|
|
58
80
|
export {};
|
package/dist/types.d.ts
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
1
|
import type React from "react";
|
|
2
|
-
import type {
|
|
2
|
+
import type { JourneyBuilderDefinitionMetadata, JourneyBuilderCustomEventKey, JourneyComputed, JourneyDefaultEventType, JourneyDefinition, JourneyEqualityFn, JourneyJsonObject, JourneyMachinePlugin, JourneyMachineWithPlugins, JourneyObservationEvent, JourneyPayloadFor, JourneySelector, JourneySendEvent, JourneySendResult, JourneySnapshot } from "@rxova/journey-core";
|
|
3
3
|
export type JourneyDefaultEvent = JourneyDefaultEventType;
|
|
4
4
|
export type JourneyViews<TStepId extends string> = Record<TStepId, React.ComponentType>;
|
|
5
|
-
type
|
|
6
|
-
|
|
5
|
+
type JourneyCustomSendEventForKeys<TEventMap extends Record<string, unknown>, TAllowedEventType extends keyof TEventMap & string> = {
|
|
6
|
+
[TCurrentEventType in TAllowedEventType]: {
|
|
7
|
+
type: TCurrentEventType;
|
|
8
|
+
payload?: JourneyPayloadFor<TEventMap, TCurrentEventType>;
|
|
9
|
+
};
|
|
10
|
+
}[TAllowedEventType];
|
|
11
|
+
type JourneyTransitionsFromDefinition<TDefinition> = TDefinition extends {
|
|
12
|
+
transitions?: infer TTransitions;
|
|
13
|
+
} ? TTransitions : never;
|
|
14
|
+
type JourneyCustomEventKeysFromTransitionEntry<TEventMap extends Record<string, unknown>, TEntry> = Extract<keyof NonNullable<TEntry>, JourneyBuilderCustomEventKey<TEventMap>>;
|
|
15
|
+
type JourneyStepHandledCustomEventMapFromTransitions<TStepId extends string, TEventMap extends Record<string, unknown>, TTransitions> = {
|
|
16
|
+
[TCurrentStepId in TStepId]: JourneyCustomEventKeysFromTransitionEntry<TEventMap, TTransitions extends readonly unknown[] ? never : TTransitions extends Record<string, unknown> ? TCurrentStepId extends keyof TTransitions ? TTransitions[TCurrentStepId] : never : never>;
|
|
17
|
+
};
|
|
18
|
+
type JourneyGlobalHandledCustomEventTypeFromTransitions<TEventMap extends Record<string, unknown>, TTransitions> = JourneyCustomEventKeysFromTransitionEntry<TEventMap, TTransitions extends readonly unknown[] ? never : TTransitions extends {
|
|
19
|
+
global?: infer TGlobalTransitions;
|
|
20
|
+
} ? TGlobalTransitions : never>;
|
|
21
|
+
type JourneyStepHandledCustomEventMapFromDefinition<TDefinition, TStepId extends string, TEventMap extends Record<string, unknown>> = TDefinition extends JourneyBuilderDefinitionMetadata<TStepId, TEventMap, infer TStepHandledMap, infer TGlobalHandledCustomEventType> ? Extract<TStepHandledMap, Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>>> & (Extract<TGlobalHandledCustomEventType, JourneyBuilderCustomEventKey<TEventMap>> extends never ? unknown : unknown) : JourneyStepHandledCustomEventMapFromTransitions<TStepId, TEventMap, JourneyTransitionsFromDefinition<TDefinition>>;
|
|
22
|
+
type JourneyGlobalHandledCustomEventTypeFromDefinition<TDefinition, TStepId extends string, TEventMap extends Record<string, unknown>> = TDefinition extends JourneyBuilderDefinitionMetadata<TStepId, TEventMap, Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>>, infer TGlobalHandledCustomEventType> ? Extract<TGlobalHandledCustomEventType, JourneyBuilderCustomEventKey<TEventMap>> : JourneyGlobalHandledCustomEventTypeFromTransitions<TEventMap, JourneyTransitionsFromDefinition<TDefinition>>;
|
|
7
23
|
export type JourneyApi<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown> = {
|
|
8
|
-
|
|
24
|
+
startJourney: () => Promise<JourneySnapshot<TContext, TStepId>>;
|
|
9
25
|
send: (event: JourneySendEvent<TStepId, TEventMap>) => Promise<JourneySendResult<TContext, TStepId>>;
|
|
10
26
|
goToNextStep: () => Promise<JourneySendResult<TContext, TStepId>>;
|
|
11
27
|
goToStepById: (stepId: TStepId) => Promise<JourneySendResult<TContext, TStepId>>;
|
|
@@ -18,17 +34,14 @@ export type JourneyApi<TContext extends JourneyJsonObject, TStepId extends strin
|
|
|
18
34
|
getStepMeta: (stepId: TStepId) => TStepMeta | undefined;
|
|
19
35
|
resetJourney: () => Promise<JourneySnapshot<TContext, TStepId>>;
|
|
20
36
|
};
|
|
21
|
-
export type
|
|
22
|
-
|
|
23
|
-
|
|
37
|
+
export type StepScopedJourneyApi<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TAllowedEventType extends keyof TEventMap & string = never, TStepMeta = unknown> = Omit<JourneyApi<TContext, TStepId, TEventMap, TStepMeta>, "send"> & {
|
|
38
|
+
send: (event: JourneyCustomSendEventForKeys<TEventMap, TAllowedEventType>) => Promise<JourneySendResult<TContext, TStepId>>;
|
|
39
|
+
};
|
|
24
40
|
export type JourneyProviderErrorContext = {
|
|
25
41
|
phase: "start";
|
|
26
42
|
};
|
|
27
|
-
export type JourneyProviderProps<TStepId extends string
|
|
43
|
+
export type JourneyProviderProps<TStepId extends string> = {
|
|
28
44
|
views: JourneyViews<TStepId>;
|
|
29
|
-
onStart?: (event: JourneyStartEvent<TStepId, TEventMap, TStepMeta>) => void;
|
|
30
|
-
onComplete?: (event: JourneyCompleteEvent<TStepId, TEventMap, TStepMeta>) => void;
|
|
31
|
-
onTerminate?: (event: JourneyTerminateEvent<TStepId, TEventMap, TStepMeta>) => void;
|
|
32
45
|
onError?: (error: unknown, context: JourneyProviderErrorContext) => void;
|
|
33
46
|
disposeOnUnmount?: boolean;
|
|
34
47
|
children: React.ReactNode;
|
|
@@ -49,10 +62,19 @@ export type JourneyRuntime<TContext extends JourneyJsonObject, TStepId extends s
|
|
|
49
62
|
context: TContext;
|
|
50
63
|
}) => void;
|
|
51
64
|
}) => void;
|
|
52
|
-
JourneyProvider: React.ComponentType<JourneyProviderProps<TStepId
|
|
65
|
+
JourneyProvider: React.ComponentType<JourneyProviderProps<TStepId>>;
|
|
53
66
|
StepRenderer: React.ComponentType<{
|
|
54
67
|
fallback?: React.ReactNode;
|
|
55
68
|
}>;
|
|
56
69
|
};
|
|
70
|
+
export type JourneyRuntimeWithStepApi<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, TPlugins extends readonly JourneyMachinePlugin[] = [], THandlers extends Record<string, unknown> = Record<never, never>, TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> = Record<TStepId, never>, TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never> = JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> & {
|
|
71
|
+
useStepApi: <TStepKey extends TStepId>(stepId: TStepKey) => StepScopedJourneyApi<TContext, TStepId, TEventMap, Extract<TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType, keyof TEventMap & string>, TStepMeta>;
|
|
72
|
+
};
|
|
73
|
+
export type JourneyBuilderRuntime<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, TPlugins extends readonly JourneyMachinePlugin[] = [], THandlers extends Record<string, unknown> = Record<never, never>, TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> = Record<TStepId, never>, TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never> = JourneyRuntimeWithStepApi<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers, TStepHandledCustomEventMap, TGlobalHandledCustomEventType>;
|
|
74
|
+
export type JourneyBuilderRuntimeFactory<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, TPlugins extends readonly JourneyMachinePlugin[] = [], THandlers extends Record<string, unknown> = Record<never, never>, TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> = Record<TStepId, never>, TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never> = () => JourneyBuilderRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers, TStepHandledCustomEventMap, TGlobalHandledCustomEventType>;
|
|
75
|
+
export type JourneyRuntimeFromDefinition<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []> = TDefinition extends JourneyDefinition<infer TContext, infer TStepId, infer TEventMap, infer TStepMeta, infer THandlers> ? JourneyRuntimeWithStepApi<Extract<TContext, JourneyJsonObject>, Extract<TStepId, string>, Extract<TEventMap, Record<string, unknown>>, TStepMeta, TPlugins, Extract<THandlers, Record<string, unknown>>, JourneyStepHandledCustomEventMapFromDefinition<TDefinition, Extract<TStepId, string>, Extract<TEventMap, Record<string, unknown>>>, JourneyGlobalHandledCustomEventTypeFromDefinition<TDefinition, Extract<TStepId, string>, Extract<TEventMap, Record<string, unknown>>>> : never;
|
|
76
|
+
export type JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []> = () => JourneyRuntimeFromDefinition<TDefinition, TPlugins>;
|
|
77
|
+
export type JourneyBuilderRuntimeFromDefinition<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []> = JourneyRuntimeFromDefinition<TDefinition, TPlugins>;
|
|
78
|
+
export type JourneyBuilderRuntimeFactoryFromDefinition<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []> = JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;
|
|
57
79
|
export type JourneyRuntimeFactory<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown, TPlugins extends readonly JourneyMachinePlugin[] = [], THandlers extends Record<string, unknown> = Record<never, never>> = () => JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers>;
|
|
58
80
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rxova/journey-react",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.2",
|
|
4
4
|
"description": "React bindings for journey.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"sideEffects": false,
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"react": ">=18.2.0",
|
|
59
|
-
"@rxova/journey-core": "^1.0.0-rc.
|
|
59
|
+
"@rxova/journey-core": "^1.0.0-rc.2"
|
|
60
60
|
},
|
|
61
61
|
"peerDependenciesMeta": {
|
|
62
62
|
"@rxova/journey-core": {
|