Skip to content

Commit a3207e2

Browse files
committed
fix doc examples
1 parent dd28505 commit a3207e2

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

docs/source/core-concepts.rst

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ whose body contains a hook usage. We'll demonstrate that with a simple
4040
import idom
4141

4242

43-
@idom.component
44-
def ClickCount():
43+
def use_counter():
4544
count, set_count = idom.hooks.use_state(0)
45+
return count, lambda: set_count(lambda old_count: old_count + 1)
46+
4647

48+
@idom.component
49+
def ClickCount():
50+
count, increment_count = use_counter()
4751
return idom.html.button(
48-
{"onClick": lambda event: set_count(count + 1)},
52+
{"onClick": lambda event: increment_count()},
4953
[f"Click count: {count}"],
5054
)
5155

@@ -79,15 +83,17 @@ which we can re-render and see what changed:
7983

8084
static_handler = StaticEventHandler()
8185

86+
8287
@idom.component
8388
def ClickCount():
84-
count, set_count = idom.hooks.use_state(0)
89+
count, increment_count = use_counter()
8590

8691
# we do this in order to capture the event handler's target ID
87-
handler = static_handler.use(lambda event: set_count(count + 1))
92+
handler = static_handler.use(lambda event: increment_count())
8893

8994
return idom.html.button({"onClick": handler}, [f"Click count: {count}"])
9095

96+
9197
with idom.Layout(ClickCount()) as layout:
9298
patch_1 = await layout.render()
9399

@@ -124,27 +130,28 @@ callback that's called by the dispatcher to collect events it should execute.
124130
import asyncio
125131

126132
from idom.core.layout import LayoutEvent
127-
from idom.core.dispatch import dispatch_single_view
133+
from idom.core.dispatcher import dispatch_single_view
128134

129135

130136
sent_patches = []
131137

138+
# We need this to simulate a scenario in which events ariving *after* each update
139+
# has been sent to the client. Otherwise the events would all arive at once and we
140+
# would observe one large update rather than many discrete updates.
141+
sempahore = asyncio.Semaphore(0)
142+
132143

133144
async def send(patch):
134145
sent_patches.append(patch)
146+
sempahore.release()
135147
if len(sent_patches) == 5:
136148
# if we didn't cancel the dispatcher would continue forever
137149
raise asyncio.CancelledError()
138150

139151

140152
async def recv():
153+
await sempahore.acquire()
141154
event = LayoutEvent(target=static_handler.target, data=[{}])
142-
143-
# We need this so we don't flood the render loop with events.
144-
# In practice this is never an issue since events won't arrive
145-
# as quickly as in this example.
146-
await asyncio.sleep(0)
147-
148155
return event
149156

150157

0 commit comments

Comments
 (0)