pydux_thunk.py 648 B

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