Skip to content

Commit a6e371a

Browse files
committed
use effect refactoring
1 parent 83cc49b commit a6e371a

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

src/reactpy/core/hooks.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,29 @@ def use_effect(
137137
hook = current_hook()
138138
dependencies = _try_to_infer_closure_values(function, dependencies)
139139
memoize = use_memo(dependencies=dependencies)
140-
last_clean_callback: Ref[_EffectCleanFunc | None] = use_ref(None)
140+
unmount_func: Ref[_EffectCleanFunc | None] = use_ref(None)
141141

142-
def add_effect(function: _SyncEffectFunc) -> None:
142+
def decorator(func: _SyncEffectFunc) -> None:
143143
async def effect(stop: asyncio.Event) -> None:
144-
if last_clean_callback.current is not None:
145-
last_clean_callback.current()
146-
last_clean_callback.current = None
147-
clean = last_clean_callback.current = function()
144+
if unmount_func.current:
145+
unmount_func.current()
146+
unmount_func.current = None
147+
148+
# Execute the effect and store the clean-up function
149+
unmount = unmount_func.current = func()
150+
151+
# Run the clean-up function when the effect is stopped
148152
await stop.wait()
149-
if clean is not None:
150-
clean()
153+
if unmount:
154+
unmount()
151155

152156
return memoize(lambda: hook.add_effect(effect))
153157

154-
if function is not None:
155-
add_effect(function)
158+
# Handle decorator usage
159+
if function:
160+
decorator(function)
156161
return None
157-
158-
return add_effect
162+
return decorator
159163

160164

161165
@overload
@@ -193,40 +197,44 @@ def use_async_effect(
193197
hook = current_hook()
194198
dependencies = _try_to_infer_closure_values(function, dependencies)
195199
memoize = use_memo(dependencies=dependencies)
196-
last_clean_callback: Ref[_EffectCleanFunc | None] = use_ref(None)
200+
unmount_func: Ref[_EffectCleanFunc | None] = use_ref(None)
197201

198-
def add_effect(function: _AsyncEffectFunc) -> None:
202+
def decorator(func: _AsyncEffectFunc) -> None:
199203
def sync_executor() -> _EffectCleanFunc | None:
200-
task = asyncio.create_task(function())
204+
task = asyncio.create_task(func())
201205

202-
def clean_future() -> None:
206+
def unmount_executor() -> None:
203207
if not task.cancel():
204208
try:
205-
clean = task.result()
209+
unmount = task.result()
206210
except asyncio.CancelledError:
207211
pass
208212
else:
209-
if clean is not None:
210-
clean()
213+
if unmount:
214+
unmount()
211215

212-
return clean_future
216+
return unmount_executor
213217

214218
async def effect(stop: asyncio.Event) -> None:
215-
if last_clean_callback.current is not None:
216-
last_clean_callback.current()
217-
last_clean_callback.current = None
218-
clean = last_clean_callback.current = sync_executor()
219+
if unmount_func.current:
220+
unmount_func.current()
221+
unmount_func.current = None
222+
223+
# Execute the effect and store the clean-up function
224+
unmount = unmount_func.current = sync_executor()
225+
226+
# Run the clean-up function when the effect is stopped
219227
await stop.wait()
220-
if clean is not None:
221-
clean()
228+
if unmount:
229+
unmount()
222230

223231
return memoize(lambda: hook.add_effect(effect))
224232

225-
if function is not None:
226-
add_effect(function)
233+
# Handle decorator usage
234+
if function:
235+
decorator(function)
227236
return None
228-
229-
return add_effect
237+
return decorator
230238

231239

232240
def use_debug_value(

0 commit comments

Comments
 (0)