bug fixes, and performance improvements, and drawer
This commit is contained in:
+14
-6
@@ -1,5 +1,9 @@
|
||||
import { createLiveTopicText } from './live-topic-text';
|
||||
|
||||
type StreamSink = Pick<ReadableStreamDefaultController, 'enqueue'>;
|
||||
|
||||
interface StreamConnection {
|
||||
controller: ReadableStreamDefaultController;
|
||||
controller: StreamSink;
|
||||
lastPing: number;
|
||||
subscribedAt: number;
|
||||
}
|
||||
@@ -91,9 +95,13 @@ export const userEvents = {
|
||||
};
|
||||
|
||||
const topicSubscribers = new Map<string, Set<StreamConnection>>();
|
||||
const liveTopicText = createLiveTopicText();
|
||||
|
||||
export const topicEvents = {
|
||||
subscribe(topicId: string, controller: ReadableStreamDefaultController) {
|
||||
applyLiveText: liveTopicText.apply,
|
||||
clearLiveText: liveTopicText.removeMessage,
|
||||
|
||||
subscribe(topicId: string, controller: StreamSink) {
|
||||
if (!topicSubscribers.has(topicId)) {
|
||||
topicSubscribers.set(topicId, new Set());
|
||||
}
|
||||
@@ -120,7 +128,7 @@ export const topicEvents = {
|
||||
(conn as any).pingInterval = pingInterval;
|
||||
},
|
||||
|
||||
unsubscribe(topicId: string, controller: ReadableStreamDefaultController) {
|
||||
unsubscribe(topicId: string, controller: StreamSink) {
|
||||
const connections = topicSubscribers.get(topicId);
|
||||
if (!connections) return;
|
||||
|
||||
@@ -137,14 +145,14 @@ export const topicEvents = {
|
||||
},
|
||||
|
||||
async emit(topicId: string, events: Array<{ type: string; payload: any; timestamp?: number }> | { type: string; payload: any; timestamp?: number }) {
|
||||
const connections = topicSubscribers.get(topicId);
|
||||
if (!connections || connections.size === 0) return;
|
||||
|
||||
if (!Array.isArray(events)) {
|
||||
events = [events];
|
||||
}
|
||||
|
||||
for (const event of events) {
|
||||
liveTopicText.update(topicId, event);
|
||||
const connections = topicSubscribers.get(topicId);
|
||||
if (!connections || connections.size === 0) continue;
|
||||
const timestamp = event.timestamp ?? Date.now();
|
||||
const data = `data: ${JSON.stringify({ ...event, timestamp })}\n\n`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user