diff --git a/lessons/how-to-use-transient-transitions/after.js b/lessons/how-to-use-transient-transitions/after.js index 2b7195c..fc5430f 100644 --- a/lessons/how-to-use-transient-transitions/after.js +++ b/lessons/how-to-use-transient-transitions/after.js @@ -1 +1,33 @@ -// after.js +const { assign, Machine } = require('xstate') + +const ifAtFirstYoudontSucceed = Machine( + { + id: 'tryTryAgain', + initial: 'idle', + context: { + tries: 0, + }, + states: { + idle: { + on: { TRY: 'trying' }, + }, + trying: { + entry: ['incTries'], + on: { + '': [{ target: 'success', cond: 'triedEnough' }, { target: 'idle' }], + }, + }, + success: {}, + }, + }, + { + actions: { + incTries: assign({ + tries: ctx => ctx.tries + 1, + }), + }, + guards: { + triedEnough: ctx => ctx.tries > 2, + }, + } +) diff --git a/lessons/how-to-use-transient-transitions/before.js b/lessons/how-to-use-transient-transitions/before.js index 11587d9..ed8faf8 100644 --- a/lessons/how-to-use-transient-transitions/before.js +++ b/lessons/how-to-use-transient-transitions/before.js @@ -1 +1,27 @@ -// before.js +const { assign, Machine } = require('xstate') + +const ifAtFirstYoudontSucceed = Machine( + { + id: 'tryTryAgain', + initial: 'idle', + context: { + tries: 0, + }, + states: { + idle: { + on: { TRY: 'trying' }, + }, + trying: { + entry: ['incTries'], + }, + success: {}, + }, + }, + { + actions: { + incTries: assign({ + tries: ctx => ctx.tries + 1, + }), + }, + } +)