UNPKG

@foxglove/ros1

Version:

Standalone TypeScript implementation of the ROS 1 (Robot Operating System) protocol with a pluggable transport layer

74 lines 2.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Publication = void 0; const SubscriberLink_1 = require("./SubscriberLink"); class Publication { constructor(name, md5sum, dataType, // eslint-disable-next-line @foxglove/no-boolean-parameters latching, messageDefinition, messageDefinitionText, messageWriter) { this._latched = new Map(); this._subscribers = new Map(); this.name = name; this.md5sum = md5sum; this.dataType = dataType; this.latching = latching; this.messageDefinition = messageDefinition; this.messageDefinitionText = messageDefinitionText; this.messageWriter = messageWriter; } subscribers() { return this._subscribers; } addSubscriber(connectionId, destinationCallerId, client) { const subscriber = new SubscriberLink_1.SubscriberLink(connectionId, destinationCallerId, client); this._subscribers.set(connectionId, subscriber); client.on("close", () => { this._subscribers.delete(connectionId); }); } async write(transportType, data) { if (this.latching) { this._latched.set(transportType, data); } const tasks = []; for (const sub of this._subscribers.values()) { if (sub.client.transportType() === transportType) { // A defensive copy of the data is needed here. The // source data array gets "detached". tasks.push(sub.client.write(new Uint8Array(data))); } } await Promise.allSettled(tasks); } close() { for (const sub of this._subscribers.values()) { sub.client.close(); } this._subscribers.clear(); } latchedMessage(transportType) { return this._latched.get(transportType); } getInfo() { return Array.from(this._subscribers.values()).map((sub) => { return [ sub.connectionId, sub.destinationCallerId, "o", sub.client.transportType(), this.name, 1, sub.client.getTransportInfo(), ]; }); } getStats() { const subStats = Array.from(this._subscribers.values()).map((sub) => { const stats = sub.client.stats(); return [sub.connectionId, stats.bytesSent, stats.bytesSent, stats.messagesSent, 0]; }); return [this.name, subStats]; } } exports.Publication = Publication; //# sourceMappingURL=Publication.js.map