async def test_jscomponent_prop1(): """ 0 0 3 ---------- 0 3 """ c, s = launch(JsComponentA) # Note: set_foo() immediately sends an INVOKE command. If the # subsequent (now commented) EVAL command is not handled in the same # event loop iter, the value will already have been updated. s.send_command('EVAL', c.id, 'foo') c.set_foo(3) print(c.foo) # s.send_command('EVAL', c.id, 'foo') loop.iter() print(c.foo) # still not set await roundtrip(s) print(c.foo) s.send_command('EVAL', c.id, 'foo') await roundtrip(s)
async def test_pycomponent_reaction1(): """ 0 sub foo changed 0 0 sub foo changed 3 3 ---------- """ c1, s = launch(PyComponentA) with c1: c2 = PyComponentA() # PyComponent sub c1.set_sub(c2) print(c2.foo) loop.iter() c2.set_foo(3) print(c2.foo) loop.iter() print(c2.foo) await roundtrip(s)
def test_dynamism3(Node): n = Node() n1 = Node() n2 = Node() loop.iter() n.children = n1, n2 n.val = 42 n.handle_children_val.handle_now() n1.val = 17 n2.val = 27 n.handle_children_val.handle_now() n1.val = 18 n2.val = 28 n.handle_children_val.handle_now() n.children = (n2, ) n.handle_children_val.handle_now() n1.val = 19 n2.val = 29 n.handle_children_val.handle_now() n.children = () n.handle_children_val.handle_now() n1.val = 11 n2.val = 21 n.handle_children_val.handle_now() return n._r2
def test_dynamism5(Node): # connection strings with static attributes - no reconnect n = Node() n1 = Node() n.foo = n1 res = [] def func(*events): for ev in events: if isinstance(ev.new_value, (float, int)): res.append(ev.new_value) else: res.append('null') handler = n.connect(func, 'foo.val') loop.iter() n.val = 42 handler.handle_now() n1.val = 17 n1.val = 18 handler.handle_now() n.foo = None # no reconnect in this case handler.handle_now() n1.val = 19 handler.handle_now() return res
async def roundtrip(*sessions): """ Coroutine to await a roundtrip to all given sessions. """ ok = [] def up(): ok.append(1) for session in sessions: session.call_after_roundtrip(up) # timeout = time.time() + 5.0 while len(ok) < len(sessions): await asyncio.sleep(0.02) loop.iter()
def test_deep2(Node): # deep connectors - string ends in deep connector n = Node() n1 = Node() n2 = Node() n.children = Node(), n1 n.children[0].children = Node(), n2 loop.iter() res = [] def func(*events): for ev in events: if isinstance(ev.new_value, (float, int)): res.append(ev.new_value) elif ev.type == 'children': if ev.source.val: res.append('id%i' % ev.source.val) else: res.append('null') handler = n.connect(func, 'children**') loop.iter() # Give val to id by - these should have no effect on res though n.val = 10 handler.handle_now() n1.val = 11 handler.handle_now() n2.val = 12 handler.handle_now() # Change children n2.children = Node(), Node(), Node() n1.children = Node(), Node() n.children = Node(), n1, Node() handler.handle_now() n2.children = [] # no longer in the tree n1.children = [] handler.handle_now() return res
async def test_pycomponent_prop1(): """ 0 3 3 ---------- 0 3 """ c, s = launch(PyComponentA) c.set_foo(3) print(c.foo) s.send_command('EVAL', c.id, 'foo') loop.iter() print(c.foo) # this will mutate foo await roundtrip(s) print(c.foo) s.send_command('EVAL', c.id, 'foo') await roundtrip(s)
def test_deep1(Node): # deep connectors n = Node() n1 = Node() n2 = Node() n.children = Node(), n1 n.children[0].children = Node(), n2 loop.iter() res = [] def func(*events): for ev in events: if isinstance(ev.new_value, (float, int)): if ev.new_value: res.append(ev.new_value) else: res.append('null') handler = n.connect(func, 'children**.val') loop.iter() # We want these n1.val = 7 handler.handle_now() n2.val = 8 handler.handle_now() # But not these n.val = 42 handler.handle_now() n1.children = Node(), Node() n.children[0].children = [] # again ... n1.val = 17 handler.handle_now() n2.val = 18 # n2 is no longer in the tree handler.handle_now() return res
def test_dynamism4b(Node): n = Node() n1 = Node() n2 = Node() res = [] def func(*events): for ev in events: if isinstance(ev.new_value, (float, int)): res.append(ev.new_value) else: res.append('null') handler = n.connect(func, 'children', 'children*.val') # also connect children loop.iter() n.children = n1, n2 n.val = 42 handler.handle_now() n1.val = 17 n2.val = 27 handler.handle_now() n1.val = 18 n2.val = 28 handler.handle_now() n.children = (n2, ) handler.handle_now() n1.val = 19 n2.val = 29 handler.handle_now() n.children = () handler.handle_now() n1.val = 11 n2.val = 21 handler.handle_now() return res
def test_dynamism2b(Node): n = Node() n1 = Node() n2 = Node() res = [] def func(*events): for ev in events: if ev.type == 'val': res.append(n.parent.val) else: res.append(None) handler = n.connect(func, 'parent', 'parent.val') # also connect to parent loop.iter() n.parent = n1 n.val = 42 handler.handle_now() n1.val = 17 n2.val = 27 handler.handle_now() n1.val = 18 n2.val = 28 handler.handle_now() n.parent = n2 handler.handle_now() n1.val = 19 n2.val = 29 handler.handle_now() n.parent = None handler.handle_now() n1.val = 11 n2.val = 21 handler.handle_now() return res
def test_dynamism1(Node): n = Node() n1 = Node() n2 = Node() loop.iter() n.parent = n1 n.val = 42 loop.iter() n1.val = 17 n2.val = 27 loop.iter() n1.val = 18 n2.val = 28 loop.iter() n.parent = n2 loop.iter() n1.val = 19 n2.val = 29 loop.iter() n.parent = None loop.iter() n1.val = 11 n2.val = 21 loop.iter() return n._r1