project.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Import dependencies
  2. import pydux
  3. import asyncio
  4. import time
  5. import pydux_thunk
  6. import pydux_sqlite
  7. # Import modules
  8. import counter
  9. import calculate
  10. # Define the async loop
  11. loop = asyncio.get_event_loop()
  12. pydux_thunk.loop = loop
  13. # Combine the middleware
  14. middleware = [
  15. pydux_thunk.middleware,
  16. pydux_sqlite.middleware
  17. ]
  18. middleware = pydux.apply_middleware(*middleware)
  19. # Combine the state
  20. initial_state = {
  21. 'counter': counter.initial_state,
  22. 'calculate': calculate.initial_state
  23. }
  24. # Combine the reducers
  25. root_reducer = pydux.combine_reducers({
  26. 'counter': counter.reducer,
  27. 'calculate': calculate.reducer
  28. })
  29. # Create the store
  30. store = pydux.create_store(root_reducer, initial_state, middleware)
  31. def my_subscriber():
  32. """
  33. Dummy subscriber, just prints the state.
  34. :return: Nothing
  35. """
  36. print('Current state', store.get_state())
  37. # Subscribe to the store
  38. store.subscribe(my_subscriber)
  39. print('starting.')
  40. store.dispatch(counter.ActionCreators.increment())
  41. store.dispatch(counter.ActionCreators.increment())
  42. store.dispatch(counter.ActionCreators.increment())
  43. store.dispatch(calculate.ActionCreators.calculate(3, 4))
  44. time.sleep(1)
  45. store.dispatch(calculate.ActionCreators.calculate(5, 0))
  46. time.sleep(1)
  47. store.dispatch(calculate.ActionCreators.calculate(9, 3))
  48. store.dispatch(counter.ActionCreators.increment())
  49. store.dispatch(counter.ActionCreators.increment())
  50. store.dispatch(counter.ActionCreators.increment())
  51. store.dispatch(counter.ActionCreators.increment())
  52. loop.run_forever()