pydux_thunk.py 505 B

12345678910111213141516171819
  1. import asyncio
  2. import inspect
  3. def middleware(store):
  4. dispatch = store['dispatch']
  5. get_state = store['get_state']
  6. def wrapper(next_):
  7. def thunk_dispatch(action):
  8. if callable(action):
  9. if inspect.iscoroutinefunction(action):
  10. print('found async action')
  11. return loop.call_soon_threadsafe(asyncio.ensure_future, action(dispatch, get_state))
  12. else:
  13. print('found sync action')
  14. return action(dispatch, get_state)
  15. return next_(action)
  16. return thunk_dispatch
  17. return wrapper