@rxova/journey-react 0.7.0 → 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 +101 -170
- package/dist/client.cjs +2 -0
- package/dist/client.cjs.map +7 -0
- package/dist/client.d.cts +1 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +2 -0
- package/dist/client.js.map +7 -0
- package/dist/createJourney.d.cts +15 -0
- package/dist/createJourney.d.ts +15 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.cts +3 -4
- package/dist/index.d.ts +3 -4
- package/dist/index.js +1 -1
- package/dist/index.js.map +4 -4
- package/dist/provider.d.cts +9 -0
- package/dist/provider.d.ts +9 -0
- package/dist/runtime-hooks.d.cts +18 -0
- package/dist/runtime-hooks.d.ts +18 -0
- package/dist/types.d.cts +80 -0
- package/dist/types.d.ts +69 -45
- package/package.json +17 -8
- package/dist/bindings/Provider.d.ts +0 -8
- package/dist/bindings/StepRenderer.d.ts +0 -13
- package/dist/bindings/index.d.ts +0 -6
- package/dist/bindings/useJourneyApi.d.ts +0 -16
- package/dist/bindings/useJourneyEvent.d.ts +0 -5
- package/dist/bindings/useJourneyMachine.d.ts +0 -4
- package/dist/bindings/useJourneySelector.d.ts +0 -5
- package/dist/bindings/useJourneySnapshot.d.ts +0 -5
package/dist/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/index.ts", "../src/
|
|
4
|
-
"sourcesContent": ["// Mark this entry point as a Next.js App Router client module so it can be used in client components.\n\"use client\";\n\nexport { createJourneyBindings } from \"./bindings\";\nexport {\n createTransitions,\n tx,\n JOURNEY_STATUS,\n JOURNEY_EVENT,\n JOURNEY_ASYNC_PHASE,\n JOURNEY_WILDCARD\n} from \"@rxova/journey-core\";\nexport type { JourneyDefinition, JourneySendResult } from \"@rxova/journey-core\";\nexport type {\n JourneyApi,\n JourneyBindings,\n JourneyBindingsProviderProps,\n JourneyEventType,\n JourneyReactDefinition,\n JourneyReactEventPayloadMap,\n JourneyReactStep,\n JourneyStoreValue\n} from \"./types\";\n", "import React from \"react\";\n\nimport type {\n JourneyBindings,\n JourneyReactDefinition,\n JourneyReactEventPayloadMap,\n JourneyStoreValue\n} from \"../types\";\nimport { createProvider } from \"./Provider\";\nimport { createStepRenderer } from \"./StepRenderer\";\nimport { createUseJourneyApi } from \"./useJourneyApi\";\nimport { createUseJourneyEvent } from \"./useJourneyEvent\";\nimport { createUseJourneyMachine } from \"./useJourneyMachine\";\nimport { createUseJourneySelector } from \"./useJourneySelector\";\nimport { createUseJourneySnapshot } from \"./useJourneySnapshot\";\n\n/**\n * Creates a typed React integration bundle (Provider, StepRenderer, and hooks)\n * bound to a specific journey definition.\n */\nexport const createJourneyBindings = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n boundJourney: JourneyReactDefinition<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n): JourneyBindings<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta> => {\n const JourneyContext = React.createContext<JourneyStoreValue<\n TContext,\n TStepId,\n TCustomEvent,\n TEventPayloadMap,\n TStepMeta\n > | null>(null);\n\n const useJourneyStore = (hookName = \"hook\") => {\n const value = React.useContext(JourneyContext);\n if (!value) {\n throw new Error(`${hookName} must be used within bindings.Provider.`);\n }\n return value;\n };\n\n const useJourneySnapshot = createUseJourneySnapshot(useJourneyStore);\n const useJourneySelector = createUseJourneySelector(useJourneyStore);\n const useJourneyMachine = createUseJourneyMachine(useJourneyStore);\n const useJourneyEvent = createUseJourneyEvent(useJourneyStore);\n const useJourneyApi = createUseJourneyApi(useJourneyStore);\n\n const Provider = createProvider({\n JourneyContext,\n boundJourney\n });\n\n const StepRenderer = createStepRenderer({\n useJourneySnapshot,\n useJourneyStore\n });\n\n return {\n Provider,\n StepRenderer,\n useJourneyApi,\n useJourneyEvent,\n useJourneyMachine,\n useJourneySelector,\n useJourneySnapshot\n };\n};\n", "import React from \"react\";\n\nimport { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyBindingsProviderProps,\n JourneyReactDefinition,\n JourneyReactEventPayloadMap,\n JourneyStoreValue\n} from \"../types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\ntype ProviderFactoryProps<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = {\n JourneyContext: React.Context<JourneyStoreValue<\n TContext,\n TStepId,\n TCustomEvent,\n TEventPayloadMap,\n TStepMeta\n > | null>;\n boundJourney: JourneyReactDefinition<\n TContext,\n TStepId,\n TCustomEvent,\n TEventPayloadMap,\n TStepMeta\n >;\n};\n\nexport const createProvider = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>({\n JourneyContext,\n boundJourney\n}: ProviderFactoryProps<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>) => {\n const Provider = ({\n journey,\n machine,\n persistence,\n completeOnNoNextStep,\n resetOnJourneyChange = false,\n resetOnPersistenceChange = false,\n onStart,\n onComplete,\n onTerminate,\n children\n }: JourneyBindingsProviderProps<\n TContext,\n TStepId,\n TCustomEvent,\n TEventPayloadMap,\n TStepMeta\n >) => {\n const incomingJourney = journey ?? boundJourney;\n const internalMachineRef = React.useRef<\n | JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>[\"machine\"]\n | null\n >(null);\n const journeyRef = React.useRef(incomingJourney);\n const persistenceRef = React.useRef(persistence);\n const completeOnNoNextStepRef = React.useRef(completeOnNoNextStep);\n const onStartRef = React.useRef(onStart);\n const onCompleteRef = React.useRef(onComplete);\n const onTerminateRef = React.useRef(onTerminate);\n const [, forceUpdate] = React.useReducer((count: number) => count + 1, 0);\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\n if (!machine && !internalMachineRef.current) {\n const options =\n persistence !== undefined || completeOnNoNextStep !== undefined\n ? {\n ...(persistence !== undefined ? { persistence } : {}),\n ...(completeOnNoNextStep !== undefined ? { completeOnNoNextStep } : {})\n }\n : undefined;\n internalMachineRef.current = createJourneyMachine(incomingJourney, options);\n journeyRef.current = incomingJourney;\n persistenceRef.current = persistence;\n completeOnNoNextStepRef.current = completeOnNoNextStep;\n }\n\n const shouldResetJourney =\n !machine && resetOnJourneyChange && journeyRef.current !== incomingJourney;\n const shouldResetPersistence =\n !machine && resetOnPersistenceChange && persistenceRef.current !== persistence;\n const shouldResetNextCompletion =\n !machine && completeOnNoNextStepRef.current !== completeOnNoNextStep;\n\n useSafeLayoutEffect(() => {\n if (\n machine ||\n (!shouldResetJourney && !shouldResetPersistence && !shouldResetNextCompletion)\n ) {\n return;\n }\n\n const previousInternalMachine = internalMachineRef.current;\n const options =\n persistence !== undefined || completeOnNoNextStep !== undefined\n ? {\n ...(persistence !== undefined ? { persistence } : {}),\n ...(completeOnNoNextStep !== undefined ? { completeOnNoNextStep } : {})\n }\n : undefined;\n const nextInternalMachine = createJourneyMachine(incomingJourney, options);\n internalMachineRef.current = nextInternalMachine;\n journeyRef.current = incomingJourney;\n persistenceRef.current = persistence;\n completeOnNoNextStepRef.current = completeOnNoNextStep;\n\n if (previousInternalMachine && previousInternalMachine !== nextInternalMachine) {\n previousInternalMachine.dispose();\n }\n\n forceUpdate();\n }, [\n completeOnNoNextStep,\n incomingJourney,\n machine,\n persistence,\n shouldResetJourney,\n shouldResetPersistence,\n shouldResetNextCompletion\n ]);\n\n const resolvedMachine = machine ?? internalMachineRef.current!;\n const resolvedJourney = machine ? incomingJourney : journeyRef.current;\n const value = React.useMemo<\n JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n >(\n () => ({\n machine: resolvedMachine,\n journey: resolvedJourney\n }),\n [resolvedMachine, resolvedJourney]\n );\n\n React.useEffect(() => {\n if (!hasOnStart && !hasOnComplete && !hasOnTerminate) {\n return;\n }\n\n const unsubStart = hasOnStart\n ? resolvedMachine.subscribeStart((event) => {\n onStartRef.current?.(event);\n })\n : undefined;\n\n const unsubComplete = hasOnComplete\n ? resolvedMachine.subscribeComplete((event) => {\n onCompleteRef.current?.(event);\n })\n : undefined;\n\n const unsubTerminate = hasOnTerminate\n ? resolvedMachine.subscribeTerminate((event) => {\n onTerminateRef.current?.(event);\n })\n : undefined;\n\n return () => {\n unsubStart?.();\n unsubComplete?.();\n unsubTerminate?.();\n };\n }, [hasOnStart, hasOnComplete, hasOnTerminate, resolvedMachine]);\n\n React.useEffect(() => {\n return () => {\n if (machine || !internalMachineRef.current) {\n return;\n }\n internalMachineRef.current.dispose();\n internalMachineRef.current = null;\n };\n }, [machine]);\n\n return <JourneyContext.Provider value={value}>{children}</JourneyContext.Provider>;\n };\n\n return Provider;\n};\n", "import React from \"react\";\n\nimport type { JourneySnapshot } from \"@rxova/journey-core\";\nimport type { JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\ntype UseJourneySnapshot<\n TContext,\n TStepId extends string,\n TStepMeta = unknown\n> = () => JourneySnapshot<TContext, TStepId, TStepMeta>;\n\ntype StepRendererFactoryProps<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = {\n useJourneySnapshot: UseJourneySnapshot<TContext, TStepId, TStepMeta>;\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n};\n\nexport const createStepRenderer = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>({\n useJourneySnapshot,\n useJourneyStore\n}: StepRendererFactoryProps<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>) => {\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const snapshot = useJourneySnapshot();\n const { journey } = useJourneyStore(\"StepRenderer\");\n\n const StepComponent = journey.steps[snapshot.currentStepId]?.component;\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n return <StepComponent key={snapshot.currentStepId} />;\n };\n\n return StepRenderer;\n};\n", "import React from \"react\";\n\nimport type { JourneyPayloadFor, JourneySendEvent, JourneySendResult } from \"@rxova/journey-core\";\nimport type { JourneyEventType, JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\nexport const createUseJourneyApi = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n) => {\n return () => {\n const { machine } = useJourneyStore(\"useJourneyApi\");\n\n return React.useMemo(\n () => ({\n send: async (\n event: JourneySendEvent<TStepId, JourneyEventType<TCustomEvent>, TEventPayloadMap>\n ): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.send(event);\n },\n goToNextStep: async (): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.goToNextStep();\n },\n terminateJourney: async (\n payload?: JourneyPayloadFor<\n JourneyEventType<TCustomEvent>,\n TEventPayloadMap,\n \"terminateJourney\"\n >\n ): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.terminateJourney(payload);\n },\n completeJourney: async (\n payload?: JourneyPayloadFor<\n JourneyEventType<TCustomEvent>,\n TEventPayloadMap,\n \"completeJourney\"\n >\n ): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.completeJourney(payload);\n },\n goToPreviousStep: async (\n steps?: number\n ): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.goToPreviousStep(steps);\n },\n goToLastVisitedStep: async (): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.goToLastVisitedStep();\n },\n clearStepError: (stepId?: TStepId) => {\n machine.clearStepError(stepId);\n },\n updateContext: (updater: (context: TContext) => TContext) => {\n machine.updateContext(updater);\n },\n updateStepMetadata: (stepId: TStepId, updater: (metadata: TStepMeta) => TStepMeta) => {\n machine.updateStepMetadata(stepId, updater);\n },\n resetJourney: () => {\n machine.resetMachine();\n }\n }),\n [machine]\n );\n };\n};\n", "import React from \"react\";\n\nimport type { JourneyObservationEvent } from \"@rxova/journey-core\";\nimport type { JourneyEventType, JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\nexport const createUseJourneyEvent = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n) => {\n return (\n listener: (\n event: JourneyObservationEvent<\n TStepId,\n JourneyEventType<TCustomEvent>,\n TEventPayloadMap,\n TStepMeta\n >\n ) => void\n ) => {\n const { machine } = useJourneyStore(\"useJourneyEvent\");\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n React.useEffect(() => {\n return machine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [machine]);\n };\n};\n", "import type { JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\nexport const createUseJourneyMachine = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n) => {\n return () => {\n return useJourneyStore(\"useJourneyMachine\").machine;\n };\n};\n", "import React from \"react\";\n\nimport type { JourneyEqualityFn, JourneySelector, JourneySnapshot } from \"@rxova/journey-core\";\nimport type { JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\ntype SelectorCache<TContext, TStepId extends string, TStepMeta, TSelected> = {\n snapshot: JourneySnapshot<TContext, TStepId, TStepMeta>;\n selected: TSelected;\n selector: JourneySelector<TContext, TStepId, TStepMeta, TSelected>;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nexport const createUseJourneySelector = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n) => {\n return <TSelected>(\n selector: JourneySelector<TContext, TStepId, TStepMeta, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const { machine } = useJourneyStore(\"useJourneySelector\");\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TStepMeta, TSelected> | null>(\n null\n );\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = machine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n cached.selector === selector &&\n cached.isEqual === isEqual &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (cached && cached.selector === selector && cached.isEqual === isEqual) {\n if (cached.isEqual(cached.selected, nextSelected)) {\n cacheRef.current = {\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n }\n\n cacheRef.current = {\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [machine, selector, isEqual]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n machine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [machine, selector, isEqual]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n};\n", "import React from \"react\";\n\nimport type { JourneySnapshot } from \"@rxova/journey-core\";\nimport type { JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\nexport const createUseJourneySnapshot = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n) => {\n return (): JourneySnapshot<TContext, TStepId, TStepMeta> => {\n const { machine } = useJourneyStore(\"useJourneySnapshot\");\n const getSnapshot = React.useCallback(() => machine.getSnapshot(), [machine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => machine.subscribe(onStoreChange),\n [machine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n};\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["index_exports", "__export", "
|
|
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 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,eAAAC,EACA,QAAAC,EACA,iBAAAC,CACF,IAIM,CACJ,IAAMC,EAAa,EAAAhB,QAAM,OAAOc,CAAO,EACjCG,EAAsB,EAAAjB,QAAM,OAAwD,IAAI,EAC9FgB,EAAW,QAAUF,EAErBf,EAAoB,IAAM,CACpBkB,EAAoB,UAAY,OA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,CACL,aAAcI,EAAe,aAC7B,KAAMA,EAAe,KACrB,aAAcA,EAAe,aAC7B,aAAcA,EAAe,aAC7B,iBAAkBA,EAAe,iBACjC,gBAAiBA,EAAe,gBAChC,iBAAkBA,EAAe,iBACjC,oBAAqBA,EAAe,oBACpC,eAAgBA,EAAe,eAC/B,cAAeA,EAAe,cAC9B,YAAaA,EAAe,YAC5B,aAAc,IAAMA,EAAe,aAAa,CAClD,GACA,CAACA,CAAc,CACjB,CAUF,EAQE,gBAAAgB,EACA,wBAtF8B,CAC9BI,EACAC,IAIS,CACTL,EAAiBG,GAAU,CACrBA,EAAM,OAAS,cAAgBA,EAAM,SAAWC,EAClDC,EAAU,UAAU,CAAE,QAASvB,EAAQ,YAAY,EAAE,OAAQ,CAAC,EACrDqB,EAAM,OAAS,aAAeA,EAAM,SAAWC,GACxDC,EAAU,UAAU,CAAE,QAASvB,EAAQ,YAAY,EAAE,OAAQ,CAAC,CAElE,CAAC,CACH,CAyEA,CACF,EFhNA,IAAMwB,EAA8B,CAQlCC,EACAC,IACiF,CAEjF,IAAMC,KAAU,wBAOdF,EARqBC,CAQK,EAQtBE,EAAQC,EACZF,CACF,EACMG,EAAoBC,EAOxBJ,EAASC,EAAM,kBAAkB,EAEnC,MAAO,CACL,QAAAD,EACA,QAAS,IAAMA,EAAQ,QAAQ,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": ["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,4 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export type {
|
|
4
|
-
export type { JourneyApi, JourneyBindings, JourneyBindingsProviderProps, JourneyEventType, JourneyReactDefinition, JourneyReactEventPayloadMap, JourneyReactStep, JourneyStoreValue } from "./types";
|
|
1
|
+
export { createJourney, createJourneyFactory } from "./createJourney";
|
|
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,4 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export type {
|
|
4
|
-
export type { JourneyApi, JourneyBindings, JourneyBindingsProviderProps, JourneyEventType, JourneyReactDefinition, JourneyReactEventPayloadMap, JourneyReactStep, JourneyStoreValue } from "./types";
|
|
1
|
+
export { createJourney, createJourneyFactory } from "./createJourney";
|
|
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
|
-
|
|
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
|
-
"sources": ["../src/
|
|
4
|
-
"sourcesContent": ["import React from \"react\";\n\nimport type {\n JourneyBindings,\n JourneyReactDefinition,\n JourneyReactEventPayloadMap,\n JourneyStoreValue\n} from \"../types\";\nimport { createProvider } from \"./Provider\";\nimport { createStepRenderer } from \"./StepRenderer\";\nimport { createUseJourneyApi } from \"./useJourneyApi\";\nimport { createUseJourneyEvent } from \"./useJourneyEvent\";\nimport { createUseJourneyMachine } from \"./useJourneyMachine\";\nimport { createUseJourneySelector } from \"./useJourneySelector\";\nimport { createUseJourneySnapshot } from \"./useJourneySnapshot\";\n\n/**\n * Creates a typed React integration bundle (Provider, StepRenderer, and hooks)\n * bound to a specific journey definition.\n */\nexport const createJourneyBindings = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n boundJourney: JourneyReactDefinition<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n): JourneyBindings<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta> => {\n const JourneyContext = React.createContext<JourneyStoreValue<\n TContext,\n TStepId,\n TCustomEvent,\n TEventPayloadMap,\n TStepMeta\n > | null>(null);\n\n const useJourneyStore = (hookName = \"hook\") => {\n const value = React.useContext(JourneyContext);\n if (!value) {\n throw new Error(`${hookName} must be used within bindings.Provider.`);\n }\n return value;\n };\n\n const useJourneySnapshot = createUseJourneySnapshot(useJourneyStore);\n const useJourneySelector = createUseJourneySelector(useJourneyStore);\n const useJourneyMachine = createUseJourneyMachine(useJourneyStore);\n const useJourneyEvent = createUseJourneyEvent(useJourneyStore);\n const useJourneyApi = createUseJourneyApi(useJourneyStore);\n\n const Provider = createProvider({\n JourneyContext,\n boundJourney\n });\n\n const StepRenderer = createStepRenderer({\n useJourneySnapshot,\n useJourneyStore\n });\n\n return {\n Provider,\n StepRenderer,\n useJourneyApi,\n useJourneyEvent,\n useJourneyMachine,\n useJourneySelector,\n useJourneySnapshot\n };\n};\n", "import React from \"react\";\n\nimport { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyBindingsProviderProps,\n JourneyReactDefinition,\n JourneyReactEventPayloadMap,\n JourneyStoreValue\n} from \"../types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\ntype ProviderFactoryProps<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = {\n JourneyContext: React.Context<JourneyStoreValue<\n TContext,\n TStepId,\n TCustomEvent,\n TEventPayloadMap,\n TStepMeta\n > | null>;\n boundJourney: JourneyReactDefinition<\n TContext,\n TStepId,\n TCustomEvent,\n TEventPayloadMap,\n TStepMeta\n >;\n};\n\nexport const createProvider = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>({\n JourneyContext,\n boundJourney\n}: ProviderFactoryProps<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>) => {\n const Provider = ({\n journey,\n machine,\n persistence,\n completeOnNoNextStep,\n resetOnJourneyChange = false,\n resetOnPersistenceChange = false,\n onStart,\n onComplete,\n onTerminate,\n children\n }: JourneyBindingsProviderProps<\n TContext,\n TStepId,\n TCustomEvent,\n TEventPayloadMap,\n TStepMeta\n >) => {\n const incomingJourney = journey ?? boundJourney;\n const internalMachineRef = React.useRef<\n | JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>[\"machine\"]\n | null\n >(null);\n const journeyRef = React.useRef(incomingJourney);\n const persistenceRef = React.useRef(persistence);\n const completeOnNoNextStepRef = React.useRef(completeOnNoNextStep);\n const onStartRef = React.useRef(onStart);\n const onCompleteRef = React.useRef(onComplete);\n const onTerminateRef = React.useRef(onTerminate);\n const [, forceUpdate] = React.useReducer((count: number) => count + 1, 0);\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\n if (!machine && !internalMachineRef.current) {\n const options =\n persistence !== undefined || completeOnNoNextStep !== undefined\n ? {\n ...(persistence !== undefined ? { persistence } : {}),\n ...(completeOnNoNextStep !== undefined ? { completeOnNoNextStep } : {})\n }\n : undefined;\n internalMachineRef.current = createJourneyMachine(incomingJourney, options);\n journeyRef.current = incomingJourney;\n persistenceRef.current = persistence;\n completeOnNoNextStepRef.current = completeOnNoNextStep;\n }\n\n const shouldResetJourney =\n !machine && resetOnJourneyChange && journeyRef.current !== incomingJourney;\n const shouldResetPersistence =\n !machine && resetOnPersistenceChange && persistenceRef.current !== persistence;\n const shouldResetNextCompletion =\n !machine && completeOnNoNextStepRef.current !== completeOnNoNextStep;\n\n useSafeLayoutEffect(() => {\n if (\n machine ||\n (!shouldResetJourney && !shouldResetPersistence && !shouldResetNextCompletion)\n ) {\n return;\n }\n\n const previousInternalMachine = internalMachineRef.current;\n const options =\n persistence !== undefined || completeOnNoNextStep !== undefined\n ? {\n ...(persistence !== undefined ? { persistence } : {}),\n ...(completeOnNoNextStep !== undefined ? { completeOnNoNextStep } : {})\n }\n : undefined;\n const nextInternalMachine = createJourneyMachine(incomingJourney, options);\n internalMachineRef.current = nextInternalMachine;\n journeyRef.current = incomingJourney;\n persistenceRef.current = persistence;\n completeOnNoNextStepRef.current = completeOnNoNextStep;\n\n if (previousInternalMachine && previousInternalMachine !== nextInternalMachine) {\n previousInternalMachine.dispose();\n }\n\n forceUpdate();\n }, [\n completeOnNoNextStep,\n incomingJourney,\n machine,\n persistence,\n shouldResetJourney,\n shouldResetPersistence,\n shouldResetNextCompletion\n ]);\n\n const resolvedMachine = machine ?? internalMachineRef.current!;\n const resolvedJourney = machine ? incomingJourney : journeyRef.current;\n const value = React.useMemo<\n JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n >(\n () => ({\n machine: resolvedMachine,\n journey: resolvedJourney\n }),\n [resolvedMachine, resolvedJourney]\n );\n\n React.useEffect(() => {\n if (!hasOnStart && !hasOnComplete && !hasOnTerminate) {\n return;\n }\n\n const unsubStart = hasOnStart\n ? resolvedMachine.subscribeStart((event) => {\n onStartRef.current?.(event);\n })\n : undefined;\n\n const unsubComplete = hasOnComplete\n ? resolvedMachine.subscribeComplete((event) => {\n onCompleteRef.current?.(event);\n })\n : undefined;\n\n const unsubTerminate = hasOnTerminate\n ? resolvedMachine.subscribeTerminate((event) => {\n onTerminateRef.current?.(event);\n })\n : undefined;\n\n return () => {\n unsubStart?.();\n unsubComplete?.();\n unsubTerminate?.();\n };\n }, [hasOnStart, hasOnComplete, hasOnTerminate, resolvedMachine]);\n\n React.useEffect(() => {\n return () => {\n if (machine || !internalMachineRef.current) {\n return;\n }\n internalMachineRef.current.dispose();\n internalMachineRef.current = null;\n };\n }, [machine]);\n\n return <JourneyContext.Provider value={value}>{children}</JourneyContext.Provider>;\n };\n\n return Provider;\n};\n", "import React from \"react\";\n\nimport type { JourneySnapshot } from \"@rxova/journey-core\";\nimport type { JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\ntype UseJourneySnapshot<\n TContext,\n TStepId extends string,\n TStepMeta = unknown\n> = () => JourneySnapshot<TContext, TStepId, TStepMeta>;\n\ntype StepRendererFactoryProps<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = {\n useJourneySnapshot: UseJourneySnapshot<TContext, TStepId, TStepMeta>;\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n};\n\nexport const createStepRenderer = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>({\n useJourneySnapshot,\n useJourneyStore\n}: StepRendererFactoryProps<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>) => {\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const snapshot = useJourneySnapshot();\n const { journey } = useJourneyStore(\"StepRenderer\");\n\n const StepComponent = journey.steps[snapshot.currentStepId]?.component;\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n return <StepComponent key={snapshot.currentStepId} />;\n };\n\n return StepRenderer;\n};\n", "import React from \"react\";\n\nimport type { JourneyPayloadFor, JourneySendEvent, JourneySendResult } from \"@rxova/journey-core\";\nimport type { JourneyEventType, JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\nexport const createUseJourneyApi = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n) => {\n return () => {\n const { machine } = useJourneyStore(\"useJourneyApi\");\n\n return React.useMemo(\n () => ({\n send: async (\n event: JourneySendEvent<TStepId, JourneyEventType<TCustomEvent>, TEventPayloadMap>\n ): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.send(event);\n },\n goToNextStep: async (): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.goToNextStep();\n },\n terminateJourney: async (\n payload?: JourneyPayloadFor<\n JourneyEventType<TCustomEvent>,\n TEventPayloadMap,\n \"terminateJourney\"\n >\n ): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.terminateJourney(payload);\n },\n completeJourney: async (\n payload?: JourneyPayloadFor<\n JourneyEventType<TCustomEvent>,\n TEventPayloadMap,\n \"completeJourney\"\n >\n ): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.completeJourney(payload);\n },\n goToPreviousStep: async (\n steps?: number\n ): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.goToPreviousStep(steps);\n },\n goToLastVisitedStep: async (): Promise<JourneySendResult<TContext, TStepId, TStepMeta>> => {\n return machine.goToLastVisitedStep();\n },\n clearStepError: (stepId?: TStepId) => {\n machine.clearStepError(stepId);\n },\n updateContext: (updater: (context: TContext) => TContext) => {\n machine.updateContext(updater);\n },\n updateStepMetadata: (stepId: TStepId, updater: (metadata: TStepMeta) => TStepMeta) => {\n machine.updateStepMetadata(stepId, updater);\n },\n resetJourney: () => {\n machine.resetMachine();\n }\n }),\n [machine]\n );\n };\n};\n", "import React from \"react\";\n\nimport type { JourneyObservationEvent } from \"@rxova/journey-core\";\nimport type { JourneyEventType, JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\nexport const createUseJourneyEvent = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n) => {\n return (\n listener: (\n event: JourneyObservationEvent<\n TStepId,\n JourneyEventType<TCustomEvent>,\n TEventPayloadMap,\n TStepMeta\n >\n ) => void\n ) => {\n const { machine } = useJourneyStore(\"useJourneyEvent\");\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n React.useEffect(() => {\n return machine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [machine]);\n };\n};\n", "import type { JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\nexport const createUseJourneyMachine = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n) => {\n return () => {\n return useJourneyStore(\"useJourneyMachine\").machine;\n };\n};\n", "import React from \"react\";\n\nimport type { JourneyEqualityFn, JourneySelector, JourneySnapshot } from \"@rxova/journey-core\";\nimport type { JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\ntype SelectorCache<TContext, TStepId extends string, TStepMeta, TSelected> = {\n snapshot: JourneySnapshot<TContext, TStepId, TStepMeta>;\n selected: TSelected;\n selector: JourneySelector<TContext, TStepId, TStepMeta, TSelected>;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nexport const createUseJourneySelector = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n) => {\n return <TSelected>(\n selector: JourneySelector<TContext, TStepId, TStepMeta, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const { machine } = useJourneyStore(\"useJourneySelector\");\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TStepMeta, TSelected> | null>(\n null\n );\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = machine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n cached.selector === selector &&\n cached.isEqual === isEqual &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (cached && cached.selector === selector && cached.isEqual === isEqual) {\n if (cached.isEqual(cached.selected, nextSelected)) {\n cacheRef.current = {\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n }\n\n cacheRef.current = {\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [machine, selector, isEqual]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n machine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [machine, selector, isEqual]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n};\n", "import React from \"react\";\n\nimport type { JourneySnapshot } from \"@rxova/journey-core\";\nimport type { JourneyReactEventPayloadMap, JourneyStoreValue } from \"../types\";\n\ntype UseJourneyStore<\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n> = (\n hookName?: string\n) => JourneyStoreValue<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>;\n\nexport const createUseJourneySnapshot = <\n TContext,\n TStepId extends string,\n TCustomEvent extends string = never,\n TEventPayloadMap extends JourneyReactEventPayloadMap<TCustomEvent> = Record<never, never>,\n TStepMeta = unknown\n>(\n useJourneyStore: UseJourneyStore<TContext, TStepId, TCustomEvent, TEventPayloadMap, TStepMeta>\n) => {\n return (): JourneySnapshot<TContext, TStepId, TStepMeta> => {\n const { machine } = useJourneyStore(\"useJourneySnapshot\");\n const getSnapshot = React.useCallback(() => machine.getSnapshot(), [machine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => machine.subscribe(onStoreChange),\n [machine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n};\n", "// Mark this entry point as a Next.js App Router client module so it can be used in client components.\n\"use client\";\n\nexport { createJourneyBindings } from \"./bindings\";\nexport {\n createTransitions,\n tx,\n JOURNEY_STATUS,\n JOURNEY_EVENT,\n JOURNEY_ASYNC_PHASE,\n JOURNEY_WILDCARD\n} from \"@rxova/journey-core\";\nexport type { JourneyDefinition, JourneySendResult } from \"@rxova/journey-core\";\nexport type {\n JourneyApi,\n JourneyBindings,\n JourneyBindingsProviderProps,\n JourneyEventType,\n JourneyReactDefinition,\n JourneyReactEventPayloadMap,\n JourneyReactStep,\n JourneyStoreValue\n} from \"./types\";\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["
|
|
3
|
+
"sources": ["../src/createJourney.tsx", "../src/provider.tsx", "../src/runtime-hooks.tsx"],
|
|
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,E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AM,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
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { JourneyEqualityFn, JourneyJsonObject, JourneySelector, JourneyMachineWithPlugins, JourneyMachinePlugin } from "@rxova/journey-core";
|
|
3
|
+
import type { JourneyProviderProps } from "./types";
|
|
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, onError, disposeOnUnmount, children }: JourneyProviderProps<TStepId>) => import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
StepRenderer: ({ fallback }: {
|
|
7
|
+
fallback?: React.ReactNode;
|
|
8
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { JourneyEqualityFn, JourneyJsonObject, JourneySelector, JourneyMachineWithPlugins, JourneyMachinePlugin } from "@rxova/journey-core";
|
|
3
|
+
import type { JourneyProviderProps } from "./types";
|
|
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, onError, disposeOnUnmount, children }: JourneyProviderProps<TStepId>) => import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
StepRenderer: ({ fallback }: {
|
|
7
|
+
fallback?: React.ReactNode;
|
|
8
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
useJourneySnapshot: () => JourneySnapshot<TContext, TStepId>;
|
|
5
|
+
useJourneyComputed: () => JourneyComputed<TStepId>;
|
|
6
|
+
useJourneySelector: <TSelected>(selector: JourneySelector<TContext, TStepId, TSelected>, equalityFn?: JourneyEqualityFn<TSelected>) => TSelected;
|
|
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>;
|
|
9
|
+
useJourneyEvent: (listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void) => void;
|
|
10
|
+
useJourneyStepLifecycle: (stepId: TStepId, callbacks: {
|
|
11
|
+
onEnter?: (args: {
|
|
12
|
+
context: TContext;
|
|
13
|
+
}) => void;
|
|
14
|
+
onLeave?: (args: {
|
|
15
|
+
context: TContext;
|
|
16
|
+
}) => void;
|
|
17
|
+
}) => void;
|
|
18
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
useJourneySnapshot: () => JourneySnapshot<TContext, TStepId>;
|
|
5
|
+
useJourneyComputed: () => JourneyComputed<TStepId>;
|
|
6
|
+
useJourneySelector: <TSelected>(selector: JourneySelector<TContext, TStepId, TSelected>, equalityFn?: JourneyEqualityFn<TSelected>) => TSelected;
|
|
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>;
|
|
9
|
+
useJourneyEvent: (listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void) => void;
|
|
10
|
+
useJourneyStepLifecycle: (stepId: TStepId, callbacks: {
|
|
11
|
+
onEnter?: (args: {
|
|
12
|
+
context: TContext;
|
|
13
|
+
}) => void;
|
|
14
|
+
onLeave?: (args: {
|
|
15
|
+
context: TContext;
|
|
16
|
+
}) => void;
|
|
17
|
+
}) => void;
|
|
18
|
+
};
|
package/dist/types.d.cts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type React from "react";
|
|
2
|
+
import type { JourneyBuilderDefinitionMetadata, JourneyBuilderCustomEventKey, JourneyComputed, JourneyDefaultEventType, JourneyDefinition, JourneyEqualityFn, JourneyJsonObject, JourneyMachinePlugin, JourneyMachineWithPlugins, JourneyObservationEvent, JourneyPayloadFor, JourneySelector, JourneySendEvent, JourneySendResult, JourneySnapshot } from "@rxova/journey-core";
|
|
3
|
+
export type JourneyDefaultEvent = JourneyDefaultEventType;
|
|
4
|
+
export type JourneyViews<TStepId extends string> = Record<TStepId, React.ComponentType>;
|
|
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>>;
|
|
23
|
+
export type JourneyApi<TContext extends JourneyJsonObject, TStepId extends string, TEventMap extends Record<string, unknown> = Record<never, never>, TStepMeta = unknown> = {
|
|
24
|
+
startJourney: () => Promise<JourneySnapshot<TContext, TStepId>>;
|
|
25
|
+
send: (event: JourneySendEvent<TStepId, TEventMap>) => Promise<JourneySendResult<TContext, TStepId>>;
|
|
26
|
+
goToNextStep: () => Promise<JourneySendResult<TContext, TStepId>>;
|
|
27
|
+
goToStepById: (stepId: TStepId) => Promise<JourneySendResult<TContext, TStepId>>;
|
|
28
|
+
terminateJourney: (payload?: JourneyPayloadFor<TEventMap, "terminateJourney">) => Promise<JourneySendResult<TContext, TStepId>>;
|
|
29
|
+
completeJourney: (payload?: JourneyPayloadFor<TEventMap, "completeJourney">) => Promise<JourneySendResult<TContext, TStepId>>;
|
|
30
|
+
goToPreviousStep: (steps?: number) => Promise<JourneySendResult<TContext, TStepId>>;
|
|
31
|
+
goToLastVisitedStep: () => Promise<JourneySendResult<TContext, TStepId>>;
|
|
32
|
+
clearStepError: (stepId?: TStepId) => Promise<JourneySnapshot<TContext, TStepId>>;
|
|
33
|
+
updateContext: (updater: (context: TContext) => TContext) => Promise<JourneySnapshot<TContext, TStepId>>;
|
|
34
|
+
getStepMeta: (stepId: TStepId) => TStepMeta | undefined;
|
|
35
|
+
resetJourney: () => Promise<JourneySnapshot<TContext, TStepId>>;
|
|
36
|
+
};
|
|
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
|
+
};
|
|
40
|
+
export type JourneyProviderErrorContext = {
|
|
41
|
+
phase: "start";
|
|
42
|
+
};
|
|
43
|
+
export type JourneyProviderProps<TStepId extends string> = {
|
|
44
|
+
views: JourneyViews<TStepId>;
|
|
45
|
+
onError?: (error: unknown, context: JourneyProviderErrorContext) => void;
|
|
46
|
+
disposeOnUnmount?: boolean;
|
|
47
|
+
children: React.ReactNode;
|
|
48
|
+
};
|
|
49
|
+
export type JourneyRuntime<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>> = {
|
|
50
|
+
machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>;
|
|
51
|
+
dispose: () => void;
|
|
52
|
+
useJourneySnapshot: () => JourneySnapshot<TContext, TStepId>;
|
|
53
|
+
useJourneyComputed: () => JourneyComputed<TStepId>;
|
|
54
|
+
useJourneySelector: <TSelected>(selector: JourneySelector<TContext, TStepId, TSelected>, equalityFn?: JourneyEqualityFn<TSelected>) => TSelected;
|
|
55
|
+
useJourneyApi: () => JourneyApi<TContext, TStepId, TEventMap, TStepMeta>;
|
|
56
|
+
useJourneyEvent: (listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void) => void;
|
|
57
|
+
useJourneyStepLifecycle: (stepId: TStepId, callbacks: {
|
|
58
|
+
onEnter?: (args: {
|
|
59
|
+
context: TContext;
|
|
60
|
+
}) => void;
|
|
61
|
+
onLeave?: (args: {
|
|
62
|
+
context: TContext;
|
|
63
|
+
}) => void;
|
|
64
|
+
}) => void;
|
|
65
|
+
JourneyProvider: React.ComponentType<JourneyProviderProps<TStepId>>;
|
|
66
|
+
StepRenderer: React.ComponentType<{
|
|
67
|
+
fallback?: React.ReactNode;
|
|
68
|
+
}>;
|
|
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>;
|
|
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>;
|
|
80
|
+
export {};
|