Created
November 28, 2019 02:27
-
-
Save rads/5a4f982291ff387306ef482737abe58e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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
function maxErrors(context, event) { | |
return context.errorCount > 2; | |
} | |
function versionMismatch(context, event) { | |
const { currentEditor } = context; | |
const clientEngineVersion = currentEditor && currentEditor.plugins | |
.get('RealTimeCollaborationClient').service._engineVersion; | |
return ( | |
window._CK_ENGINE_VERSION && | |
clientEngineVersion !== window._CK_ENGINE_VERSION | |
); | |
} | |
function createMachine(options) { | |
return Machine({ | |
id: 'supervisor', | |
initial: 'started', | |
context: { | |
initEditor: options.initEditor, | |
windowEvents: options.windowEvents, | |
startRecovery: options.startRecovery, | |
docEvents: options.docEvents, | |
cannotRecover: options.cannotRecover, | |
beforeDestroy: options.beforeDestroy, | |
online: navigator.onLine, | |
currentEditor: null, | |
currentError: null, | |
errorCount: 0, | |
initCount: 0, | |
recoveryCount: 0 | |
}, | |
states: { | |
stopped: { type: 'final' }, | |
started: { | |
initial: 'online', | |
on: { | |
stop: 'stopped' | |
}, | |
states: { | |
offline: { | |
on: { | |
online: 'online' | |
} | |
}, | |
online: { | |
invoke: { | |
id: 'addEventListeners', | |
src: 'addEventListeners' | |
}, | |
initial: 'initializingEditor', | |
on: { | |
offline: 'offline', | |
flushReceived: '.initializingEditor', | |
}, | |
states: { | |
initializingEditor: { | |
invoke: { | |
id: 'initializeEditor', | |
src: 'initializeEditor', | |
onError: { | |
target: 'error', | |
actions: assign({ currentError: (context, event) => event.data }) | |
}, | |
onDone: { | |
target: 'editing', | |
actions: assign({ currentEditor: (context, event) => event.data }) | |
} | |
}, | |
entry: assign({ initCount: (ctx, event) => ctx.initCount + 1 }) | |
}, | |
editing: { | |
on: { | |
errorDetected: 'error' | |
} | |
}, | |
error: { | |
on: { | |
'': [ | |
{ cond: maxErrors, target: 'manualRefresh' }, | |
{ cond: versionMismatch, target: 'manualRefresh' }, | |
{ target: 'recovering' } | |
] | |
}, | |
entry: assign({ errorCount: (ctx, event) => ctx.errorCount + 1 }) | |
}, | |
recovering: { | |
entry: [ | |
assign({ recoveryCount: (ctx, event) => ctx.recoveryCount + 1 }), | |
'startRecovery' | |
] | |
}, | |
manualRefresh: { type: 'final' }, | |
} | |
} | |
} | |
} | |
} | |
}, { | |
activities: options.activities, | |
actions: options.actions, | |
guards: options.guards, | |
services: options.services | |
}); | |
} | |
const machine = createMachine({}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment