Skip to content

Instantly share code, notes, and snippets.

@danielpox
Last active September 21, 2020 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielpox/e3d415bf0b0ee7440a17a609133f50be to your computer and use it in GitHub Desktop.
Save danielpox/e3d415bf0b0ee7440a17a609133f50be 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 rateLimitMachine = Machine({
id: 'limit_machine',
context: {
parent_state: ''
},
initial: 'fetch_rate_limits',
states: {
fetch_rate_limits: {
on: {
DONE: 'validate_rate_limits'
}
},
validate_rate_limits: {
on: {
OK: 'proceed',
LIMITED: 'limited'
}
},
proceed: {
type: 'final'
},
limited: {
initial: 'update_database_rate_limits',
states: {
update_database_rate_limits: {
on: {
DONE: 'check_limited_resource'
}
},
check_limited_resource: {
on: {
DONE: 'update_status'
}
},
update_status: {
meta: 'Sets the database entry status to "limited.{resource}"',
on: {
DONE: 'return_timestamp'
}
},
return_timestamp: {
meta: 'Skicka tillbaka tidstämpel för {resurs} till användaren.',
entry: sendParent('LIMITED')
}
}
}
}
})
const twuotaMachine = Machine({
id: 'twuota',
initial: 'idle',
states: {
idle: {
on: {
START: 'prepare'
}
},
prepare: {
initial: 'check_database',
states: {
check_database: {
on: {
EMPTY: 'create_entry',
EXISTS: 'resume'
}
},
create_entry: {
initial: 'create',
states: {
create: {
on: {
DONE: 'check_rate_limits'
}
},
check_rate_limits: {
invoke: {
id: 'get_rate_limits.create_entry',
src: rateLimitMachine,
data: {
parent_state: 'prepare.create_entry.check_rate_limits'
},
onDone: '#twuota.create'
},
on: {
LIMITED: '#twuota.cancelled'
}
}
}
},
resume: {
initial: 'check_status',
states: {
check_status: {
on: {
DONE: 'done',
LIMITED: 'limited'
}
},
limited: {
initial: 'get_rate_limits_db',
states: {
get_rate_limits_db: {
on: {
DONE: 'validate_rate_limits'
}
},
validate_rate_limits: {
on: {
OK: '#twuota.retry',
LIMITED: 'limited'
}
},
limited: {
type: 'final',
meta: 'Skicka tillbaka tidstämpel till användaren.'
}
},
onDone: '#twuota.cancelled'
},
done: {
initial: 'get_request_date',
states: {
get_request_date: {
on: {
OVER_7_DAYS: 'new',
RECENT: 'return_prev_data'
}
},
new: {
initial: 'cleanup',
states: {
cleanup: {
on: {
DONE: '#twuota.prepare.create_entry'
}
}
}
},
return_prev_data: {
type: 'final',
meta: 'Skicka tillbaka resultatet från sist, den sammanställda statistiken.'
}
},
onDone: '#twuota.completed'
}
}
}
}
},
create: {
initial: 'get_user',
states: {
get_user: {
initial: 'fetch',
states: {
fetch: {
on: {
DONE: '#twuota.create.get_friends_ids',
LIMITED: 'limited'
}
},
limited: {
invoke: {
id: 'get_rate_limits.get_user',
src: rateLimitMachine,
data: {
parent_state: 'create.get_user'
},
onDone: '#twuota.create.get_user'
},
on: {
LIMITED: '#twuota.cancelled'
}
}
}
},
get_friends_ids: {
initial: 'fetch',
states: {
fetch: {
on: {
DONE: '#twuota.create.get_friends',
LIMITED: 'limited'
}
},
limited: {
invoke: {
id: 'get_rate_limits.get_friends_ids',
src: rateLimitMachine,
data: {
parent_state: 'create.get_friends_ids'
},
onDone: '#twuota.create.get_friends_ids'
},
on: {
LIMITED: '#twuota.cancelled'
}
}
}
},
get_friends: {
initial: 'fetch',
states: {
fetch: {
on: {
DONE: '#twuota.process',
LIMITED: 'limited'
}
},
limited: {
invoke: {
id: 'get_rate_limits.get_friends',
src: rateLimitMachine,
data: {
parent_state: 'create.get_friends'
},
onDone: '#twuota.create.get_friends'
},
on: {
LIMITED: '#twuota.cancelled'
}
}
}
}
}
},
retry: {
initial: 'fetch_rate_limits',
states: {
fetch_rate_limits: {
invoke: {
id: 'get_rate_limits.retry',
src: rateLimitMachine,
data: {
parent_state: 'retry.fetch_rate_limits'
},
onDone: 'check_status'
},
on: {
LIMITED: '#twuota.cancelled'
}
},
check_status: {
on: {
GET_RATE_LIMITS: 'rate_limits',
GET_USER: '#twuota.create.get_user',
GET_FRIENDS_IDS: '#twuota.create.get_friends_ids',
GET_FRIENDS: '#twuota.create.get_friends',
GET_TWEETS: '#twuota.process',
}
},
rate_limits: {
on: {
CORRUPT_ENTRY: '#twuota.create',
COMPLETE: '#twuota.process'
}
}
}
},
process: {
initial: 'get_tweets',
states: {
get_tweets: {
on: {
DONE: 'sum_count',
RATE_LIMIT: 'limited'
}
},
limited: {
initial: 'cancel_requests',
states: {
cancel_requests: {
on: {
DONE: 'mark_friends_as_cancelled'
}
},
mark_friends_as_cancelled: {
on: {
DONE: 'get_rate_limits'
}
},
get_rate_limits: {
invoke: {
id: 'get_rate_limits.get_tweets',
src: rateLimitMachine,
data: {
parent_state: 'process.get_tweets'
},
onDone: '#twuota.process.get_tweets'
},
on: {
LIMITED: '#twuota.cancelled'
}
}
}
},
sum_count: {
on: {
DONE: '#twuota.statistics'
}
}
}
},
statistics: {
initial: 'make_statistics',
states: {
make_statistics: {
on: {
DONE: 'save'
}
},
save: {
on: {
DONE: 'return_stats'
}
},
return_stats: {
meta: 'Return statistics to user.',
type: 'final'
}
},
onDone: '#twuota.completed'
},
cancelled: {
meta: 'The process was aborted.',
type: 'final'
},
completed: {
meta: 'The process was completed.',
type: 'final'
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment