From 134df9dbaafdb8cd7c41c1652f75520c0fa9d7d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Brand=C3=A9ho?= Date: Sat, 5 Aug 2023 01:39:46 +0200 Subject: [PATCH] fix lost events when yielding (#1521) --- .../jinja/web/pages/index.js.jinja2 | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/reflex/.templates/jinja/web/pages/index.js.jinja2 b/reflex/.templates/jinja/web/pages/index.js.jinja2 index 96310bb58..bfdb052b4 100644 --- a/reflex/.templates/jinja/web/pages/index.js.jinja2 +++ b/reflex/.templates/jinja/web/pages/index.js.jinja2 @@ -49,25 +49,29 @@ export default function Component() { processEvent({{state_name}}, {{state_name|react_setter}}, {{const.result}}, {{const.result|react_setter}}, {{const.router}}, {{const.socket}}.current) } - // If there is a new result, update the state. - if ({{const.result}}.{{const.state}} != null) { - // Apply the new result to the state and the new events to the queue. - {{state_name|react_setter}}(state => ({ - ...{{const.result}}.{{const.state}}, - events: [...state.{{const.events}}, ...{{const.result}}.{{const.events}}], - })) + // Reset the result. + {{const.result|react_setter}}(result => { + // If there is a new result, update the state. + if ({{const.result}}.{{const.state}} != null) { + // Apply the new result to the state and the new events to the queue. + {{state_name|react_setter}}(state => { + return { + ...{{const.result}}.{{const.state}}, + events: [...state.{{const.events}}, ...{{const.result}}.{{const.events}}], + } + }) + return { + {{const.state}}: null, + {{const.events}}: [], + {{const.final}}: true, + {{const.processing}}: !{{const.result}}.{{const.final}}, + } + } + return result; + }) - // Reset the result. - {{const.result|react_setter}}(result => ({ - {{const.state}}: null, - {{const.events}}: [], - {{const.final}}: true, - {{const.processing}}: !{{const.result}}.{{const.final}}, - })) - - // Process the next event. - processEvent({{state_name}}, {{state_name|react_setter}}, {{const.result}}, {{const.result|react_setter}}, {{const.router}}, {{const.socket}}.current) - } + // Process the next event. + processEvent({{state_name}}, {{state_name|react_setter}}, {{const.result}}, {{const.result|react_setter}}, {{const.router}}, {{const.socket}}.current) }) // Set focus to the specified element.