Last active
December 7, 2019 02:20
-
-
Save rads/be64730f2c0e586573c3daac8d4ecf3a to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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
// ============================================================================= | |
// 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 }); | |
const setLastError = assign({ lastError: (context, event) => event.error }); | |
const setPendingRecoveryTrue = assign((context, event) => { | |
sessionStorage.setItem('pendingRecovery', 'true'); | |
return { pendingRecovery: true }; | |
}); | |
const setPendingRecoveryFalse = assign((context, event) => { | |
sessionStorage.setItem('pendingRecovery', 'false'); | |
return { pendingRecovery: 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 randInt(start, end) { | |
return start + Math.floor(Math.random() * Math.floor(end - start)); | |
} | |
const recoveryTimeoutDelay = 1000; | |
function recoveryStartDelay(context, event) { | |
return (context.recoveryCount === 1) ? 0 : randInt(1000, 3000); | |
} | |
function getPendingRecoveryFromStorage() { | |
return sessionStorage.getItem('pendingRecovery') === 'true'; | |
} | |
function isPendingRecovery(context, event) { | |
return context.pendingRecovery; | |
} | |
function createMachine(options) { | |
return Machine({ | |
id: 'supervisor', | |
initial: 'started', | |
context: { | |
initEditor: options.initEditor, | |
windowEvents: options.windowEvents, | |
sendRecoveryRequest: options.sendRecoveryRequest, | |
docEvents: options.docEvents, | |
requestRefresh: options.requestRefresh, | |
beforeDestroy: options.beforeDestroy, | |
online: navigator.onLine, | |
currentEditor: null, | |
errorCount: 0, | |
initCount: 0, | |
recoveryCount: 0, | |
pendingInitialization: false, | |
pendingRecovery: getPendingRecoveryFromStorage(), | |
lastError: null, | |
lastSessionState: options.lastSessionState | |
}, | |
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', | |
recoveryTimeout: '.manualRefresh' | |
}, | |
states: { | |
manualRefresh: { | |
type: 'final', | |
entry: 'requestRefresh' | |
}, | |
offline: { | |
on: { | |
online: 'online' | |
} | |
}, | |
online: { | |
invoke: { | |
id: 'addEventListeners', | |
src: 'addEventListeners' | |
}, | |
initial: 'initializing', | |
on: { | |
offline: 'offline', | |
flushReceived: '.initializing', | |
errorDetected: '.error' | |
}, | |
states: { | |
initializing: { | |
entry: [incInitCount, setPendingInitializationTrue, 'destroyEditor'], | |
exit: [setPendingInitializationFalse], | |
invoke: { | |
id: 'initializeEditor', | |
src: 'initializeEditor' | |
}, | |
on: { | |
initSuccess: { | |
target: 'editing', | |
actions: setCurrentEditor | |
}, | |
initError: { | |
target: 'error', | |
actions: 'sendErrorDetected' | |
} | |
} | |
}, | |
editing: { | |
on: { | |
'': { | |
cond: isPendingRecovery, | |
actions: [ | |
setPendingRecoveryFalse, | |
'sendRecoverySuccess' | |
] | |
} | |
} | |
}, | |
error: { | |
entry: [ | |
incErrorCount, | |
setLastError, | |
], | |
on: { | |
'': [ | |
{ | |
cond: isPendingRecovery, | |
actions: [ | |
setPendingRecoveryFalse, | |
'sendRecoveryFailure' | |
] | |
}, | |
{ | |
cond: isMaxErrors, | |
actions: send('maxErrors'), | |
target: 'failed' | |
}, | |
{ | |
cond: 'isRecoveryTimeout', | |
actions: send('recoveryTimeout'), | |
target: 'failed' | |
}, | |
{ target: 'recovering' } | |
] | |
} | |
}, | |
failed: { type: 'final' }, | |
recovering: { | |
initial: 'starting', | |
states: { | |
starting: { | |
entry: [ | |
incRecoveryCount, | |
setPendingRecoveryTrue, | |
send('recoveryStart') | |
], | |
after: { | |
recoveryStartDelay: 'sendingRequest' | |
} | |
}, | |
sendingRequest: { | |
entry: [ | |
'sendRecoveryRequest', | |
'errorOnRecoveryTimeout', | |
], | |
on: { | |
'': [ | |
{ | |
cond: isVersionMismatch, | |
actions: send('versionMismatch'), | |
target: 'waiting' | |
}, | |
{ target: 'waiting' } | |
] | |
} | |
}, | |
waiting: { | |
exit: 'cancelRecoveryTimeout' | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}, { | |
activities: options.activities, | |
actions: options.actions, | |
guards: options.guards, | |
services: options.services, | |
delays: { | |
recoveryStartDelay, | |
recoveryTimeoutDelay | |
} | |
}); | |
} | |
// 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