Skip to content

Commit

Permalink
added fetch message by receiver id
Browse files Browse the repository at this point in the history
  • Loading branch information
yu-jeffy committed Jan 6, 2025
1 parent 9d2fb53 commit 469e494
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,22 @@ def fetch_all(self) -> List[MessagePayload]:
return current_messages
except Exception as e:
logger.error(f"Error fetching messages: {e}")
return []
return []

def fetch_for_receiver(self, receiver_id: str) -> List[MessagePayload]:
"""
Retrieve and remove all messages for a given receiver_id.
If receiver_id is None, it means broadcast messages.
"""
with self.lock:
matching = []
remaining = []
for m in self.messages:
# Include messages where receiver_id matches or is any broadcast
if m.receiver_id is None or m.receiver_id == receiver_id:
matching.append(m)
else:
remaining.append(m)

self.messages = remaining
return matching

0 comments on commit 469e494

Please sign in to comment.