Skip to content

Instantly share code, notes, and snippets.

@rads
Last active December 5, 2019 00:37
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/76ef0374310bd9be57d3f426d78ea54d to your computer and use it in GitHub Desktop.
Save rads/76ef0374310bd9be57d3f426d78ea54d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// =============================================================================
// Updating the Visualization
//
// The code after this comment can be copy-pasted into the XState Visualizer:
// https://xstate.js.org/viz/
// =============================================================================
const incErrorCount = assign({ errorCount: (ctx, event) => ctx.errorCount + 1 });
const incInitCount = assign({ initCount: (ctx, event) => ctx.initCount + 1 });
const incRecoveryCount = assign({ recoveryCount: (ctx, event) => ctx.recoveryCount + 1 });
const setCurrentEditor = assign({ currentEditor: (context, event) => event.editor });
const setPendingInitializationTrue = assign({ pendingInitialization: (context, event) => true });
const setPendingInitializationFalse = assign({ pendingInitialization: (context, event) => false });
function isNotPendingInitialization(context, event) {
return !context.pendingInitialization;
}
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 createMachine(options) {
return Machine({
id: 'supervisor',
initial: 'started',
context: {
initEditor: options.initEditor,
windowEvents: options.windowEvents,
startRecovery: options.startRecovery,
docEvents: options.docEvents,
requestRefresh: options.requestRefresh,
beforeDestroy: options.beforeDestroy,
online: navigator.onLine,
currentEditor: null,
stopped: {value: false},
errorCount: 0,
initCount: 0,
recoveryCount: 0,
pendingInitialization: false
},
states: {
stopping: {
on: {
'': { cond: isNotPendingInitialization, target: 'stopped' },
initSuccess: 'stopped',
initError: 'stopped'
}
},
stopped: {
entry: 'destroyEditor',
type: 'final'
},
started: {
initial: 'online',
on: {
stop: 'stopping',
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: {
entry: [incInitCount, setPendingInitializationTrue, 'destroyEditor'],
exit: [setPendingInitializationFalse],
invoke: {
id: 'initializeEditor',
src: 'initializeEditor'
},
on: {
initSuccess: { target: 'editing', actions: setCurrentEditor },
initError: { target: 'error', actions: 'sendErrorDetected' }
}
},
editing: { },
error: {
entry: incErrorCount,
on: {
'': [
{
cond: isMaxErrors,
actions: send('maxErrors'),
target: 'failed'
},
{
cond: isVersionMismatch,
actions: send('versionMismatch'),
target: 'recovering'
},
{ target: 'recovering' }
]
}
},
failed: { type: 'final' },
recovering: {
entry: [ incRecoveryCount, 'startRecovery' ]
}
}
}
}
}
}
}, {
activities: options.activities,
actions: options.actions,
guards: options.guards,
services: options.services
});
}
// Uncomment the following line for the visualizer:
const machine = createMachine({});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment