def go(): initial_state = { 'todos': (), } def add_todo(text): return {'type': 'ADD_TODO', 'text': text} def add_todo_complex(): @coroutine def thunk(dispatch, state_func): yield from dispatch({'type': 'ADD_TODO', 'text': 'todo text'}) return 19 return thunk def reducer(state, action): if action['type'] == 'ADD_TODO': todos = state['todos'] + (action['text'],) return toolz.assoc(state, 'todos', todos) return state create_store_partial = functools.partial(aioredux.create_store, loop=self.loop) create_store_with_middleware = aioredux.apply_middleware(aioredux.middleware.thunk_middleware)(create_store_partial) # noqa store = yield from create_store_with_middleware(reducer, initial_state) self.assertIsNotNone(store.state) self.assertIsNotNone(store.state['todos']) self.assertEqual(len(store.state['todos']), 0) # use a thunk return_value = yield from store.dispatch(add_todo_complex()) self.assertEqual(return_value, 19) self.assertIsNotNone(store.state) self.assertIsNotNone(store.state['todos']) self.assertEqual(len(store.state['todos']), 1)
def go(): initial_state = { 'todos': (), } def add_todo(text): return {'type': 'ADD_TODO', 'text': text} def reducer(state, action): if action['type'] == 'ADD_TODO': todos = state['todos'] + (action['text'], ) return toolz.assoc(state, 'todos', todos) return state create_store_partial = functools.partial(aioredux.create_store, loop=self.loop) create_store_with_middleware = aioredux.apply_middleware( aioredux.middleware.thunk_middleware)( create_store_partial) # noqa store = yield from create_store_with_middleware( reducer, initial_state) self.assertIsNotNone(store.state) self.assertIsNotNone(store.state['todos']) self.assertEqual(len(store.state['todos']), 0) @asyncio.coroutine def thunk(dispatch, state_func): yield from dispatch(add_todo('todo text')) yield from store.dispatch(thunk) self.assertIsNotNone(store.state) self.assertIsNotNone(store.state['todos']) self.assertEqual(len(store.state['todos']), 1)
async def run_async(driver, config, loop): media_handler = asyncio.ensure_future(handle_media_changes()) duty_logger = asyncio.ensure_future(driver.track_duty()) media_dir = config.get('files', 'media_dir') state = await initial_state.read(media_dir) width, height = driver.get_dimensions() state = state.copy(app=state['app'].copy( display=frozendict({ 'width': width, 'height': height }))) thunk_middleware = aioredux.middleware.thunk_middleware create_store = aioredux.apply_middleware(thunk_middleware)( aioredux.create_store) store = await create_store(main_reducer, state) await store.dispatch(actions.load_books('start')) store.subscribe(handle_changes(driver, config, store)) # since handle_changes subscription happens after init and library.sync it # may not have triggered. so we trigger it here. if we put it before init # it will start of by rendering a possibly invalid state. library.sync # won't dispatch if the library is already in sync so there would be no # guarantee of the subscription triggering if subscribed before that. await store.dispatch(actions.trigger()) try: while 1: state = store.state if (await handle_hardware(driver, state, store, media_dir)): media_handler.cancel() try: await media_handler except asyncio.CancelledError: pass duty_logger.cancel() try: await duty_logger except asyncio.CancelledError: pass break await buttons.check(driver, state['app'], store.dispatch) await display.send_line(driver) # in the emulated driver we can be too agressive in checking buttons # and sending lines if we don't have any delay if not isinstance(driver, Pi): await asyncio.sleep(0.01) else: await asyncio.sleep(0) except asyncio.CancelledError: media_handler.cancel() duty_logger.cancel() raise
def run(): thunk_middleware = aioredux.middleware.thunk_middleware create_store_with_middleware = aioredux.apply_middleware(thunk_middleware)(aioredux.Store) store = create_store_with_middleware(todo_app, initial_state) store.subscribe(lambda: logging.info("new state: {}".format(store.state))) store.dispatch(fetch_todo()) for i in range(5): store.dispatch(add_todo('do task {}'.format(i))) store.dispatch(complete_todo(1)) store.dispatch(complete_todo(2)) logging.info('Finished')
def run(): thunk_middleware = aioredux.middleware.thunk_middleware create_store_with_middleware = aioredux.apply_middleware(thunk_middleware)( aioredux.Store) store = create_store_with_middleware(todo_app, initial_state) store.subscribe(lambda: logging.info("new state: {}".format(store.state))) store.dispatch(fetch_todo()) for i in range(5): store.dispatch(add_todo('do task {}'.format(i))) store.dispatch(complete_todo(1)) store.dispatch(complete_todo(2)) logging.info('Finished')
def go(): initial_state = { 'todos': (), } def add_todo(text): return {'type': 'ADD_TODO', 'text': text} def add_todo_thunk_nested(): @coroutine def thunk(dispatch, state_func): yield from dispatch({ 'type': 'ADD_TODO', 'text': 'todo text' }) return 5 return thunk def add_todo_thunk(): @coroutine def thunk(dispatch, state_func): yield from dispatch(add_todo_thunk_nested()) return 3 return thunk def reducer(state, action): if action['type'] == 'ADD_TODO': todos = state['todos'] + (action['text'], ) return toolz.assoc(state, 'todos', todos) return state create_store_partial = functools.partial(aioredux.create_store, loop=self.loop) create_store_with_middleware = aioredux.apply_middleware( aioredux.middleware.thunk_middleware)( create_store_partial) # noqa store = yield from create_store_with_middleware( reducer, initial_state) self.assertEqual(len(store.state['todos']), 0) # first make sure the nested thunk works as expected return_value = yield from store.dispatch(add_todo_thunk_nested()) self.assertEqual(return_value, 5) self.assertEqual(len(store.state['todos']), 1) # now test the thunk dispatching a thunk return_value = yield from store.dispatch(add_todo_thunk()) self.assertEqual(return_value, 3) self.assertEqual(len(store.state['todos']), 2)
def go(): initial_state = { 'todos': (), } def add_todo(text): return {'type': 'ADD_TODO', 'text': text} def add_todo_thunk_nested(): @coroutine def thunk(dispatch, state_func): yield from dispatch({'type': 'ADD_TODO', 'text': 'todo text'}) return 5 return thunk def add_todo_thunk(): @coroutine def thunk(dispatch, state_func): yield from dispatch(add_todo_thunk_nested()) return 3 return thunk def reducer(state, action): if action['type'] == 'ADD_TODO': todos = state['todos'] + (action['text'],) return toolz.assoc(state, 'todos', todos) return state create_store_partial = functools.partial(aioredux.create_store, loop=self.loop) create_store_with_middleware = aioredux.apply_middleware(aioredux.middleware.thunk_middleware)(create_store_partial) # noqa store = yield from create_store_with_middleware(reducer, initial_state) self.assertEqual(len(store.state['todos']), 0) # first make sure the nested thunk works as expected return_value = yield from store.dispatch(add_todo_thunk_nested()) self.assertEqual(return_value, 5) self.assertEqual(len(store.state['todos']), 1) # now test the thunk dispatching a thunk return_value = yield from store.dispatch(add_todo_thunk()) self.assertEqual(return_value, 3) self.assertEqual(len(store.state['todos']), 2)
async def run_async(driver, config, loop): library_dir = config.get('files', 'library_dir') state = await initial_state.read(library_dir) width, height = driver.get_dimensions() state = state.copy(app=state['app'].copy( display=frozendict({'width': width, 'height': height}))) thunk_middleware = aioredux.middleware.thunk_middleware create_store = aioredux.apply_middleware( thunk_middleware)(aioredux.create_store) store = await create_store(main_reducer, state) await store.dispatch(actions.load_books('start')) store.subscribe(handle_changes(driver, config, store)) # if we startup and update_ui is still 'in progress' then we are using the # old state file and update has failed if state['app']['update_ui'] == 'in progress': await store.dispatch(actions.update_ui('failed')) # since handle_changes subscription happens after init and library.sync it # may not have triggered. so we trigger it here. if we put it before init # it will start of by rendering a possibly invalid state. library.sync # won't dispatch if the library is already in sync so there would be no # guarantee of the subscription triggering if subscribed before that. await store.dispatch(actions.trigger()) while 1: state = store.state if (await handle_hardware(driver, state, store, library_dir)): break await buttons.check(driver, state['app'], store.dispatch) await display.send_line(driver) await asyncio.sleep(0)
async def run_async(driver, config, loop): media_dir = config.get('files', 'media_dir') state = await initial_state.read(media_dir) width, height = driver.get_dimensions() state = state.copy(app=state['app'].copy( display=frozendict({'width': width, 'height': height}))) thunk_middleware = aioredux.middleware.thunk_middleware create_store = aioredux.apply_middleware( thunk_middleware)(aioredux.create_store) store = await create_store(main_reducer, state) await store.dispatch(actions.load_books('start')) store.subscribe(handle_changes(driver, config, store)) # since handle_changes subscription happens after init and library.sync it # may not have triggered. so we trigger it here. if we put it before init # it will start of by rendering a possibly invalid state. library.sync # won't dispatch if the library is already in sync so there would be no # guarantee of the subscription triggering if subscribed before that. await store.dispatch(actions.trigger()) while 1: state = store.state if (await handle_hardware(driver, state, store, media_dir)): break await buttons.check(driver, state['app'], store.dispatch) await display.send_line(driver) # in the emulated driver we can be too agressive in checking buttons # and sending lines if we don't have any delay if not isinstance(driver, Pi): await asyncio.sleep(0.01) else: await asyncio.sleep(0)
def go(): initial_state = { 'todos': (), } def add_todo(text): return {'type': 'ADD_TODO', 'text': text} def add_todo_slow(): @coroutine def thunk(dispatch, state_func): yield from asyncio.sleep(0.01, loop=self.loop) yield from dispatch(add_todo('slow todo')) return thunk def reducer(state, action): if action['type'] == 'ADD_TODO': todos = state['todos'] + (action['text'],) return toolz.assoc(state, 'todos', todos) return state create_store_partial = functools.partial(aioredux.create_store, loop=self.loop) create_store_with_middleware = aioredux.apply_middleware(aioredux.middleware.thunk_middleware)(create_store_partial) # noqa store = yield from create_store_with_middleware(reducer, initial_state) self.assertIsNotNone(store.state) self.assertIsNotNone(store.state['todos']) self.assertEqual(len(store.state['todos']), 0) yield from store.dispatch(add_todo('first todo')) self.assertIsNotNone(store.state) self.assertEqual(len(store.state['todos']), 1) yield from store.dispatch(add_todo_slow()) yield from asyncio.sleep(0.02, loop=self.loop) self.assertIsNotNone(store.state) self.assertEqual(len(store.state['todos']), 2)