@@ -23,10 +23,10 @@ export class TourStep {
2323 onPrevious,
2424 } : {
2525 step : TourStepProps
26- onPrepare ?: ( ) => void
27- onInit ?: ( ) => void
28- onExit ?: ( ) => void
29- onPrevious ?: ( ) => void
26+ onPrepare ?: ( ) => void | Promise < void >
27+ onInit ?: ( ) => void | Promise < void >
28+ onExit ?: ( ) => void | Promise < void >
29+ onPrevious ?: ( ) => void | Promise < void >
3030 } ) {
3131 const content = (
3232 < div className = { this . contentClassname } > { step . content } </ div >
@@ -48,13 +48,13 @@ export class TourStep {
4848 return this . _step
4949 }
5050
51- onInit ?( ) : void
51+ onInit ?( ) : void | Promise < void >
5252
53- onPrepare ?( ) : void
53+ onPrepare ?( ) : void | Promise < void >
5454
55- onExit ?( ) : void
55+ onExit ?( ) : void | Promise < void >
5656
57- onPrevious ?( ) : void
57+ onPrevious ?( ) : void | Promise < void >
5858}
5959
6060export interface TourProps {
@@ -141,93 +141,114 @@ export function Tour({
141141
142142 const _steps = useMemo ( ( ) => steps . map ( ( it ) => it . step ) , [ steps ] )
143143
144+ // this is used to prevent multiple callbacks from being called at the same time
145+ const isTransitioning = useRef ( false )
146+
144147 const callback = useCallback (
145148 ( joyrideState : CallBackProps ) => {
146- const { action, index, type, step } = joyrideState
147- switch ( type ) {
148- case "tour:start" :
149- beforeAll ( )
150- isInit . current = true
151- setStepIndex ( 0 )
152- break
153- case "tour:end" :
154- reset ( )
155- break
156- case "step:before" :
157- steps [ index ] ?. onPrepare ?.( )
158- break
159- case "step:after" :
160- steps [ index ] ?. onExit ?.( )
161- switch ( action ) {
162- case "next" :
163- steps [ index + 1 ] ?. onInit ?.( )
164- next ( 1 )
165- break
166- case "prev" :
167- steps [ index ] ?. onPrevious ?.( )
168- steps [ index - 1 ] ?. onInit ?.( )
169- next ( - 1 )
170- break
171- case "skip" :
172- steps [ index + 2 ] ?. onInit ?.( )
173- next ( 2 )
174- break
175- case "close" :
176- reset ( )
177- break
178- case "reset" :
179- reset ( )
180- break
181- case "stop" :
149+ ( async ( ) => {
150+ if ( isTransitioning . current ) return
151+ isTransitioning . current = true
152+ const { action, index, type, step } = joyrideState
153+ try {
154+ switch ( type ) {
155+ case "tour:start" :
156+ beforeAll ( )
157+ isInit . current = true
158+ setStepIndex ( 0 )
159+ await steps [ 0 ] ?. onInit ?.( )
160+ break
161+ case "tour:end" :
162+ reset ( )
163+ break
164+ case "step:before" :
165+ await ( steps [ index ] ?. onPrepare ?.( ) )
166+ break
167+ case "step:after" :
168+ await steps [ index ] ?. onExit ?.( )
169+ switch ( action ) {
170+ case "next" :
171+ await steps [ index + 1 ] ?. onInit ?.( )
172+ next ( 1 )
173+ break
174+ case "prev" :
175+ await steps [ index ] ?. onPrevious ?.( )
176+ await steps [ index - 1 ] ?. onInit ?.( )
177+ next ( - 1 )
178+ break
179+ case "skip" :
180+ await steps [ index + 2 ] ?. onInit ?.( )
181+ next ( 2 )
182+ break
183+ case "close" :
184+ reset ( )
185+ break
186+ case "reset" :
187+ reset ( )
188+ break
189+ case "stop" :
190+ reset ( )
191+ break
192+ default :
193+ break
194+ }
195+ break
196+ case "error:target_not_found" :
197+ if ( skipOnError ) {
198+ console . info ( "Skipped" , joyrideState )
199+ if ( showInfoAndError ) {
200+ Toast . showInformationToastFlag ( {
201+ title : "Tour-Info" ,
202+ description : `Ein Step [${ steps [ index ] . step ?. title ?? "Unbekannt" } ] wurde übersprungen, das Element ${ steps [ index ] . step . target } wurde nicht gefunden.` ,
203+ } )
204+ }
205+ next ( action === "next" ? 1 : - 1 )
206+ } else {
207+ if ( showInfoAndError ) {
208+ Toast . showErrorToastFlag ( {
209+ title : "Tour-Fehler" ,
210+ description : `Fehler bei Step [${ steps [ index ] . step ?. title ?? "Unbekannt" } ]. Das Element ${ step . target } wurde nicht gefunden.` ,
211+ } )
212+ }
182213 reset ( )
183- break
184- default :
185- break
186- }
187- break
188- case "error:target_not_found" :
189- if ( skipOnError ) {
190- console . info ( "Skipped" , joyrideState )
191- if ( showInfoAndError ) {
192- Toast . showInformationToastFlag ( {
193- title : "Tour-Info" ,
194- description : `Ein Step [${ steps [ index ] . step ?. title ?? "Unbekannt" } ] wurde übersprungen, das Element ${ steps [ index ] . step . target } wurde nicht gefunden.` ,
195- } )
196214 }
197- next ( action === "next" ? 1 : - 1 )
198- } else {
199- if ( showInfoAndError ) {
200- Toast . showErrorToastFlag ( {
201- title : "Tour-Fehler" ,
202- description : `Fehler bei Step [${ steps [ index ] . step ?. title ?? "Unbekannt" } ]. Das Element ${ step . target } wurde nicht gefunden.` ,
203- } )
215+ break
216+ case "error" :
217+ if ( skipOnError ) {
218+ if ( showInfoAndError ) {
219+ Toast . showInformationToastFlag ( {
220+ title : "Tour-Info" ,
221+ description : `Ein Step [${ steps [ index ] . step ?. title ?? "Unbekannt" } ] wurde übersprungen.` ,
222+ } )
223+ }
224+ next ( action === "next" ? 1 : - 1 )
225+ } else {
226+ if ( showInfoAndError ) {
227+ Toast . showErrorToastFlag ( {
228+ title : "Tour-Fehler" ,
229+ description : `Fehler bei Step [${ steps [ index ] . step ?. title ?? "Unbekannt" } ].` ,
230+ } )
231+ }
232+ reset ( )
204233 }
205- reset ( )
234+ break
235+
236+ default :
237+ break
206238 }
207- break
208- case "error" :
209- if ( skipOnError ) {
210- if ( showInfoAndError ) {
211- Toast . showInformationToastFlag ( {
212- title : "Tour-Info" ,
213- description : `Ein Step [${ steps [ index ] . step ?. title ?? "Unbekannt" } ] wurde übersprungen.` ,
214- } )
215- }
216- next ( action === "next" ? 1 : - 1 )
217- } else {
218- if ( showInfoAndError ) {
219- Toast . showErrorToastFlag ( {
220- title : "Tour-Fehler" ,
221- description : `Fehler bei Step [${ steps [ index ] . step ?. title ?? "Unbekannt" } ].` ,
222- } )
223- }
224- reset ( )
239+ } catch ( e ) {
240+ console . error ( "Joyride Tour - callback error" , e )
241+ if ( showInfoAndError ) {
242+ Toast . showErrorToastFlag ( {
243+ title : "Tour-Fehler" ,
244+ description : "Unerwarteter Fehler im Tour-Callback." ,
245+ } )
225246 }
226- break
227-
228- default :
229- break
230- }
247+ reset ( )
248+ } finally {
249+ isTransitioning . current = false
250+ }
251+ } ) ( )
231252 } ,
232253 [ beforeAll , reset , next , showInfoAndError , skipOnError , steps ] ,
233254 )
0 commit comments