Skip to content

Instantly share code, notes, and snippets.

@rads
Last active November 28, 2019 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rads/568cb2c6b04684c508a255edd43a861c to your computer and use it in GitHub Desktop.
Save rads/568cb2c6b04684c508a255edd43a861c to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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