Skip to content
This repository was archived by the owner on Aug 13, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/jdom/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ import {
REGISTER_POLL_REPORT_VOLATILE_MAX_INTERVAL,
REGISTER_POLL_REPORT_VOLATILE_INTERVAL,
SRV_INFRASTRUCTURE,
CONNECTION_STATE,
PACKET_RECEIVE_NO_DEVICE,
FRAME_PROCESS,
FRAME_SEND,
CONNECTION_STATE,
} from "./constants"
import { serviceClass } from "./pretty"
import { JDNode } from "./node"
Expand Down Expand Up @@ -309,21 +309,6 @@ export class JDBus extends JDNode {
// the purpose of this code is to orchestrate
// interactions with multiple tabs and windows
const channel = new BroadcastChannel("jacdac")
const postConnectionState = () => {
channel.postMessage({
id: this.selfDevice.shortId,
event: CONNECTION_STATE,
transports: this._transports.map(tr => ({
type: tr.type,
connectionState: tr.connectionState,
})),
})
}
// update other windows with connection status
const unsubConnectionState = this.subscribe(
CONNECTION_STATE,
postConnectionState
)
this._postBroadcastMessage = (
event: BusBroadcastMessageType,
msg: Partial<BusBroadcastMessage>
Expand All @@ -336,8 +321,23 @@ export class JDBus extends JDNode {
//console.debug(`jacdac broadcast: ${bmsg.event}`, bmsg)
channel.postMessage(bmsg)
}
const handleConnectionState = () => {
this._postBroadcastMessage?.("connectionstate", <
BusBroadcastConnectionStateMessage
>{
transports: this._transports.map(tr => ({
type: tr.type,
connectionState: tr.connectionState,
})),
})
}
// update other windows with connection status
const unsubConnectionState = this.subscribe(
CONNECTION_STATE,
handleConnectionState
)
const handleVisibilityChange = () =>
this._postBroadcastMessage("visibilitychange", <
this._postBroadcastMessage?.("visibilitychange", <
BusBroadcastVisibilityChangeMessage
>{
visibilityState: document.visibilityState,
Expand Down Expand Up @@ -381,7 +381,9 @@ export class JDBus extends JDNode {
)
.forEach(ctr => {
this.transports
.filter(tr => tr.type === ctr.type)
.filter(
tr => !tr.shared && tr.type === ctr.type
)
.forEach(tr => tr.disconnect())
})
}
Expand Down
1 change: 1 addition & 0 deletions src/jdom/transport/nodesocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class NodeSocketTransport extends Transport {
options?: NodeSocketTransportOptions
) {
super(NODESOCKET_TRANSPORT, options)
this.shared = true;
}

protected transportConnectAsync(background?: boolean): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions src/jdom/transport/nodespi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class SpiTransport extends Transport {
readonly options: SpiTransportOptions
) {
super("spi", options)
this.shared = true
this.handleRxPinRising = this.handleRxPinRising.bind(this)
this.handleTxPinRising = this.handleTxPinRising.bind(this)

Expand Down
5 changes: 5 additions & 0 deletions src/jdom/transport/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export abstract class Transport extends JDEventSource {
].filter(c => !!c)
}

/**
* Allows multiple clients
*/
shared: boolean;

get bus() {
return this._bus
}
Expand Down
1 change: 1 addition & 0 deletions src/jdom/transport/websockettransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class WebSocketTransport extends Transport {

constructor(readonly url: string, options?: WebSocketTransportOptions) {
super(WEBSOCKET_TRANSPORT, options)
this.shared = true
this.protocols = options?.protocols
this.on(FRAME_SEND_DISCONNECT, this.handleSendDisconnect.bind(this))
}
Expand Down
3 changes: 3 additions & 0 deletions src/jdom/transport/workertransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ class WorkerTransport extends Transport {
public readonly worker: Worker,
public readonly options: {
requestDevice: () => Promise<string>
shared: boolean
} & TransportOptions
) {
super(type, options)
this.shared = options.shared
this.worker.addEventListener("message", this.handleMessage.bind(this))
}

Expand Down Expand Up @@ -119,6 +121,7 @@ export function createUSBWorkerTransport(worker: Worker) {
isWebUSBEnabled() &&
new WorkerTransport(USB_TRANSPORT, worker, {
checkPulse: true,
shared: false,
requestDevice: () =>
usbRequestDevice(USB_FILTERS).then(dev => dev?.serialNumber),
connectObservable: new EventTargetObservable(
Expand Down