Skip to content

Instantly share code, notes, and snippets.

@zhangtaihao
Last active August 12, 2018 14:01
Show Gist options
  • Save zhangtaihao/3e458f5d11210429bc19e39ce89e087f to your computer and use it in GitHub Desktop.
Save zhangtaihao/3e458f5d11210429bc19e39ce89e087f to your computer and use it in GitHub Desktop.
Data feed algorithm
get :: retrieveCache || retrieveUpdate => data
update :: retrieveUpdate => data
subscribe(s :: data => {}) :: <addSubscriber>(s) + (retrieveCache => data) && s(data) + refresh
unsubscribe(s) :: <removeSubscriber>(s)
refresh :: !retrieveCache && retrieveUpdate ~~~> <subscribed> && refresh
retrieveCache :: !<expired> && fetchCache => data
retrieveUpdate :: <debounce>(fetchRemote || fetchCache || fetchInitial) => data
fetchCache :: loadCache => data || <error>
fetchRemote :: invokeSaveBroadcast(<fetch>) => data || <error>
fetchInitial :: (loadCache || invokeSaveBroadcast(<initial>)) => data || <error>
invokeSaveBroadcast(f :: () => data) :: (f => data) && saveCache(data) && <broadcast>(data) => data || <error>
loadCache :: <memory> || <storage>
saveCache :: <memory> && <storage>
<fetch> : Slow data source
<initial> : Initial data if uncached and fetch failed
<error> : Abort call, e.g. raising exception
<addSubscriber> : Register subscriber
<removeSubscriber> : Unregister subscriber
<subscribed> : Has registered subscribers
<broadcast> : Broadcast to registered subscribers
<resetRefresh> : ...
<debounce> : ...
<expired> : Loaded but stale
<memory> : In-memory storage, e.g. local variable
<storage> : Local persistent storage (fast), e.g. flash memory
# N.B. "Cached" means "loaded", i.e. from storage, remote or initial.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment