Skip to content

Instantly share code, notes, and snippets.

@andrewiggins
Last active July 12, 2020 18:53
Show Gist options
  • Save andrewiggins/80c62c3012452b6c4ab96a9c9c995975 to your computer and use it in GitHub Desktop.
Save andrewiggins/80c62c3012452b6c4ab96a9c9c995975 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: 'Lock',
initial: 'acquiring',
context: {
held_time: 0 // Cumulative time the lock has been held
},
states: {
// Read lock and either keep waiting or write & hold
acquiring: {
entry() {
// Read lock and determine if it is held.
// if it is held, send WAIT
// if it is available, write to lock and send HOLD, held_time: 0
},
on: {
HOLD: 'holding',
WAIT: 'waiting'
}
},
// Wait random time before attempting to acquire again
waiting: {
entry() {
// Wait random time, then send ACQUIRE
},
on: {
ACQUIRE: 'acquiring'
}
},
// Wait deterministic time before reading lock
holding: {
entry(ctx, event) {
// Wait appropriate hold time
// send CHECK, held_time += hold time
},
on: {
CHECK_HOLD: 'checking',
}
},
// read lock to see if we still have it and either keep holding, assume lock is acquired, or go back to waiting if it isn't
checking: {
entry(ctx, event) {
// read lock
// if still ours and min hold time is not met, send HOLD
// if still ours and hold time is met, send ACQUIRED,
// if not ours, send WAIT
},
on: {
HOLD: 'holding',
WAIT: 'waiting',
ACQUIRED: 'acquired'
}
},
// done!
acquired: {
type: 'final'
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment