def test_dedupe_middleware(self): store = redux.Store(middlewares=[redux.dedupe]) listener = unittest.mock.Mock() store.subscribe(listener) store.dispatch("A") store.dispatch("A") listener.assert_called_once_with({"value": "A"})
async def idle(): store = redux.Store([ IdleReducer, ], cleaner_period=0.05) await store.dispatch("idle:test", redux.Action.no_op_command()) assert "idle:test" in store await asyncio.sleep(0.03) await store.dispatch("idle:test", redux.Action.no_op_command()) await asyncio.sleep(0.22) assert "idle:test" not in store await store.dispatch("idle:test2", redux.Action.no_op_command()) assert "idle:test2" in store await asyncio.sleep(0.11) assert "idle:test2" not in store handle = (await store.subscribe("idle:test3", IdleListener())).unwrap() await asyncio.sleep(0.11) assert "idle:test3" in store handle() await asyncio.sleep(0.11) assert "idle:test3" not in store handle = (await store.subscribe("idle:test4", IdleListener())).unwrap() await asyncio.sleep(0.11) assert "idle:test4" in store handle() handle = (await store.subscribe("idle:test4", IdleListener())).unwrap() await asyncio.sleep(0.11) assert "idle:test4" in store handle() await asyncio.sleep(0.11)
async def work(): store = redux.Store([PublicService, UserService, TransactionNode, UserNode]) server_opt = await redux.RemoteManager().serve_entry("127.0.0.1", 9966, store, [PublicService]) server = server_opt.unwrap() alice_socket = await websockets.connect("ws://127.0.0.1:9966/events") subscribe_action = { "type": "SUBSCRIBE", "user": "******", } await alice_socket.send(json.dumps(subscribe_action)) transfer_action = { "type": "TRANSFER", "user": "******", "from": "Alice", "to": "Bob", "change": 10 } await alice_socket.send(json.dumps(transfer_action)) await asyncio.sleep(0.1) transfer_action = { "type": "TRANSFER", "user": "******", "from": "Alice", "to": "Bob", "change": 1000 } await alice_socket.send(json.dumps(transfer_action)) await asyncio.sleep(0.1)
def test_action_log(self): log = redux.Log() store = redux.Store(middlewares=[log]) store.dispatch("A") store.dispatch("B") result = log.actions expect = ["A", "B"] self.assertEqual(expect, result)
async def work(): store = redux.Store() server_opt = await redux.RemoteManager().serve_entry("127.0.0.1", 9966, store, [TickReducer]) server = server_opt.unwrap() while True: await asyncio.sleep(2) for reducer in store.find_reducer_list_by_type(TickReducer): await store.dispatch(reducer.key, redux.Action.no_op_command())
async def send_in_socket(): redux.RemoteManager().client_url.add("ws://127.0.0.1:9906") store = redux.Store([ReducerWithSocketSend]) server_opt = await redux.RemoteManager().serve("127.0.0.1", 9906, store) server = server_opt.unwrap() await store.dispatch("1", redux.Action("todo")) asyncio.wait_for(socket_sent_future, 1) await redux.RemoteManager().stop_serve(server)
async def work(): store = redux.Store([CombineReducer]) await store.dispatch("combine:1", redux.Action("START")) await asyncio.sleep(0.2) assert timeout_future.done() await store.dispatch("combine:1", redux.Action("START")) await store.dispatch("combine:1", redux.Action("A")) await store.dispatch("combine:1", redux.Action("B")) await asyncio.sleep(0.2) assert combine_future.done()
async def fetch_state(): redux.RemoteManager().client_url.add("ws://127.0.0.1:9906") store = redux.Store([ReducerStateProvider, ReducerStateFetcher]) server_opt = await redux.RemoteManager().serve("127.0.0.1", 9906, store) await asyncio.sleep(0.11) server = server_opt.unwrap() await store.dispatch("user", redux.Action("todo")) await store.dispatch("fetcher", redux.Action("todo")) asyncio.wait_for(refetch_future, 1) await redux.RemoteManager().stop_serve(server)
async def basic_redux(): store = redux.Store([ReducerBasic]) await store.dispatch("1", redux.Action("todo")) handler = (await store.subscribe("1", L())).unwrap() await store.dispatch("1", redux.Action("NAME", name="peter")) handler() await store.dispatch("1", redux.Action("NAME", name="john")) handler = (await store.subscribe("1", L())).unwrap() await store.dispatch("1", redux.Action("NAME", name="bob")) await store.dispatch("1", redux.Action("NAME", name="jim")) await store.dispatch("1", redux.Action("NAME", name="tony"))
def test_dedupe_multiple_calls(self): store = redux.Store(middlewares=[redux.dedupe]) listener = unittest.mock.Mock() store.subscribe(listener) store.dispatch("A") store.dispatch("B") store.dispatch("C") calls = [ unittest.mock.call({"value": "A"}), unittest.mock.call({"value": "B"}), unittest.mock.call({"value": "C"}) ] listener.assert_has_calls(calls)
def test_log_before_dedupe(self): log = redux.Log() store = redux.Store(middlewares=[log, redux.dedupe]) listener = unittest.mock.Mock() store.subscribe(listener) store.dispatch("A") store.dispatch("B") store.dispatch("B") store.dispatch("C") result = log.actions expect = ["A", "B", "B", "C"] self.assertEqual(expect, result) calls = [ unittest.mock.call({"value": "A"}), unittest.mock.call({"value": "B"}), unittest.mock.call({"value": "C"}) ] listener.assert_has_calls(calls)
async def work(): store = redux.Store() store.insert_reducer_type(LoginVerify) server_opt = await redux.RemoteManager().serve_entry("127.0.0.1", 9966, store, [LoginReducer]) server = server_opt.unwrap() client_socket = await websockets.connect("ws://127.0.0.1:9966/login/entry/test") login_action = { "type": "LOGIN", "userName": "******", "password": "******", } await client_socket.send(json.dumps(login_action)) await asyncio.sleep(0.2) login_action = { "type": "LOGIN", "userName": "******", "password": "******", } await client_socket.send(json.dumps(login_action)) await asyncio.sleep(0.2)
def test_observer(self): store = redux.Store() listener = unittest.mock.Mock() store.subscribe(listener) store.dispatch("A") listener.assert_called_once_with({"value": "A"})
async def local_subscribe(): store = redux.Store( [LocalMediumSubscribeReducer, LocalMediumListenerReducer]) await store.dispatch("listener:1", redux.Action("sub")) await store.dispatch("sub:local:1", redux.Action("setName")) await asyncio.sleep(5)
import pprint import dataclasses import redux from State import State import actions import my_selectors store = redux.Store(State()) def print_state(): pprint.pprint(dataclasses.asdict(store.state)) print() def do_actions(actions_list): for action in actions_list: print(action) store.dispatch(action) print() def main(): print_state_unsubscribe = store.subscribe(print_state) actions1 = [ actions.UpdateOperation(actions.OperationNames.SCALE), actions.SetMatrix.scale(3), redux.stack_actions.Push(), ]
async def send_in_process(): store = redux.Store([ReducerWithSend]) await store.dispatch("1", redux.Action("todo"))