Line data Source code
1 : import type { CoachStep, EntryPath } from './onboardingTours'
2 :
3 2355 : export const IDLE = { phase: 'idle' } as const
4 :
5 : /**
6 : * One activation of a tour. The same tour can start, lose its trigger and start
7 : * again, so its name cannot tell a reply meant for this run apart from one that
8 : * outlived the run before it.
9 : */
10 : export type RunId = number & { readonly __brand: 'RunId' }
11 :
12 : /** `entering` is separate from `showing` because `onEnter` runs between them. */
13 : export type TourState =
14 : | typeof IDLE
15 : | { phase: 'resolving'; tour: EntryPath; run: RunId }
16 : | {
17 : phase: 'waiting'
18 : tour: EntryPath
19 : run: RunId
20 : steps: CoachStep[]
21 : fromIdx: number | null
22 : }
23 : | {
24 : phase: 'entering'
25 : tour: EntryPath
26 : run: RunId
27 : steps: CoachStep[]
28 : fromIdx: number | null
29 : toIdx: number
30 : }
31 : | {
32 : phase: 'showing'
33 : tour: EntryPath
34 : run: RunId
35 : steps: CoachStep[]
36 : idx: number
37 : }
38 :
39 : type RunningState = Extract<
40 : TourState,
41 : { phase: 'waiting' | 'entering' | 'showing' }
42 : >
43 :
44 47 : export function isRunning(state: TourState): state is RunningState {
45 47 : return (
46 : state.phase === 'waiting' ||
47 : state.phase === 'entering' ||
48 : state.phase === 'showing'
49 : )
50 : }
51 :
52 : /** `fromIdx === null` means nothing is on screen yet, so nothing to travel. */
53 2391 : export function shownIdx(state: TourState): number | null {
54 2391 : if (state.phase === 'showing') return state.idx
55 2391 : if (state.phase === 'entering')
56 18 : return state.fromIdx === null ? null : state.toIdx
57 2391 : if (state.phase === 'waiting') return state.fromIdx
58 2345 : return null
59 : }
60 :
61 : /** Events answering an `await` name their run, so a late reply can be refused. */
62 : export type TourEvent =
63 : | { type: 'requested'; tour: EntryPath; run: RunId }
64 : | { type: 'resolved'; run: RunId; steps: CoachStep[] }
65 : | { type: 'resolvedEmpty'; run: RunId }
66 : | { type: 'targetAwaited'; run: RunId; fromIdx: number | null }
67 : | { type: 'stepEntering'; run: RunId; toIdx: number }
68 : | { type: 'stepShown'; run: RunId; idx: number }
69 : | { type: 'ended'; run: RunId }
70 :
71 : /**
72 : * The only transition table. An event meaningless in the current phase, or from
73 : * a run that is over, returns `state` itself rather than a phase that shouldn't
74 : * follow.
75 : */
76 43 : export function reduceTour(state: TourState, event: TourEvent): TourState {
77 43 : if (event.type === 'requested')
78 6 : return state.phase === 'idle'
79 : ? { phase: 'resolving', tour: event.tour, run: event.run }
80 : : state
81 :
82 : // The tour name alone would let a later run inherit an earlier run's reply.
83 43 : if (state.phase === 'idle' || state.run !== event.run) return state
84 :
85 43 : switch (event.type) {
86 : case 'resolved':
87 6 : return state.phase === 'resolving'
88 : ? {
89 : phase: 'entering',
90 : tour: state.tour,
91 : run: state.run,
92 : steps: event.steps,
93 : fromIdx: null,
94 : toIdx: 0
95 : }
96 : : state
97 : case 'resolvedEmpty':
98 0 : return state.phase === 'resolving' ? IDLE : state
99 : case 'targetAwaited':
100 1 : return isRunning(state)
101 : ? {
102 : phase: 'waiting',
103 : tour: state.tour,
104 : run: state.run,
105 : steps: state.steps,
106 : fromIdx: event.fromIdx
107 : }
108 : : state
109 : case 'stepEntering':
110 13 : return isRunning(state)
111 : ? {
112 : phase: 'entering',
113 : tour: state.tour,
114 : run: state.run,
115 : steps: state.steps,
116 : fromIdx: shownIdx(state),
117 : toIdx: event.toIdx
118 : }
119 : : state
120 : case 'stepShown':
121 13 : return state.phase === 'entering'
122 : ? {
123 : phase: 'showing',
124 : tour: state.tour,
125 : run: state.run,
126 : steps: state.steps,
127 : idx: event.idx
128 : }
129 : : state
130 : case 'ended':
131 4 : return IDLE
132 : }
133 : }
|