PAN Bus Monitor Fix
Problem
The PAN Bus Monitor wasn't showing any events when components interacted. Testing with the drag-drop-list example showed no messages appearing in the monitor.
Root Cause
The monitor was listening for the wrong event type. It was trying to hook into dispatchEvent and looking for pan-message events, but the actual PAN bus uses:
pan:publish- When a message is published to the buspan:deliver- When a message is delivered to a subscriber
// โ WRONG: Hooking dispatchEvent looking for 'pan-message'
bus.dispatchEvent = (event) => {
if (event.type === 'pan-message') {
this.logMessage(event.detail);
}
return this.originalDispatchEvent(event);
};
Solution
Changed to properly listen for the actual PAN bus events using document-level event listeners:
// โ
CORRECT: Listen for actual PAN bus events
this.publishHandler = (event) => {
if (event.detail && event.detail.topic) {
this.logMessage({
topic: event.detail.topic,
data: event.detail.data,
type: 'publish'
});
}
};
this.deliverHandler = (event) => {
if (event.detail && event.detail.topic) {
this.logMessage({
topic: event.detail.topic,
data: event.detail.data,
type: 'deliver'
});
}
};
document.addEventListener('pan:publish', this.publishHandler, true);
document.addEventListener('pan:deliver', this.deliverHandler, true);
How PAN Bus Actually Works
From the core PAN bus implementation (core/pan-bus.mjs):
Publishing
When a component publishes a message:bus.publish(topic, data, options);
// Dispatches: 'pan:publish' event with { topic, data, ...options }
Delivering
When the bus delivers to subscribers:target.dispatchEvent(new CustomEvent('pan:deliver', {
detail: { topic, data, id, ts }
}));
Why Two Event Types?
pan:publish- Captures the intent to send (one per publish call)pan:deliver- Captures each delivery (multiple per publish if multiple subscribers)
- What's being published (even if no subscribers)
- What's being delivered (shows the message flow)
Enhancements Made
1. Message Type Display
Messages now show whether they're publish or deliver:- ๐ค PUBLISH - Green border (message sent)
- ๐ฅ DELIVER - Blue border (message received)
2. Better Layout
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ค PUBLISH 10:30:45 AM โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ list.reorder โ
โ {"items": [...], "from": 0} โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
3. Improved Styling
- Header with type and timestamp
- Topic in monospace font
- Data with scrollable area (max 200px)
- Color-coded borders by message type
- Dark theme matching code panel
Files Changed
components/pg-bus-monitor.mjs
startMonitoring(): Listen topan:publishandpan:deliveron documentstopMonitoring(): Clean up event listeners properlyrender(): Show message type (publish/deliver) with icons and header
styles/playground.css
- Added
.bus-message-publish- Green border for published messages - Added
.bus-message-deliver- Blue border for delivered messages - Added
.bus-message-header- Header layout for type and time - Added
.bus-message-type- Styled message type badge - Updated
.bus-message-data- Scrollable area, better formatting
Testing
cd playground
python3 -m http.server 8080
open http://localhost:8080/
list.reorder)
- โ
Data is formatted and readable
- โ
Timestamps are accurate
Example Output
When dragging items in drag-drop-list, you should see:
๐ค PUBLISH 10:30:45.123
list.reorder
{"items": [{"id": 2, "text": "Item 2"}, {"id": 1, "text": "Item 1"}], "from": 0, "to": 1}
๐ฅ DELIVER 10:30:45.124
list.reorder
{"items": [{"id": 2, "text": "Item 2"}, {"id": 1, "text": "Item 1"}], "from": 0, "to": 1}
The PUBLISH shows the message being sent, and DELIVER shows it being received by subscribers.
Why This Pattern?
Using capture: true
document.addEventListener('pan:publish', handler, true);
^^^^
The true flag enables capture phase listening, which ensures we catch events before they bubble up, even if other handlers stop propagation.
Document-level Listeners
PAN bus events bubble through the document, so listening at the document level catches all messages regardless of which component sent them.Proper Cleanup
disconnectedCallback() {
this.stopMonitoring(); // Remove listeners when monitor closes
}
This prevents memory leaks when the monitor is toggled on/off.
Benefits
โ Actually works - Messages now appear in real-time โ Shows message flow - See both publish and deliver events โ Better debugging - Understand which components are communicating โ Visual distinction - Color-coded by message type โ Proper cleanup - No memory leaks
The PAN Monitor is now fully functional for debugging component communication! ๐