Created
November 5, 2019 20:23
-
-
Save matbee-eth/f9db2f26c176589d250765fc5d940c6b 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
const grantedState = { | |
id: 'granted', | |
type: 'final' | |
} | |
const deniedState = { | |
id: 'denied', | |
type: 'final' | |
} | |
const blockedState = { | |
invoke: { | |
id: 'doRequest', | |
src: () => requestNotifications(), | |
onDone: { | |
target: 'querying', | |
actions: assign({ response: (ctx, event) => event.data.status }) | |
}, | |
onError: { | |
target: 'unavailable', | |
actions: assign({ response: (ctx, event) => event.data.status }) | |
} | |
} | |
} | |
const unavailableState = { | |
id: 'unavailable', | |
type: 'final' | |
} | |
const queryingState = { | |
on: { | |
GRANTED: 'granted', | |
DENIED: 'denied', | |
BLOCKED: 'blocked', | |
UNAVAILABLE: 'unavailable' | |
} | |
} | |
const idleState = { | |
invoke: { | |
id: 'doQuery', | |
src: () => { | |
return checkNotifications() | |
.then((permission) => { | |
if (permission.status == "blocked" || permission.status == "granted") { | |
return Promise.resolve(permission.status) | |
} else { | |
return Promise.reject(permission.status) | |
} | |
}) | |
}, | |
onDone: { | |
target: '' | |
}, | |
onError: { | |
target: 'unavailable' | |
} | |
}, | |
on: { | |
QUERYING: 'querying' | |
} | |
} | |
let requestPermission = () => { | |
requestNotifications(['alert', 'sound', 'badge', 'lockScreen']).then(({status, settings}) => { | |
// … | |
}); | |
} | |
const initialState = { | |
id: 'toggle', | |
initial: 'idle', | |
context: { | |
msg: '' | |
}, | |
states: { | |
idle: {...idleState}, | |
granted: {...grantedState}, | |
denied: {...deniedState}, | |
blocked: {...blockedState}, | |
unavailable: {...unavailableState}, | |
querying: {...queryingState} | |
} | |
} | |
Machine(initialState) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment