Last active
October 21, 2020 06:45
-
-
Save CMCDragonkai/b2960caed7b4c1e5e7ef7224405d26ba to your computer and use it in GitHub Desktop.
Backup Restore Machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const canBackup = (context, event) => { | |
return !context.backupLock | |
}; | |
const canRestore = (context, event) => { | |
return !context.restoreLock | |
}; | |
const backupMachine = Machine( | |
{ | |
id: 'backup', | |
context: { | |
backupLock: false, | |
restoreLock: false | |
}, | |
initial: 'start', | |
states: { | |
start: { | |
on: { | |
GET_RESTORE: { | |
target: 'queryS3', | |
cond: 'canRestore' | |
}, | |
GET_BACKUP: { | |
target: 'backingUp', | |
cond: 'canBackup' | |
} | |
} | |
}, | |
backingUp: { | |
on: { | |
BACKUP_FINISH: 'start' | |
}, | |
entry: assign({ | |
backupLock: (context, event) => true | |
}), | |
exit: assign({ | |
backupLock: (context, event) => false | |
}) | |
}, | |
queryS3: { | |
on: { | |
NO_BACKUP: 'start', | |
EXISTS_BACKUP: 'restoring' | |
} | |
}, | |
restoring: { | |
on: { | |
RESTORE_FINISH: 'start' | |
}, | |
entry: assign({ | |
restoreLock: (context, event) => true | |
}), | |
exit: assign({ | |
restoreLock: (context, event) => false | |
}) | |
} | |
} | |
}, | |
{ | |
guards: { | |
canBackup, | |
canRestore | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment