Skip to content

Commit

Permalink
feat(overlay): Add direct event sending w/o sidecar
Browse files Browse the repository at this point in the history
Working towards #133, supersedes #498
  • Loading branch information
BYK committed Aug 27, 2024
1 parent b2b5c85 commit de0f845
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/nasty-lizards-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@spotlightjs/overlay': minor
---

Add direct event ingestion through `Spotlight.sendEvent('<content-type>', <event>)` to allow sending events without the
sidecar
15 changes: 10 additions & 5 deletions packages/overlay/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function App({
}
}

const result: [contentType: string, listener: (event: MessageEvent) => void][] = [];
const result: Record<string, (event: MessageEvent) => void> = Object.create(null);
for (const [contentType, integrations] of contentTypeToIntegrations.entries()) {
const listener = (event: MessageEvent): void => {
log(`Received new ${contentType} event`);
Expand All @@ -67,10 +67,10 @@ export default function App({
}
};

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

// `contentType` could for example be "application/x-sentry-envelope"
result.push([contentType, listener]);
result[contentType] = listener;
}
return result;
}, [integrations]);
Expand Down Expand Up @@ -139,8 +139,11 @@ export default function App({
navigate(e.detail);
};

return { clearEvents, onOpen, onClose, onNavigate, onToggle };
}, [integrations, navigate, sidecarUrl]);
const onEvent = ({ detail }: CustomEvent<{ contentType: string; event: MessageEvent }>) =>
contentTypeListeners[detail.contentType]?.(detail.event);

return { clearEvents, onEvent, onOpen, onClose, onNavigate, onToggle };
}, [integrations, navigate, sidecarUrl, contentTypeListeners]);

useKeyPress(['ctrlKey', 'F12'], eventHandlers.onToggle);

Expand All @@ -150,13 +153,15 @@ export default function App({
spotlightEventTarget.addEventListener('close', eventHandlers.onClose);
spotlightEventTarget.addEventListener('navigate', eventHandlers.onNavigate as EventListener);
spotlightEventTarget.addEventListener('clearEvents', eventHandlers.clearEvents as EventListener);
spotlightEventTarget.addEventListener('event', eventHandlers.onEvent as EventListener);

return (): undefined => {
log('useEffect[destructor]: Removing event listeners');
spotlightEventTarget.removeEventListener('open', eventHandlers.onOpen as EventListener);
spotlightEventTarget.removeEventListener('close', eventHandlers.onClose);
spotlightEventTarget.removeEventListener('navigate', eventHandlers.onNavigate as EventListener);
spotlightEventTarget.removeEventListener('clearEvents', eventHandlers.clearEvents as EventListener);
spotlightEventTarget.removeEventListener('event', eventHandlers.onEvent as EventListener);
};
}, [spotlightEventTarget, eventHandlers]);

Expand Down
21 changes: 13 additions & 8 deletions packages/overlay/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,35 @@ function createStyleSheet(styles: string) {
/**
* Open the Spotlight debugger Window
*/
export async function openSpotlight(path?: string | undefined) {
export function openSpotlight(path?: string | undefined) {
trigger('open', { path });
}

/**
* Close the Spotlight debugger Window
*/
export async function closeSpotlight() {
export function closeSpotlight() {
trigger('close');
}

/**
* Invokes the passed in callback when the Spotlight debugger Window is closed
*/
export async function onClose(cb: EventListener) {
export function onClose(cb: EventListener) {
on('closed', cb);
}

/**
* Send an event to spotlight without the sidecar
*/
export function sendEvent(contentType: string, event: MessageEvent) {
trigger('event', { contentType, event });
}

/**
* Invokes the passed in callback when the Spotlight debugger Window is opened
*/
export async function onOpen(cb: EventListener) {
export function onOpen(cb: EventListener) {
on('opened', cb);
}

Expand All @@ -69,10 +76,8 @@ export async function onOpen(cb: EventListener) {
* by a Spotlight integration.
* A count of the number of collected severe events is passed to the callback.
*/
export async function onSevereEvent(cb: (count: number) => void) {
on('severeEventCount', e => {
cb((e as CustomEvent).detail?.count ?? 1);
});
export function onSevereEvent(cb: (count: number) => void) {
on('severeEventCount', e => cb((e as CustomEvent).detail?.count ?? 1));
}

function isSpotlightInjected() {
Expand Down
13 changes: 7 additions & 6 deletions packages/overlay/src/sidecar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { log } from './lib/logger';

export function connectToSidecar(
sidecarUrl: string,
contentTypeListeners: [contentType: string, listener: (event: MessageEvent) => void][],
// Content Type to listener
contentTypeListeners: Record<string, (event: MessageEvent) => void>,
setOnline: React.Dispatch<React.SetStateAction<boolean>>,
): () => void {
log('Connecting to sidecar at', sidecarUrl);
const source = new EventSource(sidecarUrl);

for (const [contentType, listener] of contentTypeListeners) {
for (const [contentType, listener] of Object.entries(contentTypeListeners)) {
source.addEventListener(contentType, listener);
}

Expand All @@ -24,10 +25,10 @@ export function connectToSidecar(
});

return () => {
log(`Removing ${contentTypeListeners.length} listeners`);
for (const typeAndListener of contentTypeListeners) {
source.removeEventListener(typeAndListener[0], typeAndListener[1]);
log('Removed listener for type', typeAndListener[0]);
log('Removing all content type listeners');
for (const [contentType, listener] of Object.entries(contentTypeListeners)) {
source.removeEventListener(contentType, listener);
log('Removed listener for type', contentType);
}
};
}

0 comments on commit de0f845

Please sign in to comment.