Skip to content

Commit

Permalink
Fix generator placeholder and optimize updates (#6105)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 authored and philippjfr committed Jan 17, 2024
1 parent a27b783 commit b00c76f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 5 additions & 1 deletion panel/chat/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ def _update_card_params(self):

@param.depends("placeholder_text", watch=True, on_init=True)
def _update_placeholder(self):
if self._placeholder is not None:
self._placeholder.param.update(object=self.placeholder_text)
return

loading_avatar = SVG(
PLACEHOLDER_SVG, sizing_mode=None, css_classes=["rotating-placeholder"]
)
Expand Down Expand Up @@ -437,7 +441,7 @@ async def _schedule_placeholder(
return

start = asyncio.get_event_loop().time()
while not task.done() and num_entries == len(self._chat_log):
while not self._callback_state == CallbackState.IDLE and num_entries == len(self._chat_log):
duration = asyncio.get_event_loop().time() - start
if duration > self.placeholder_threshold:
self.append(self._placeholder)
Expand Down
20 changes: 11 additions & 9 deletions panel/tests/chat/test_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ async def callback(contents, user, instance):
}
instance.respond()
elif user == "arm":
user_entry = instance.objects[-2]
for user_entry in instance.objects:
if user_entry.user == "User":
break
user_contents = user_entry.object
yield {
"user": "leg",
Expand Down Expand Up @@ -450,8 +452,7 @@ async def echo(contents, user, instance):

chat_feed.callback = echo
chat_feed.send("Message", respond=True)
await asyncio.sleep(0.5)
assert len(chat_feed.objects) == 2
await async_wait_until(lambda: len(chat_feed.objects) == 2)
assert chat_feed.objects[1].object == "Message"

@pytest.mark.parametrize("callback_user", [None, "Bob"])
Expand Down Expand Up @@ -500,12 +501,12 @@ async def echo(contents, user, instance):

chat_feed.callback = echo
chat_feed.send("Message", respond=True)
await asyncio.sleep(0.5)
await async_wait_until(lambda: len(chat_feed.objects) == 2)
assert len(chat_feed.objects) == 2
assert chat_feed.objects[1].object == "Message"

@pytest.mark.asyncio
async def test_generator(self, chat_feed):
def test_generator(self, chat_feed):
async def echo(contents, user, instance):
message = ""
for char in contents:
Expand All @@ -514,7 +515,7 @@ async def echo(contents, user, instance):

chat_feed.callback = echo
chat_feed.send("Message", respond=True)
await asyncio.sleep(0.5)
wait_until(lambda: len(chat_feed.objects) == 2)
assert len(chat_feed.objects) == 2
assert chat_feed.objects[1].object == "Message"

Expand All @@ -532,8 +533,7 @@ async def echo(contents, user, instance):

chat_feed.callback = echo
chat_feed.send("Message", respond=True)
await asyncio.sleep(0.5)
assert len(chat_feed.objects) == 2
await async_wait_until(lambda: len(chat_feed.objects) == 2)
assert chat_feed.objects[1].object == "Message"

def test_placeholder_disabled(self, chat_feed):
Expand Down Expand Up @@ -593,11 +593,13 @@ async def echo(contents, user, instance):

def test_placeholder_threshold_exceed_generator(self, chat_feed):
async def echo(contents, user, instance):
assert instance._placeholder not in instance._chat_log
await asyncio.sleep(0.5)
assert instance._placeholder in instance._chat_log
yield "hello testing"
assert instance._placeholder not in instance._chat_log

chat_feed.placeholder_threshold = 0.1
chat_feed.placeholder_threshold = 0.2
chat_feed.callback = echo
chat_feed.send("Message", respond=True)
assert chat_feed._placeholder not in chat_feed._chat_log
Expand Down

0 comments on commit b00c76f

Please sign in to comment.