Skip to content

Commit

Permalink
Merge branch 'main' into fn/trigger-add-event
Browse files Browse the repository at this point in the history
  • Loading branch information
BYK committed Aug 27, 2024
2 parents b2fafea + b2b5c85 commit 3d775b8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
44 changes: 37 additions & 7 deletions packages/overlay/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,57 @@ export default function App({
const [isOpen, setOpen] = useState(openOnInit);
log('App rerender', integrationData, isOnline, triggerButtonCount, isOpen);

// Map that holds the information which kind of content type should be dispatched to which integration(s)
const contentTypeToIntegrations = useMemo(() => {
const result = new Map<string, Integration[]>();
const contentTypeListeners = useMemo(() => {
// Map that holds the information which kind of content type should be dispatched to which integration(s)
const contentTypeToIntegrations = new Map<string, Integration[]>();
for (const integration of integrations) {
if (!integration.forwardedContentType) continue;

for (const contentType of integration.forwardedContentType) {
let integrationsForContentType = result.get(contentType);
let integrationsForContentType = contentTypeToIntegrations.get(contentType);
if (!integrationsForContentType) {
integrationsForContentType = [];
result.set(contentType, integrationsForContentType);
contentTypeToIntegrations.set(contentType, integrationsForContentType);
}
integrationsForContentType.push(integration);
}
}

const result: [contentType: string, listener: (event: MessageEvent) => void][] = [];
for (const [contentType, integrations] of contentTypeToIntegrations.entries()) {
const listener = (event: MessageEvent): void => {
log(`Received new ${contentType} event`);
for (const integration of integrations) {
const newIntegrationData = integration.processEvent
? integration.processEvent({
contentType,
data: event.data,
})
: { event };

if (!newIntegrationData) {
continue;
}

const integrationName = integration.name;
setIntegrationData(prev => ({
...prev,
[integrationName]: [...(prev[integrationName] || []), newIntegrationData],
}));
}
};

log('Adding listener for', contentType, 'sum', result.length);

// `contentType` could for example be "application/x-sentry-envelope"
result.push([contentType, listener]);
}
return result;
}, [integrations]);

useEffect(
() => connectToSidecar(sidecarUrl, contentTypeToIntegrations, setIntegrationData, setOnline) as () => undefined,
[sidecarUrl, contentTypeToIntegrations],
() => connectToSidecar(sidecarUrl, contentTypeListeners, setOnline) as () => undefined,
[sidecarUrl, contentTypeListeners],
);

const spotlightEventTarget = useMemo(() => getSpotlightEventTarget(), []);
Expand Down
35 changes: 2 additions & 33 deletions packages/overlay/src/sidecar.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,15 @@
import type React from 'react';
import type { Integration, IntegrationData } from './integrations/integration';
import { log } from './lib/logger';

export function connectToSidecar(
sidecarUrl: string,
contentTypeToIntegrations: Map<string, Integration<unknown>[]>,
setIntegrationData: React.Dispatch<React.SetStateAction<IntegrationData<unknown>>>,
contentTypeListeners: [contentType: string, listener: (event: MessageEvent) => void][],
setOnline: React.Dispatch<React.SetStateAction<boolean>>,
): () => void {
log('Connecting to sidecar at', sidecarUrl);
const source = new EventSource(sidecarUrl);

const contentTypeListeners: [contentType: string, listener: (event: MessageEvent) => void][] = [];

for (const [contentType, integrations] of contentTypeToIntegrations.entries()) {
const listener = (event: MessageEvent): void => {
log(`Received new ${contentType} event`);
for (const integration of integrations) {
const integrationData = integration.processEvent
? integration.processEvent({
contentType,
data: event.data,
})
: { event };

if (!integrationData) {
continue;
}
setIntegrationData(prev => {
const integrationName = integration.name;
return {
...prev,
[integrationName]: [...(prev[integrationName] || []), integrationData],
};
});
}
};

log('Adding listener for', contentType, 'sum', contentTypeListeners.length);

// `contentType` could for example be "application/x-sentry-envelope"
contentTypeListeners.push([contentType, listener]);
for (const [contentType, listener] of contentTypeListeners) {
source.addEventListener(contentType, listener);
}

Expand Down

0 comments on commit 3d775b8

Please sign in to comment.