Skip to content

Instantly share code, notes, and snippets.

@RichieAHB
Created May 25, 2021 20:42
Show Gist options
  • Save RichieAHB/ae49b660db56b83e42158f39dbe49398 to your computer and use it in GitHub Desktop.
Save RichieAHB/ae49b660db56b83e42158f39dbe49398 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 fetchMachine = Machine({
initial: 'idle',
states: {
idle: {
on: {
INIT: 'authenticating',
},
},
authenticating: {
effect: (send, update) => {
Promise.all([fetchScatmanUser(), fetchSpotifyUser()])
.then(([scatmanUser, spotifyUser]) => {
update(_ => ({ scatmanUser, spotifyUser }));
send('DID_AUTHENTICATE');
})
.catch(() => {
send('DID_NOT_AUTHENTICATE');
});
},
on: {
DID_AUTHENTICATE: 'authenticated',
DID_NOT_AUTHENTICATE: 'unauthenticated',
},
},
authenticated: {
effect(send) {
send('VALIDATE');
},
on: {
VALIDATE: 'validating',
},
},
unauthenticated: {},
validating: {
effect: send => {
canMigrate().then(res => {
switch (res) {
case 'CAN_MIGRATE': {
send('CAN_MIGRATE');
return;
}
case 'MIGRATED': {
send('HAS_MIGRATED');
return;
}
default: {
send('CANNOT_MIGRATE');
return;
}
}
});
},
on: {
CAN_MIGRATE: 'verify',
HAS_MIGRATED: 'redirecting',
CANNOT_MIGRATE: 'forbidden',
},
},
verify: {
on: {
MIGRATE: 'migrating',
},
},
forbidden: {},
migrating: {
effect: send => {
migrate().then(res => {
if (res.ok) {
send('DID_MIGRATE');
} else {
send('DID_NOT_MIGRATE');
}
});
},
on: {
DID_MIGRATE: 'succeeded',
DID_NOT_MIGRATE: 'failed',
},
},
succeeded: {},
failed: {},
redirecting: {
effect() {
setTimeout(() => {
window.location.href = 'https://sonar.spotify.com';
}, 5000);
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment