Skip to content

Instantly share code, notes, and snippets.

@thanhpk
Created September 8, 2018 05:50
Show Gist options
  • Save thanhpk/607bbcf9bb8b47bafc5bab6d1c1354b5 to your computer and use it in GitHub Desktop.
Save thanhpk/607bbcf9bb8b47bafc5bab6d1c1354b5 to your computer and use it in GitHub Desktop.
resolver state machine
var store = require('store')
var ajax = require('@subiz/ajax')
var api = ajax.post()
const DEAD = 'dead'
const REFRESHING = 'refreshing'
const NORMAL 'normal'
const JUST_REFRESHED = 'just_refreshed'
export Token = {}
/*constructor(tokenhost) {
this.actoken = ''
this.rftoken = ''
this.api = ajax.post(tokenhost, 'refresh-token').setParser('json').setContentType('form')
this.refreshQ = []
},
*/
Token.set = (actoken, rftoken) => {
this.actoken = actoken
this.rftoken = rftoken
store.set('subiz_tokens', { refresh_token: rftoken, access_token: actoken })
}
Token.refresh = (actoken, rftoken) => {
return new Promise(resolve => this.refreshQ.push({actoken, rftoken, resolve}))
}
Token.onJustRefresh = () => new Promise(resolve => {
setTimeout(() => resolve(NORMAL), 5000)
for (;this.state === JUST_REFRESHED;) {
let req = this.refreshQ.pop()
if (req) {
req.resolve([this.actoken, this.rftoken, undefined])
}
}
}),
Token.onRefreshing = () => new Promise(resolve => {
/*refresh token*/
})
Token.onDead = () => {
for (;this.state === DEAD;) {
let req = this.refreshQ.pop()
if (req) {
req.resolve([undefined, undefined, 'i am dead'])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment