Skip to content

Instantly share code, notes, and snippets.

@dperetti
Last active August 7, 2019 08:23
Show Gist options
  • Save dperetti/02ea0c42fd04e0781ce857e85a6be2b0 to your computer and use it in GitHub Desktop.
Save dperetti/02ea0c42fd04e0781ce857e85a6be2b0 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: 'outbox',
initial: 'checkStaleNotes',
context: {
staleCount: 2,
},
states: {
reset: {
on: {
'': {
target: 'checkStaleNotes'
}
}
},
checkStaleNotes: {
entry: ['getStaleNotesCount'],
on: {
SAVE: {
target: 'checkStaleNotes',
actions: 'saveNotes',
},
'': {
target: 'checkUpstream',
cond: 'noStaleNotes'
},
RESET: {
target: 'reset',
}
}
},
checkUpstream: {
on: {
RESET: {
target: 'reset',
}
},
onDone: {
'qsd': {
target: 'issues'
}
},
states: {
serverCall: {
invoke: {
src: 'checkSyncCursorOnServer',
onDone: {
target: 'result',
actions: assign({ cursor: (context, event) => event.data })
},
}
},
result: {
on: {
'': {
target: '#outbox.issues'
}
}
}
}
},
issues: {
}
},
},
{
services: {
checkSyncCursorOnServer: (context, event) => new Promise((resolve, reject) => resolve({ syncCursorId: 81 }))
},
actions: {
saveNotes: assign({ staleCount: 0 }),
getStaleNotesCount: assign({
staleCount: (context, event) => event.type === 'SAVE' ? 0 : 4 // simulate the save
}),
getSyncCursorIds: assign({
localSyncCursorId: (context, event) => event.data.syncCursorId, // see checkSyncCursorOnServer below
remoteSyncCursorId: 95 // () => LocalDBStore.state.syncCursorId
}),
pullProject: assign({
localSyncCursorId: 92,
remoteSyncCursorId: 92
}),
},
guards: {
noStaleNotes: (context, event) => {
return context.staleCount === 0
},
localCursorEqualsServerCursor: (context, event) => {
return context.localSyncCursorId === context.remoteSyncCursorId + 1
}
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment