Last active
December 15, 2020 18:59
-
-
Save Zlass/889cdcd1957235119bc13eebca7615dd 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 AppStateMonitorServiceId = { | |
AppStateEventListener : 'AppStateEventListener', | |
} | |
const AppStateMonitorStates ={ | |
Active : 'active', | |
Background : 'background', | |
Extension : 'extension', | |
Unknown : 'unknown', | |
} | |
const ActiveSubStates ={ | |
Focused : 'focused', | |
Blurred : 'blurred', | |
} | |
const AppStateMonitorEventType= { | |
Changed : 'changed', | |
// Android ONLY when a system UI menu is opened | |
Focused : 'focused', | |
Blurred : 'blurred', | |
} | |
const AppStateMonitorGuards = { | |
IsIOS : 'is_ios', | |
IsAndroid : 'is_android', | |
IsInactiveState : 'is_inactive_state', | |
IsActiveState : 'is_active_state', | |
IsActiveStateAndIOS : 'IS_ACTIVE_STATE_AND_iOS', | |
IsBackgroundState : 'is_background_state', | |
IsUnknownState : 'is_unknown_state', | |
IsExtensionState : 'is_extension_state', | |
} | |
const AppStateMonitorAction = { | |
NotifyAppInFocus : 'app_in_focus', | |
NotifyAppOutOfFocus : 'app_out_of_focus', | |
NotifyAppInBackground : 'app_in_background', | |
} | |
const fetchMachine = Machine({ | |
id: 'AppStateMonitor', | |
initial: AppStateMonitorStates.Active, | |
context: { | |
os: 'ios', | |
}, | |
invoke: { | |
src: AppStateMonitorServiceId.AppStateEventListener, | |
}, | |
on: { | |
[AppStateMonitorEventType.Changed]: [ | |
{ | |
cond: AppStateMonitorGuards.IsActiveState, | |
target: AppStateMonitorStates.Active, | |
actions: (_, event) => console.log(AppStateMonitorStates.Active, event.state), | |
}, | |
{ | |
cond: AppStateMonitorGuards.IsBackgroundState, | |
target: AppStateMonitorStates.Background, | |
actions: (_, event) => console.log(AppStateMonitorStates.Background, event.state), | |
}, | |
{ | |
cond: AppStateMonitorGuards.IsExtensionState, | |
target: AppStateMonitorStates.Extension, | |
actions: (_, event) => console.log(AppStateMonitorStates.Extension, event.state), | |
}, | |
{ | |
target: AppStateMonitorStates.Unknown, | |
actions: (_, event) => console.log(AppStateMonitorStates.Unknown, event.state), | |
}, | |
], | |
}, | |
states: { | |
[AppStateMonitorStates.Active]: { | |
initial: ActiveSubStates.Focused, | |
states: { | |
[ActiveSubStates.Focused]: { | |
entry: AppStateMonitorAction.NotifyAppInFocus, | |
on: { | |
[AppStateMonitorEventType.Blurred]: { | |
target: ActiveSubStates.Blurred, | |
}, | |
[AppStateMonitorEventType.Changed]: { | |
cond: AppStateMonitorGuards.IsInactiveState, | |
target: ActiveSubStates.Blurred, | |
}, | |
}, | |
}, | |
[ActiveSubStates.Blurred]: { | |
entry: AppStateMonitorAction.NotifyAppOutOfFocus, | |
on: { | |
[AppStateMonitorEventType.Focused]: { | |
target: ActiveSubStates.Focused, | |
}, | |
[AppStateMonitorEventType.Changed]: { | |
cond: AppStateMonitorGuards.IsActiveStateAndIOS, | |
target: ActiveSubStates.Focused, | |
}, | |
}, | |
}, | |
}, | |
}, | |
[AppStateMonitorStates.Background]: { | |
entry: AppStateMonitorAction.NotifyAppInBackground, | |
}, | |
[AppStateMonitorStates.Extension]: {}, | |
[AppStateMonitorStates.Unknown]: {}, | |
}, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment