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).
72 lines • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetworkTablesPrefixTopic = void 0;
var tslib_1 = require("tslib");
var base_topic_1 = require("./base-topic");
var NetworkTablesPrefixTopic = /** @class */ (function (_super) {
tslib_1.__extends(NetworkTablesPrefixTopic, _super);
/**
* 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 NetworkTablesPrefixTopic(client, name) {
var _this = _super.call(this, client, name) || this;
_this.type = 'prefix';
var existingTopic = _this.client.getPrefixTopicFromName(name);
if (existingTopic) {
return existingTopic;
}
_this.client.registerTopic(_this);
return _this;
}
/** */
/* SUBSCRIBING */
/** */
/**
* Creates a new subscriber.
* @param callback - The callback to call when the topic value changes.
* @param options - The options for the subscriber.
* @param id - The UID of the subscriber.
* @param save - Whether to save the subscriber.
* @returns The UID of the subscriber.
*/
NetworkTablesPrefixTopic.prototype.subscribe = function (callback, options, id, save) {
if (options === void 0) { options = {}; }
if (save === void 0) { save = true; }
var subuid = id || this.client.messenger.getNextSubUID();
var subscribeParams = {
topics: [this.name],
subuid: subuid,
options: tslib_1.__assign(tslib_1.__assign({}, options), { prefix: true }),
};
this.client.messenger.subscribe(subscribeParams);
if (save)
this.subscribers.set(subuid, { callback: callback, options: options });
return subuid;
};
NetworkTablesPrefixTopic.prototype.resubscribeAll = function (client) {
var _this = this;
this.client = client;
this.subscribers.forEach(function (info, subuid) {
_this.subscribe(info.callback, info.options, subuid, false);
});
};
/**
* Updates the value of a subtopic. Notifies all subscribers of the change.
* @param params - The params of the subtopic
* @param value - The value of the subtopic
* @param serverTime - The time the value was updated
*/
NetworkTablesPrefixTopic.prototype.updateValue = function (params, value, serverTime) {
this._lastChangedTime = serverTime;
this.notifySubscribers(params, value);
};
NetworkTablesPrefixTopic.prototype.notifySubscribers = function (params, value) {
this.subscribers.forEach(function (info) { return info.callback(value, params); });
};
return NetworkTablesPrefixTopic;
}(base_topic_1.NetworkTablesBaseTopic));
exports.NetworkTablesPrefixTopic = NetworkTablesPrefixTopic;
//# sourceMappingURL=prefix-topic.js.map