project.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. pydux_sqlite.thread.start()
  14. # Combine the middleware
  15. middleware = [
  16. pydux_thunk.middleware,
  17. pydux_sqlite.middleware
  18. ]
  19. middleware = pydux.apply_middleware(*middleware)
  20. # Combine the state
  21. initial_state = {
  22. 'counter': counter.initial_state,
  23. 'calculate': calculate.initial_state
  24. }
  25. # Combine the reducers
  26. root_reducer = pydux.combine_reducers({
  27. 'counter': counter.reducer,
  28. 'calculate': calculate.reducer
  29. })
  30. # Create the store
  31. store = pydux.create_store(root_reducer, initial_state, middleware)
  32. def my_subscriber():
  33. """
  34. Dummy subscriber, just prints the state.
  35. :return: Nothing
  36. """
  37. print('Current state', store.get_state())
  38. # Subscribe to the store
  39. store.subscribe(my_subscriber)
  40. print('starting.')
  41. store.dispatch(counter.ActionCreators.increment())
  42. store.dispatch(counter.ActionCreators.increment())
  43. store.dispatch(counter.ActionCreators.increment())
  44. store.dispatch(calculate.ActionCreators.calculate(3, 4))
  45. time.sleep(1)
  46. store.dispatch(calculate.ActionCreators.calculate(5, 0))
  47. time.sleep(1)
  48. store.dispatch(calculate.ActionCreators.calculate(9, 3))
  49. store.dispatch(counter.ActionCreators.increment())
  50. store.dispatch(counter.ActionCreators.increment())
  51. store.dispatch(counter.ActionCreators.increment())
  52. store.dispatch(counter.ActionCreators.increment())
  53. loop.run_forever()
  54. pydux_sqlite.thread.join()