Created
December 9, 2019 12:20
-
-
Save Nachox07/8300d11d0ac4c8fe86f045c393dada5e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const SHOW_LOADING_SCREEN_TIME_THRESHOLD = 1000; | |
const GameLauncherMachine = { | |
id: "gameLauncher", | |
initial: "browsing", | |
states: { | |
browsing: { | |
on: { | |
LAUNCH_GAME: [ | |
{ | |
target: "launching", | |
in: "#gaming.session.loggedIn", | |
actions: "assignLaunchRequest", | |
}, | |
{ | |
actions: ["assignLaunchRequest", "displayLogin"], | |
}, | |
], | |
}, | |
}, | |
launching: { | |
invoke: { | |
src: "requestLaunchGame", | |
}, | |
on: { | |
LAUNCH_REQUEST_SUCCESS: { | |
target: "running", | |
actions: assign({ | |
launchedGame: (_, result) => result.data, | |
}), | |
}, | |
LANCH_REQUEST_FAILURE: { | |
target: "launchFailed", | |
actions: assign({ | |
error: (_, result) => result.error, | |
}), | |
}, | |
}, | |
initial: "loadingInBackground", | |
states: { | |
loadingInBackground: { | |
onEntry: [ | |
send("SHOW_LOADING_SCREEN", { | |
delay: SHOW_LOADING_SCREEN_TIME_THRESHOLD, | |
}), | |
], | |
on: { | |
SHOW_LOADING_SCREEN: { target: "loadingScreen" }, | |
}, | |
}, | |
loadingScreen: { | |
on: { | |
STOP_GAME: { | |
target: "#gameLauncher.browsing", | |
actions: ["unsetLaunchedGame"], | |
}, | |
}, | |
}, | |
}, | |
}, | |
running: { | |
entry: "displayLaunchedGame", | |
on: { | |
STOP_GAME: { | |
target: "browsing", | |
actions: ["unsetLaunchedGame"], | |
}, | |
}, | |
}, | |
launchFailed: { | |
on: { | |
LAUNCH_GAME: [ | |
{ | |
target: "launching.loadingInBackground", | |
in: "#gaming.session.loggedIn", | |
actions: ["assignLaunchRequest", "removeError"], | |
}, | |
{ | |
target: "browsing", | |
actions: ["assignLaunchRequest", "removeError", "displayLogin"], | |
}, | |
], | |
}, | |
}, | |
}, | |
}; | |
const SessionMachine = { | |
initial: "notDetermined", | |
states: { | |
notDetermined: { | |
on: { | |
AUTHENTICATE_USER: { | |
target: "loggedIn", | |
}, | |
UNAUTHENTICATE_USER: { | |
target: "loggedOut", | |
}, | |
}, | |
}, | |
loggedIn: { | |
on: { | |
UNAUTHENTICATE_USER: { | |
target: "loggedOut", | |
}, | |
}, | |
}, | |
loggedOut: { | |
on: { | |
AUTHENTICATE_USER: { | |
target: "loggedIn", | |
}, | |
}, | |
}, | |
}, | |
}; | |
const RecentlyPlayedMachine = { | |
id: "recentlyPlayed", | |
initial: "userNotLoggedIn", | |
states: { | |
userNotLoggedIn: { | |
on: { | |
AUTHENTICATE_USER: { | |
target: "recentDisplayed", | |
actions: "assignUserId", | |
}, | |
}, | |
}, | |
recentDisplayed: { | |
entry: "fetchRecentlyPlayedSync", | |
on: { | |
LAUNCH_GAME: { | |
actions: "storeRecent", | |
}, | |
UNAUTHENTICATE_USER: { | |
target: "userNotLoggedIn", | |
actions: "removeUserId", | |
}, | |
}, | |
}, | |
}, | |
}; | |
const GamesLobbyMachine = Machine({ | |
type: "parallel", | |
id: "gaming", | |
context: { | |
error: null, | |
anonymousLobbyData: null, | |
authenticatedLobbyData: null, | |
currentLobbyData: null, | |
loggedIn: false, | |
userId: null, | |
games: [], | |
launchedGame: null, | |
launchRequest: null, | |
}, | |
states: { | |
lobby: { | |
id: "lobby", | |
initial: "pending", | |
states: { | |
pending: { | |
invoke: { | |
src: "fetchGamesLobby", | |
}, | |
on: { | |
LOBBY_FETCH_SUCCESS: { | |
target: "success", | |
actions: assign({ | |
authenticatedLobbyData: (_, result) => result.data, | |
}), | |
}, | |
LOBBY_FETCH_FAILED: { | |
target: "failure", | |
actions: assign({ | |
error: (_, result) => result.data, | |
}), | |
}, | |
}, | |
}, | |
failure: {}, | |
success: { | |
id: "lobbyList", | |
type: "parallel", | |
entry: "filterLobbyBasedOnAuthState", | |
on: { | |
AUTHENTICATE_USER: { | |
actions: [ | |
assign({ | |
loggedIn: true, | |
}), | |
"filterLobbyBasedOnAuthState", | |
], | |
}, | |
UNAUTHENTICATE_USER: { | |
actions: [ | |
assign({ | |
loggedIn: false, | |
}), | |
"filterLobbyBasedOnAuthState", | |
], | |
}, | |
}, | |
states: { | |
gameLauncher: GameLauncherMachine, | |
recentlyPlayed: RecentlyPlayedMachine, | |
}, | |
}, | |
}, | |
}, | |
session: SessionMachine, | |
}, | |
}, { | |
actions: { | |
assignLaunchRequest: assign({ | |
launchRequest: (_, event) => event.launchRequest, | |
}), | |
removeError: assign({ | |
error: () => null, | |
}), | |
unsetLaunchedGame: assign({ | |
launchedGame: () => null, | |
}), | |
}, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment