fix(buffer): live counter

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-02-11 21:38:26 +01:00
parent 848f475412
commit 3205ea0a31
6 changed files with 56 additions and 18 deletions

View File

@@ -6,8 +6,8 @@ import { getSuperJson } from '@openpanel/common';
import type { IServiceEvent, Notification } from '@openpanel/db';
import {
TABLE_NAMES,
eventBuffer,
getEvents,
getLiveVisitors,
getProfileByIdCached,
transformMinimalEvent,
} from '@openpanel/db';
@@ -82,20 +82,20 @@ export function wsVisitors(
if (channel === 'event:received') {
const event = getSuperJson<IServiceEvent>(message);
if (event?.projectId === params.projectId) {
getLiveVisitors(params.projectId).then((count) => {
eventBuffer.getActiveVisitorCount(params.projectId).then((count) => {
connection.socket.send(String(count));
});
}
}
};
const pmessage = (pattern: string, channel: string, message: string) => {
if (!message.startsWith('live:visitors:')) {
if (!message.startsWith('live:visitor:')) {
return null;
}
const [projectId] = getLiveEventInfo(message);
if (projectId && projectId === params.projectId) {
getLiveVisitors(params.projectId).then((count) => {
eventBuffer.getActiveVisitorCount(params.projectId).then((count) => {
connection.socket.send(String(count));
});
}

View File

@@ -1,12 +1,12 @@
import withSuspense from '@/hocs/with-suspense';
import { getLiveVisitors } from '@openpanel/db';
import { eventBuffer } from '@openpanel/db';
import type { LiveCounterProps } from './live-counter';
import LiveCounter from './live-counter';
async function ServerLiveCounter(props: Omit<LiveCounterProps, 'data'>) {
const count = await getLiveVisitors(props.projectId);
const count = await eventBuffer.getActiveVisitorCount(props.projectId);
return <LiveCounter data={count} {...props} />;
}

View File

@@ -39,7 +39,7 @@ export default function useWS<T>(
onMessage(event) {
try {
const data = getSuperJson<T>(event.data);
if (data) {
if (data !== null && data !== undefined) {
debouncedOnMessage(data);
}
} catch (error) {