Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. /* @flow */
  2.  
  3. type State = Object
  4.  
  5. type Action = {type: string | void}
  6. type AsyncAction = (performAction: FluxPerformFunction, state: State) => void
  7. type ActionCreator = () => Action | AsyncAction
  8.  
  9. type StoreFunction = (state: State, action: Action) => State
  10.  
  11. type StoreDefinition = {name: string, store: StoreFunction}
  12. type CombineStoresFunction = (stores: Array<StoreDefinition>) => StoreFunction
  13.  
  14. type FluxListener = (state: State) => void
  15. type FluxUnsubscribe = () => void
  16. type FluxHydro = {state: State, listeners: Array<FluxListener>}
  17. type FluxDehydrateFunction = () => FluxHydro
  18. type FluxHydrateFuntion = (hydro: FluxHydro) => void
  19. type FluxSubscribeFunction = (listener: FluxListener) => FluxUnsubscribe
  20. type FluxPerformFunction = (action: Action | AsyncAction) => void
  21. type Flux = {subscribe: FluxSubscribeFunction, perform: FluxPerformFunction, dehydrate: FluxDehydrateFunction, hydrate: FluxHydrateFuntion}
  22. type FluxCreator = (store: StoreFunction) => Flux
  23.  
  24. function syncActionCreator(): Action {
  25. return {
  26. type: 'ACTION_TYPE'
  27. }
  28. }
  29.  
  30. function asyncActionCreator(): AsyncAction {
  31. return function asyncAction(performAction: FluxPerformFunction, state: State): void {
  32. performAction(syncActionCreator())
  33. }
  34. }
  35.  
  36. function itemsStore(state: State = {}, action: Action): State {
  37. switch (action.type) {
  38. default:
  39. return state
  40. }
  41. }
  42.  
  43. function combineStores(stores: Array<StoreDefinition>): StoreFunction {
  44. return function combinedStore(state: State, action: Action): State {
  45. return stores.reduce((acc, storeDefinition) => {
  46. acc[storeDefinition.name] = storeDefinition.store(state[storeDefinition.name], action)
  47. return acc
  48. }, {})
  49. }
  50. }
  51.  
  52. function createFlux(store: StoreFunction): Flux {
  53. var state: State = store({}, {type: undefined})
  54. var listeners: Array<FluxListener> = []
  55.  
  56. function subscribe(listener: FluxListener): function {
  57. listeners.push(listener)
  58.  
  59. listener(state)
  60.  
  61. return function() {
  62. listeners = listeners.filter(l => l !== listener)
  63. }
  64. }
  65.  
  66. function dispatch(action: Action): void {
  67. state = store(state, action)
  68.  
  69. listeners.forEach(listener => listener(state))
  70. }
  71.  
  72. function perform(action: Action | AsyncAction): void {
  73. return typeof action === 'function'
  74. ? action(perform, state)
  75. : dispatch(action)
  76. }
  77.  
  78. function dehydrate() {
  79. var hydro = {state, listeners}
  80. state = {}
  81. listeners = []
  82. return hydro
  83. }
  84.  
  85. function hydrate(hydro = {}) {
  86. state = hydro.state || {}
  87. listeners = hydro.listeners || []
  88. }
  89.  
  90. return {
  91. subscribe,
  92. perform,
  93. hydrate,
  94. dehydrate,
  95. }
  96. }
  97.  
  98. var combinedStore = combineStores([
  99. {name: 'items', store: itemsStore},
  100. ])
  101. var flux = createFlux(combinedStore)
  102.  
  103. flux.perform(asyncActionCreator())
  104.  
  105. var unsubscribe = flux.subscribe(state => {
  106. console.log(state)
  107. })
  108.  
  109. unsubscribe()
  110.  
  111. var hydro = flux.dehydrate()
  112. flux.hydrate(hydro)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement