@@ -134,11 +134,9 @@ ever be removed from the model. Then you'll just need to call and await a
134
134
135
135
return idom.html.button({"onClick": increment}, [f"Click count: {count}"])
136
136
137
-
138
137
click_count = ClickCount(0)
139
- layout = idom.Layout(click_count)
140
- update = await layout.render()
141
-
138
+ async with idom.Layout(click_count) as layout:
139
+ update = await layout.render()
142
140
143
141
assert update.src == click_count.id
144
142
assert update.new == {
@@ -174,13 +172,17 @@ method. Then we just have to re-render the layout and see what changed:
174
172
175
173
from idom.core.layout import LayoutEvent
176
174
177
- event_handler_id = update.new[click_count.id]["eventHandlers"]["onClick"]["target"]
178
- dummy_event = LayoutEvent(event_handler_id, [{}])
175
+ click_count = ClickCount(0)
176
+ async with idom.Layout(click_count) as layout:
177
+ first_udpate = await layout.render() # same as above
178
+
179
+ event_handler_id = first_udpate.new[click_count.id]["eventHandlers"]["onClick"]["target"]
180
+ dummy_event = LayoutEvent(event_handler_id, [{}])
179
181
180
- await layout.trigger(dummy_event)
182
+ await layout.trigger(dummy_event)
183
+ second_update = await layout.render()
181
184
182
- new_update = await layout.render()
183
- assert new_update.new[click_count.id]["children"][0]["data"] == "Click count: 1"
185
+ assert second_update.new[click_count.id]["children"][0]["data"] == "Click count: 1"
184
186
185
187
186
188
Layout Renderer
@@ -201,7 +203,6 @@ callback that's called by the renderer to events it should execute.
201
203
from idom.core import SingleStateRenderer, EventHandler
202
204
from idom.core.layout import LayoutEvent
203
205
204
- layout = idom.Layout(ClickCount(0))
205
206
sent_updates = []
206
207
207
208
@@ -224,11 +225,9 @@ callback that's called by the renderer to events it should execute.
224
225
return event
225
226
226
227
227
- renderer = SingleStateRenderer(layout)
228
-
229
- context = None # see note below
230
-
231
- await renderer.run(send, recv, context)
228
+ async with SingleStateRenderer(idom.Layout(ClickCount(0))) as renderer:
229
+ context = None # see note below
230
+ await renderer.run(send, recv, context)
232
231
233
232
assert len(sent_updates) == 5
234
233
0 commit comments