Skip to content

Instantly share code, notes, and snippets.

@rads
Created November 28, 2019 03:42
Show Gist options
  • Save rads/8bf47eb69c01e683b3b51aa825a344f7 to your computer and use it in GitHub Desktop.
Save rads/8bf47eb69c01e683b3b51aa825a344f7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function isMaxErrors(context, event) {
return context.errorCount > 2;
}
function isVersionMismatch(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 requestRefresh(context, event) {
context.cannotRecover();
}
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',
maxErrors: '.manualRefresh',
versionMismatch: '.manualRefresh',
},
states: {
manualRefresh: {
type: 'final',
entry: 'requestRefresh'
},
offline: {
on: {
online: 'online'
}
},
online: {
invoke: {
id: 'addEventListeners',
src: 'addEventListeners'
},
initial: 'initializingEditor',
on: {
offline: 'offline',
flushReceived: '.initializingEditor',
errorDetected: '.error'
},
states: {
initializingEditor: {
on: {
},
invoke: {
id: 'initializeEditor',
src: 'initializeEditor',
onError: {
actions: [
assign({ currentError: (context, event) => event.data }),
send('errorDetected')
]
},
onDone: {
target: 'editing',
actions: assign({ currentEditor: (context, event) => event.data })
}
},
entry: assign({ initCount: (ctx, event) => ctx.initCount + 1 })
},
editing: {
on: {
}
},
error: {
on: {
'': [
{ cond: isMaxErrors, actions: send('maxErrors') },
{ cond: isVersionMismatch, actions: send('versionMismatch'), target: 'recovering' },
{ target: 'recovering' }
]
},
entry: assign({ errorCount: (ctx, event) => ctx.errorCount + 1 })
},
recovering: {
entry: [
assign({ recoveryCount: (ctx, event) => ctx.recoveryCount + 1 }),
'startRecovery'
]
}
}
}
}
}
}
}, {
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