ntcore-ts-client
Version:
A TypeScript library for communication over [WPILib's NetworkTables 4.1 protocol](https://github.com/wpilibsuite/allwpilib/blob/main/ntcore/doc/networktables4.adoc).
146 lines • 5.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetworkTablesBaseTopic = void 0;
var tslib_1 = require("tslib");
var NetworkTablesBaseTopic = /** @class */ (function () {
/**
* Creates a new topic. This should only be done after the
* base NTCore client has been initialized.
* @param client - The client that owns the topic.
* @param name - The name of the topic.
*/
function NetworkTablesBaseTopic(client, name) {
this.client = client;
this._name = name;
this._announceParams = null;
this._subscribers = new Map();
}
NetworkTablesBaseTopic.prototype.isRegular = function () {
return this.type === 'regular';
};
NetworkTablesBaseTopic.prototype.isPrefix = function () {
return this.type === 'prefix';
};
Object.defineProperty(NetworkTablesBaseTopic.prototype, "id", {
/**
* Gets the ID of the topic.
* @returns The ID of the topic.
*/
get: function () {
return this._id;
},
enumerable: false,
configurable: true
});
Object.defineProperty(NetworkTablesBaseTopic.prototype, "name", {
/**
* Gets the name of the topic.
* @returns The name of the topic.
*/
get: function () {
return this._name;
},
enumerable: false,
configurable: true
});
Object.defineProperty(NetworkTablesBaseTopic.prototype, "lastChangedTime", {
/**
* Gets the server time of the last value change.
* @returns The server time of the last value change.
*/
get: function () {
return this._lastChangedTime;
},
enumerable: false,
configurable: true
});
Object.defineProperty(NetworkTablesBaseTopic.prototype, "announced", {
/**
* Whether the topic has been announced.
* @returns Whether the topic has been announced.
*/
get: function () {
return this._announceParams != null;
},
enumerable: false,
configurable: true
});
Object.defineProperty(NetworkTablesBaseTopic.prototype, "subscribers", {
/**
* Gets the subscribers to the topic.
* @returns The subscribers to the topic.
*/
get: function () {
return this._subscribers;
},
enumerable: false,
configurable: true
});
/** */
/* ANNOUNCEMENTS */
/** */
/**
* Marks the topic as announced. This should only be called by the PubSubClient.
* @param params - The parameters of the announcement.
*/
NetworkTablesBaseTopic.prototype.announce = function (params) {
this._announceParams = params;
this._id = params.id;
};
/** Marks the topic as unannounced. This should only be called by the PubSubClient. */
NetworkTablesBaseTopic.prototype.unannounce = function () {
this._announceParams = null;
this._id = undefined;
};
/**
* Removes a subscriber
* @param subuid - The UID of the subscriber.
* @param removeCallback - Whether to remove the callback. Leave this as true unless you know what you're doing.
*/
NetworkTablesBaseTopic.prototype.unsubscribe = function (subuid, removeCallback) {
if (removeCallback === void 0) { removeCallback = true; }
this.client.messenger.unsubscribe(subuid);
if (removeCallback)
this.subscribers.delete(subuid);
};
/**
* Removes all local subscribers.
*/
NetworkTablesBaseTopic.prototype.unsubscribeAll = function () {
var _this = this;
this.subscribers.forEach(function (_, subuid) { return _this.unsubscribe(subuid); });
};
/** */
/* PUBLISHING */
/** */
/**
* Sets the properties of the topic.
* @param persistent - If true, the last set value will be periodically saved to persistent storage on the server and be restored during server startup. Topics with this property set to true will not be deleted by the server when the last publisher stops publishing.
* @param retained - Topics with this property set to true will not be deleted by the server when the last publisher stops publishing.
* @returns The server's response.
*/
NetworkTablesBaseTopic.prototype.setProperties = function (persistent, retained) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var setPropertiesParams;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
setPropertiesParams = {
name: this.name,
update: {
persistent: persistent,
retained: retained,
},
};
return [4 /*yield*/, this.client.messenger.setProperties(setPropertiesParams)];
case 1:
// Send the set properties request
return [2 /*return*/, _a.sent()];
}
});
});
};
return NetworkTablesBaseTopic;
}());
exports.NetworkTablesBaseTopic = NetworkTablesBaseTopic;
//# sourceMappingURL=base-topic.js.map