project.py 1.7 KB

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