Skip to content

Instantly share code, notes, and snippets.

@larvanitis
Last active August 5, 2020 08:08
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 larvanitis/bfad75fd3573e41a9813122d27cda483 to your computer and use it in GitHub Desktop.
Save larvanitis/bfad75fd3573e41a9813122d27cda483 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const roomStates = {
initial: 'idle',
states: {
idle: {
on: {
JOIN_ROOM: {
target: 'joining',
cond: (context, event) => !!context.token
}
}
},
joining: {
on: {
JOINED_ROOM: {
target: 'joined'
}
}
},
joined: {
on: {
LEAVE_ROOM: 'idle'
}
}
},
};
const socketStates = {
initial: 'disconnected',
states: {
disconnected: {
on: {
CONNECT: 'connecting'
}
},
connecting: {
on: {
SUCCESS: 'connected',
FAILURE: 'failure',
CANCEL: 'disconnected'
}
},
connected: {
entry: assign({
retries: 0
}),
on: {
DISCONNECT: 'disconnected'
},
...roomStates
},
failure: {
on: {
'': { // RETRY
target: 'connecting',
actions: assign({
retries: (context, event) => context.retries + 1
})
}
}
}
}
};
const tokenStates = {
initial: 'noToken',
states: {
noToken: {
entry: assign({
token: ''
}),
on: {
'': 'issuing'
}
},
issuing: {
on: {
SUCCESS: 'issued',
AUTH_ERROR: 'authError',
// TIMEOUT_ERROR: 'retry'
}
},
issued: {
entry: assign({
tokenRetries: 0,
token: (context, event) => 'event.token-' + Math.random()
}),
on: {
EXPIRE_ERROR: 'issuing'
}
},
retry: {
on: {
'': { // RETRY
target: 'issuing',
actions: assign({
tokenRetries: (context, event) => context.tokenRetries + 1
})
}
}
},
authError: {
type: 'final'
}
}
};
const docIdStates = {
initial: 'unset',
states: {
unset: {
on: {
SET: 'set'
},
entry: assign({
docId: () => ''
})
},
set: {
entry: assign({
docId: (context, event) => 'event.docId-' + Math.random()
}),
on: {
UNSET: 'unset',
CHANGE: 'changed'
},
...tokenStates
},
changed: {
on: {
'': 'set'
},
entry: assign({
docId: (context, event) => 'event.docId-' + Math.random()
})
}
}
};
const fetchMachine = Machine({
id: 'rte',
type: 'parallel',
context: {
docId: '',
token: '',
retries: 0,
tokenRetries: 0
},
states: {
socket: {
...socketStates
},
docId: {
...docIdStates
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment