diff --git a/README.md b/README.md index 1ac4b30..eec0ed1 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,29 @@ await autoModeTopic.publish(); autoModeTopic.setValue('25 Ball Auto and Climb'); ``` +### Subscribing to Announced Topics + +You can register a callback to know when your topics are announced: + +```typescript +const ntcore = NetworkTables.getInstanceByTeam(973); +ntcore.addOnTopicAnnouncedListener((topic) => { + console.log(`Topic announced: ${topic.name}`); +}); +``` + +By supplying `true` to the `announceAll` parameter, all topics published to NetworkTables (from any client) will also be announced: + +```typescript +const ntcore = NetworkTables.getInstanceByTeam(973); +ntcore.addOnTopicAnnouncedListener((topic) => { + console.log(`Topic announced: ${topic.name}`); + topic.subscribe((value) => console.log(`Got ${topic.name} value: ${value}`)); +}, true); +``` + +This will allow you to subscribe to any or all topics from the robot, which is useful for dynamic dashboard applications. + ### More info The API for Topics is much more exhaustive than this quick example. Feel free to view the docs at [https://ntcore.chrislawson.dev](https://ntcore.chrislawson.dev). diff --git a/docs/assets/navigation.js b/docs/assets/navigation.js index a105119..7a5cb2d 100644 --- a/docs/assets/navigation.js +++ b/docs/assets/navigation.js @@ -1 +1 @@ -window.navigationData = "data:application/octet-stream;base64,H4sIAAAAAAAAE53UTU8CMRAG4P/SMxEhwQ9uGi8eRBLwZDx01wEalm7T6UY3xv9uAkTcdobZet333WenbbqvXyrAZ1BTNYPwUfvtUhcVoBoop8NGTVVZaUTAYSe+2IRdpQZqa+y7mo7GN98D2lnWzpQCtu/0F1sHj3ZVSyP+9s7J98Zq3z4Bol7Dgw76hBobwK90CThMWl1yPLn6Q95ZWze2hGP9BIbWAQ6jOJIub69HkzGvzbXXOxTMQ0mSO4uKxU4oSYzR823y0GKLLGXLycalDcmc+9qBDwaQWXVSyBbpM2Zqot4UlcENN2wnzbOYMYmO5C4giNtKdf7j0lPzTfEbTYGlNwV33eM812PmJVu97WcXTG159ZhL3v6/fdq3mItiUWsdzJpdohwe93l7Ebyxawo4JJLxYvX5f3dSyBbp42Rqsu7O3u44z/W4YamWbKNwU9JGvslNTPdo/+0HtFSZ1hIJAAA=" \ No newline at end of file +window.navigationData = "data:application/octet-stream;base64,H4sIAAAAAAAACp3UTU8CMRAG4P/SMxEhwQ9uGi8eRBLwZDx01wEalm7T6UY3xv9uAkTcdobZet333WenbbqvXyrAZ1BTNYPwUfvtUhcVoBoop8NGTVVZaUTAYSe+2IRdpQZqa+y7mo7GN98D2lnWzpQCtu/0F1sHj3ZVSyP+9s7J98Zq3z4Bol7Dgw76hBobwK90CThMWl1yPLn6Q95ZWze2hGP9BIbWAQ6jOJIub69HkzGvzbXXOxTMQ0mSO4uKxU4oSYzR823y0GKLLGXLycalDcmc+9qBDwaQWXVSyBbpM2Zqot4UlcENN2wnzbOYMYmO5C4giNtKdf7j0lPzTfEbTYGlNwV33eM812PmJVu97WcXTG159ZhL3v6/fdq3mItiUWsdzJpdohwe93l7Ebyxawo4JJLxYvX5f3dSyBbp42Rqsu7O3u44z/W4YamWbKNwU9JGvslNTPdo/+0HtFSZ1hIJAAA=" \ No newline at end of file diff --git a/docs/assets/search.js b/docs/assets/search.js index 7ae5446..1958e10 100644 --- a/docs/assets/search.js +++ b/docs/assets/search.js @@ -1 +1 @@ -window.searchData = "data:application/octet-stream;base64,H4sIAAAAAAAAE62bTW/bSBKG/wtz5TruT1q6JTMXA7vZYCYzF8EIaIl2CMukQFKeNQz/90VTlFjFKllFUacEZtdb1c2nulrd7LeoKv+po/niLXrKi1U0V/omjor0OYvm0bes+aesnn6k9+usjuJoW62jebRcp3Wd1Z/R06tfzfM6ivcPo3kUvcd7TX1te9HHrLkt6iYtltnX1x9Z+iwQ/sQZAVdxtEmrrGhIzJIY/vrjdnQIO5uREXitrD3EsFznob3A86HltB7L+3lW76C35a+0eMyEDmHjaT38XlaiAe2bTvCX13+U92XzW1kU2bLJVhLHjM3FIsiLx/Eh7IwmxJCuVkivLP6d101WZJUkmA+tx0XlnTP+ENbPn83rJpsWwtXxR/9SVwcPgjA/UPrg0dHkqrK0yX6Um3w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9PJmlM7z/rCvV+u/qH2LrD1HbdMdgPaazvSazog079vtPyY+lwCtZLzWqt1x7vU8eJeJjJHd9aJtlaO4QFgyle4OVi8BRkk2SGhvHgABhJTZ2XoZp7urHc3uLgZQVEBRiZQesybvLrfdvzbt/TqgB+TGqw1GXoM0kopt2stagIJew0s1BmHAvJFKdFvgoC/g1WkZBDmeDEAYShZHXsMZD88tINlkuYbF2tPXXg1MVLJ56ikNB2D33dEpeF2gl17Wy6f7/ak8kAH1wctyfy+T7k7mgBaYALxsBnhadaf0QAVkhpelRqdCA4JMy6B+yvdn9kAGpJeX5ddehoYEcPIynp7q7gwfqIAc8bIc6VRoQIBIL0MyXBraFYBV057ZgdwD3VOy7nEFGKhYmcruwAsEAlhUMhaL3RFu013M7rVAisgyBCnRSgJwUjKcsODrJsvb40pQN8GIGeGIcZp4zQLWBU5WPIkoFgT56GT5uDl8GMVxAvTsmXrMUg2uY2ToHD6iB28ETGNGNo2Ba2iAFvASlOwldDrMgBmQ7EaW7FiMjpYFklYsucUlW4MU07IcAzcXQP/g+lE2L4LLJ2m4fALEQFBGFtTuQ4bhpJgAFBJZwtdZswEfDIKgQNk3srKPtLgsAv200n5SSSaRgLATC9MFIVzdCoevv1wFdMAcqWVz5EGHGzbwVq0swYdyzJABUTdStNzfPAAxgqGzI4euQkoaBKZlgbWFD+d5AohIRqjwueDA1ONkUw9XOxWoIsrvDBPZ5BHkii36XWeAmpHVpLZOkjWeAWXIyMoQvFQJXh2ISMsi6oU48EFgdmxgx8kHE5uTTWzg6hwYNoC8kSF/0OEqJii/RlZ+h3JMzQSiVirKz2kgBbQsBYDSoOBpEJYeHRZHCijHVpZRVJBBBbxiJ3zF7V1rWloAdFoGHdFIQDSJQOMujjb5JlvnRRbNF3fv7/8HipGYPS5KAAA="; \ No newline at end of file +window.searchData = "data:application/octet-stream;base64,H4sIAAAAAAAACr2bXW/byA6G/4t6q00z30nu2t2bAOd0i93u3hhBodhqKsSRDEnuniDofz8Y+UOkSNuUZ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hemCEORLLRy+/uAY0AE5Usty5F6HGzZwV61sgg/lmCEDom6kaLU7VQFiBENnRw5djZQ0fMSQBdYVPjzPAyAijFDh54IDqcfLUg9XOxWoIupmYxhkySPKlWv0XGeAmpHVpK5OkjWeAWXIyMoQPDAKbh2ISMsi6oU48EFgdmxgh8mHuweyxAaOBYJhg1sIMuT3OlxnQfm1svI7lGP6CkSdVJTPaWAKGNkUAErDggfCMqPD4gYPbgjIZhQVZIYP3GInvMXdOXJaWgB0WgYd0QggmiDQeEiTVbHKl0WZJ3ezh58//w/2Ql4ZhUwAAA=="; \ No newline at end of file diff --git a/docs/classes/NetworkTables.html b/docs/classes/NetworkTables.html index 7dac1d9..f8f1944 100644 --- a/docs/classes/NetworkTables.html +++ b/docs/classes/NetworkTables.html @@ -1,6 +1,7 @@ NetworkTables | ntcore-ts-client

NetworkTables class for interacting with NetworkTables over a WebSocket connection.

-

Accessors

Methods

Accessors

Methods

Methods

  • Adds a listener for when topics are announced

    +

    Parameters

    • callback: ((_: NetworkTablesTopic<any>) => void)

      A callback triggered with the announced topic

      +
    • OptionalannounceAll: boolean

      Whether to trigger all topics in NetworkTables to be announced

      +

    Returns (() => void)

    A function to remove the listener.

    +
      • (): void
      • Returns void

  • Adds a listener for robot connection status updates.

    Parameters

    • callback: ((_: boolean) => void)

      The callback to call when the connection status changes.

        • (_): void
        • Parameters

          • _: boolean

          Returns void

    • OptionalimmediateNotify: boolean

      Whether to immediately notify the callback of the current connection status.

    Returns (() => void)

    A function to remove the listener.

    -
      • (): void
      • Returns void

  • Creates a new NetworkTables instance if it does not exist.

    +
+
diff --git a/docs/classes/NetworkTablesTopic.html b/docs/classes/NetworkTablesTopic.html index 91cc4f8..7f6e8d3 100644 --- a/docs/classes/NetworkTablesTopic.html +++ b/docs/classes/NetworkTablesTopic.html @@ -1,4 +1,4 @@ -NetworkTablesTopic | ntcore-ts-client

Class NetworkTablesTopic<T>

Type Parameters

Constructors

constructor +NetworkTablesTopic | ntcore-ts-client

Class NetworkTablesTopic<T>

Type Parameters

Constructors

Accessors

Constructors

  • Creates a new topic. This should only be done after the base NTCore client has been initialized.

    -

    Type Parameters

    • T extends
          | string
          | number
          | boolean
          | string[]
          | ArrayBuffer
          | boolean[]
          | number[]

    Parameters

    • client: PubSubClient

      The client that owns the topic.

      +

      Type Parameters

      • T extends
            | string
            | number
            | boolean
            | string[]
            | ArrayBuffer
            | boolean[]
            | number[] = any

      Parameters

      • client: PubSubClient

        The client that owns the topic.

      • name: string

        The name of the topic.

      • typeInfo: NetworkTablesTypeInfo

        The type info for the topic.

      • OptionaldefaultValue: T

        The default value for the topic.

        -

      Returns NetworkTablesTopic<T>

Accessors

  • get announced(): boolean
  • Whether the topic has been announced.

    +

Returns NetworkTablesTopic<T>

Accessors

  • get subscribers(): Map<number, {
        callback: ((_: null | T) => void);
        immediateNotify: boolean;
        options: {
            all?: boolean;
            periodic?: number;
            prefix?: boolean;
            topicsonly?: boolean;
        };
    }>
  • Gets the subscribers to the topic.

    +
  • get subscribers(): Map<number, {
        callback: ((_: null | T) => void);
        immediateNotify: boolean;
        options: {
            all?: boolean;
            periodic?: number;
            prefix?: boolean;
            topicsonly?: boolean;
        };
    }>
  • Gets the subscribers to the topic.

    Returns Map<number, {
        callback: ((_: null | T) => void);
        immediateNotify: boolean;
        options: {
            all?: boolean;
            periodic?: number;
            prefix?: boolean;
            topicsonly?: boolean;
        };
    }>

    The subscribers to the topic.

    -

Methods

Methods

  • Marks the topic as announced. This should only be called by the PubSubClient.

    Parameters

    • id: number

      The ID of the topic.

    • Optionalpubuid: number

      The UID of the publisher.

      -

    Returns void

  • Publishes the topic.

    +

Returns void

Returns void

Returns void

Returns void

+

Returns void

diff --git a/docs/classes/NetworkTablesTypeInfos.html b/docs/classes/NetworkTablesTypeInfos.html index 6a03423..f26d727 100644 --- a/docs/classes/NetworkTablesTypeInfos.html +++ b/docs/classes/NetworkTablesTypeInfos.html @@ -1,4 +1,4 @@ -NetworkTablesTypeInfos | ntcore-ts-client

Class NetworkTablesTypeInfos

Constructors

constructor +NetworkTablesTypeInfos | ntcore-ts-client

Class NetworkTablesTypeInfos

Constructors

Properties

kArrayBuffer: NetworkTablesTypeInfo = ...
kBoolean: NetworkTablesTypeInfo = ...
kBooleanArray: NetworkTablesTypeInfo = ...
kDouble: NetworkTablesTypeInfo = ...
kDoubleArray: NetworkTablesTypeInfo = ...
kInteger: NetworkTablesTypeInfo = ...
kIntegerArray: NetworkTablesTypeInfo = ...
kString: NetworkTablesTypeInfo = ...
kStringArray: NetworkTablesTypeInfo = ...
+

Constructors

Properties

kArrayBuffer: NetworkTablesTypeInfo = ...
kBoolean: NetworkTablesTypeInfo = ...
kBooleanArray: NetworkTablesTypeInfo = ...
kDouble: NetworkTablesTypeInfo = ...
kDoubleArray: NetworkTablesTypeInfo = ...
kInteger: NetworkTablesTypeInfo = ...
kIntegerArray: NetworkTablesTypeInfo = ...
kString: NetworkTablesTypeInfo = ...
kStringArray: NetworkTablesTypeInfo = ...
diff --git a/docs/interfaces/BinaryMessageData.html b/docs/interfaces/BinaryMessageData.html index f4d1d69..873f154 100644 --- a/docs/interfaces/BinaryMessageData.html +++ b/docs/interfaces/BinaryMessageData.html @@ -1,5 +1,5 @@ -BinaryMessageData | ntcore-ts-client

Interface BinaryMessageData

interface BinaryMessageData {
    serverTime: number;
    topicId: number;
    typeInfo: NetworkTablesTypeInfos;
    value:
        | string
        | number
        | boolean
        | string[]
        | ArrayBuffer
        | boolean[]
        | number[];
}

Properties

serverTime +BinaryMessageData | ntcore-ts-client

Interface BinaryMessageData

interface BinaryMessageData {
    serverTime: number;
    topicId: number;
    typeInfo: NetworkTablesTypeInfos;
    value:
        | string
        | number
        | boolean
        | string[]
        | ArrayBuffer
        | boolean[]
        | number[];
}

Properties

serverTime: number
topicId: number
value:
    | string
    | number
    | boolean
    | string[]
    | ArrayBuffer
    | boolean[]
    | number[]
+

Properties

serverTime: number
topicId: number
value:
    | string
    | number
    | boolean
    | string[]
    | ArrayBuffer
    | boolean[]
    | number[]
diff --git a/docs/types/AnnounceMessage.html b/docs/types/AnnounceMessage.html index fac8e6e..13aa7cd 100644 --- a/docs/types/AnnounceMessage.html +++ b/docs/types/AnnounceMessage.html @@ -1 +1 @@ -AnnounceMessage | ntcore-ts-client

Type Alias AnnounceMessage

AnnounceMessage: z.infer<typeof announceMessageSchema>
+AnnounceMessage | ntcore-ts-client

Type Alias AnnounceMessage

AnnounceMessage: z.infer<typeof announceMessageSchema>
diff --git a/docs/types/AnnounceMessageParams.html b/docs/types/AnnounceMessageParams.html index 25ecc4f..8cea106 100644 --- a/docs/types/AnnounceMessageParams.html +++ b/docs/types/AnnounceMessageParams.html @@ -1 +1 @@ -AnnounceMessageParams | ntcore-ts-client

Type Alias AnnounceMessageParams

AnnounceMessageParams: AnnounceMessage["params"]
+AnnounceMessageParams | ntcore-ts-client

Type Alias AnnounceMessageParams

AnnounceMessageParams: AnnounceMessage["params"]
diff --git a/docs/types/BinaryMessage.html b/docs/types/BinaryMessage.html index cc8a2be..69ca540 100644 --- a/docs/types/BinaryMessage.html +++ b/docs/types/BinaryMessage.html @@ -1 +1 @@ -BinaryMessage | ntcore-ts-client

Type Alias BinaryMessage

BinaryMessage: z.infer<typeof msgPackSchema>
+BinaryMessage | ntcore-ts-client

Type Alias BinaryMessage

BinaryMessage: z.infer<typeof msgPackSchema>
diff --git a/docs/types/Message.html b/docs/types/Message.html index fa4bace..7b12efa 100644 --- a/docs/types/Message.html +++ b/docs/types/Message.html @@ -1 +1 @@ -Message | ntcore-ts-client

Type Alias Message

Message:
    | PublishMessage
    | UnpublishMessage
    | SetPropertiesMessage
    | SubscribeMessage
    | UnsubscribeMessage
    | AnnounceMessage
    | UnannounceMessage
    | PropertiesMessage
+Message | ntcore-ts-client

Type Alias Message

Message:
    | PublishMessage
    | UnpublishMessage
    | SetPropertiesMessage
    | SubscribeMessage
    | UnsubscribeMessage
    | AnnounceMessage
    | UnannounceMessage
    | PropertiesMessage
diff --git a/docs/types/NetworkTablesTypeInfo.html b/docs/types/NetworkTablesTypeInfo.html index dbd7ea5..c230e24 100644 --- a/docs/types/NetworkTablesTypeInfo.html +++ b/docs/types/NetworkTablesTypeInfo.html @@ -1 +1 @@ -NetworkTablesTypeInfo | ntcore-ts-client

Type Alias NetworkTablesTypeInfo

NetworkTablesTypeInfo: [TypeNum, TypeString]
+NetworkTablesTypeInfo | ntcore-ts-client

Type Alias NetworkTablesTypeInfo

NetworkTablesTypeInfo: [TypeNum, TypeString]
diff --git a/docs/types/NetworkTablesTypes.html b/docs/types/NetworkTablesTypes.html index 3aa6580..328f3c6 100644 --- a/docs/types/NetworkTablesTypes.html +++ b/docs/types/NetworkTablesTypes.html @@ -1 +1 @@ -NetworkTablesTypes | ntcore-ts-client

Type Alias NetworkTablesTypes

NetworkTablesTypes: z.infer<typeof msgPackValueSchema>
+NetworkTablesTypes | ntcore-ts-client

Type Alias NetworkTablesTypes

NetworkTablesTypes: z.infer<typeof msgPackValueSchema>
diff --git a/docs/types/PropertiesMessage.html b/docs/types/PropertiesMessage.html index f20654f..13b142b 100644 --- a/docs/types/PropertiesMessage.html +++ b/docs/types/PropertiesMessage.html @@ -1 +1 @@ -PropertiesMessage | ntcore-ts-client

Type Alias PropertiesMessage

PropertiesMessage: z.infer<typeof propertiesMessageSchema>
+PropertiesMessage | ntcore-ts-client

Type Alias PropertiesMessage

PropertiesMessage: z.infer<typeof propertiesMessageSchema>
diff --git a/docs/types/PropertiesMessageParams.html b/docs/types/PropertiesMessageParams.html index 84ebd8c..f2ae5c8 100644 --- a/docs/types/PropertiesMessageParams.html +++ b/docs/types/PropertiesMessageParams.html @@ -1 +1 @@ -PropertiesMessageParams | ntcore-ts-client

Type Alias PropertiesMessageParams

PropertiesMessageParams: PropertiesMessage["params"]
+PropertiesMessageParams | ntcore-ts-client

Type Alias PropertiesMessageParams

PropertiesMessageParams: PropertiesMessage["params"]
diff --git a/docs/types/PublishMessage.html b/docs/types/PublishMessage.html index 1ebcaf9..51f938b 100644 --- a/docs/types/PublishMessage.html +++ b/docs/types/PublishMessage.html @@ -1 +1 @@ -PublishMessage | ntcore-ts-client

Type Alias PublishMessage

PublishMessage: z.infer<typeof publishMessageSchema>
+PublishMessage | ntcore-ts-client

Type Alias PublishMessage

PublishMessage: z.infer<typeof publishMessageSchema>
diff --git a/docs/types/PublishMessageParams.html b/docs/types/PublishMessageParams.html index 76a9f59..7c09fc9 100644 --- a/docs/types/PublishMessageParams.html +++ b/docs/types/PublishMessageParams.html @@ -1 +1 @@ -PublishMessageParams | ntcore-ts-client

Type Alias PublishMessageParams

PublishMessageParams: PublishMessage["params"]
+PublishMessageParams | ntcore-ts-client

Type Alias PublishMessageParams

PublishMessageParams: PublishMessage["params"]
diff --git a/docs/types/SetPropertiesMessage.html b/docs/types/SetPropertiesMessage.html index 5664386..ead6385 100644 --- a/docs/types/SetPropertiesMessage.html +++ b/docs/types/SetPropertiesMessage.html @@ -1 +1 @@ -SetPropertiesMessage | ntcore-ts-client

Type Alias SetPropertiesMessage

SetPropertiesMessage: z.infer<typeof setPropertiesMessageSchema>
+SetPropertiesMessage | ntcore-ts-client

Type Alias SetPropertiesMessage

SetPropertiesMessage: z.infer<typeof setPropertiesMessageSchema>
diff --git a/docs/types/SetPropertiesMessageParams.html b/docs/types/SetPropertiesMessageParams.html index 421f537..94438aa 100644 --- a/docs/types/SetPropertiesMessageParams.html +++ b/docs/types/SetPropertiesMessageParams.html @@ -1 +1 @@ -SetPropertiesMessageParams | ntcore-ts-client

Type Alias SetPropertiesMessageParams

SetPropertiesMessageParams: SetPropertiesMessage["params"]
+SetPropertiesMessageParams | ntcore-ts-client

Type Alias SetPropertiesMessageParams

SetPropertiesMessageParams: SetPropertiesMessage["params"]
diff --git a/docs/types/SubscribeMessage.html b/docs/types/SubscribeMessage.html index dfb85d0..5b92c75 100644 --- a/docs/types/SubscribeMessage.html +++ b/docs/types/SubscribeMessage.html @@ -1 +1 @@ -SubscribeMessage | ntcore-ts-client

Type Alias SubscribeMessage

SubscribeMessage: z.infer<typeof subscribeMessageSchema>
+SubscribeMessage | ntcore-ts-client

Type Alias SubscribeMessage

SubscribeMessage: z.infer<typeof subscribeMessageSchema>
diff --git a/docs/types/SubscribeMessageParams.html b/docs/types/SubscribeMessageParams.html index fcddb3c..0e5f9c0 100644 --- a/docs/types/SubscribeMessageParams.html +++ b/docs/types/SubscribeMessageParams.html @@ -1 +1 @@ -SubscribeMessageParams | ntcore-ts-client

Type Alias SubscribeMessageParams

SubscribeMessageParams: SubscribeMessage["params"]
+SubscribeMessageParams | ntcore-ts-client

Type Alias SubscribeMessageParams

SubscribeMessageParams: SubscribeMessage["params"]
diff --git a/docs/types/SubscribeOptions.html b/docs/types/SubscribeOptions.html index f2064a1..4e5fc23 100644 --- a/docs/types/SubscribeOptions.html +++ b/docs/types/SubscribeOptions.html @@ -1 +1 @@ -SubscribeOptions | ntcore-ts-client

Type Alias SubscribeOptions

SubscribeOptions: z.infer<typeof subscriptionOptionsSchema>
+SubscribeOptions | ntcore-ts-client

Type Alias SubscribeOptions

SubscribeOptions: z.infer<typeof subscriptionOptionsSchema>
diff --git a/docs/types/TopicProperties.html b/docs/types/TopicProperties.html index 5308300..50b01c9 100644 --- a/docs/types/TopicProperties.html +++ b/docs/types/TopicProperties.html @@ -1 +1 @@ -TopicProperties | ntcore-ts-client

Type Alias TopicProperties

TopicProperties: z.infer<typeof topicPropertiesSchema>
+TopicProperties | ntcore-ts-client

Type Alias TopicProperties

TopicProperties: z.infer<typeof topicPropertiesSchema>
diff --git a/docs/types/TypeNum.html b/docs/types/TypeNum.html index c5dda61..5b7f747 100644 --- a/docs/types/TypeNum.html +++ b/docs/types/TypeNum.html @@ -1 +1 @@ -TypeNum | ntcore-ts-client

Type Alias TypeNum

TypeNum: z.infer<typeof typeNumSchema>
+TypeNum | ntcore-ts-client

Type Alias TypeNum

TypeNum: z.infer<typeof typeNumSchema>
diff --git a/docs/types/TypeString.html b/docs/types/TypeString.html index d99880e..4cfee24 100644 --- a/docs/types/TypeString.html +++ b/docs/types/TypeString.html @@ -1 +1 @@ -TypeString | ntcore-ts-client

Type Alias TypeString

TypeString: z.infer<typeof typeStringSchema>
+TypeString | ntcore-ts-client

Type Alias TypeString

TypeString: z.infer<typeof typeStringSchema>
diff --git a/docs/types/UnannounceMessage.html b/docs/types/UnannounceMessage.html index 764ca14..f5b9d66 100644 --- a/docs/types/UnannounceMessage.html +++ b/docs/types/UnannounceMessage.html @@ -1 +1 @@ -UnannounceMessage | ntcore-ts-client

Type Alias UnannounceMessage

UnannounceMessage: z.infer<typeof unannounceMessageSchema>
+UnannounceMessage | ntcore-ts-client

Type Alias UnannounceMessage

UnannounceMessage: z.infer<typeof unannounceMessageSchema>
diff --git a/docs/types/UnannounceMessageParams.html b/docs/types/UnannounceMessageParams.html index bfe82ee..bacc31f 100644 --- a/docs/types/UnannounceMessageParams.html +++ b/docs/types/UnannounceMessageParams.html @@ -1 +1 @@ -UnannounceMessageParams | ntcore-ts-client

Type Alias UnannounceMessageParams

UnannounceMessageParams: UnannounceMessage["params"]
+UnannounceMessageParams | ntcore-ts-client

Type Alias UnannounceMessageParams

UnannounceMessageParams: UnannounceMessage["params"]
diff --git a/docs/types/UnpublishMessage.html b/docs/types/UnpublishMessage.html index 9795cb1..8df751c 100644 --- a/docs/types/UnpublishMessage.html +++ b/docs/types/UnpublishMessage.html @@ -1 +1 @@ -UnpublishMessage | ntcore-ts-client

Type Alias UnpublishMessage

UnpublishMessage: z.infer<typeof unpublishMessageSchema>
+UnpublishMessage | ntcore-ts-client

Type Alias UnpublishMessage

UnpublishMessage: z.infer<typeof unpublishMessageSchema>
diff --git a/docs/types/UnpublishMessageParams.html b/docs/types/UnpublishMessageParams.html index dbed616..1fbe779 100644 --- a/docs/types/UnpublishMessageParams.html +++ b/docs/types/UnpublishMessageParams.html @@ -1 +1 @@ -UnpublishMessageParams | ntcore-ts-client

Type Alias UnpublishMessageParams

UnpublishMessageParams: UnpublishMessage["params"]
+UnpublishMessageParams | ntcore-ts-client

Type Alias UnpublishMessageParams

UnpublishMessageParams: UnpublishMessage["params"]
diff --git a/docs/types/UnsubscribeMessage.html b/docs/types/UnsubscribeMessage.html index 290ac8f..baabd80 100644 --- a/docs/types/UnsubscribeMessage.html +++ b/docs/types/UnsubscribeMessage.html @@ -1 +1 @@ -UnsubscribeMessage | ntcore-ts-client

Type Alias UnsubscribeMessage

UnsubscribeMessage: z.infer<typeof unsubscribeMessageSchema>
+UnsubscribeMessage | ntcore-ts-client

Type Alias UnsubscribeMessage

UnsubscribeMessage: z.infer<typeof unsubscribeMessageSchema>
diff --git a/docs/types/UnsubscribeMessageParams.html b/docs/types/UnsubscribeMessageParams.html index 8176d87..de4f2a7 100644 --- a/docs/types/UnsubscribeMessageParams.html +++ b/docs/types/UnsubscribeMessageParams.html @@ -1 +1 @@ -UnsubscribeMessageParams | ntcore-ts-client

Type Alias UnsubscribeMessageParams

UnsubscribeMessageParams: UnsubscribeMessage["params"]
+UnsubscribeMessageParams | ntcore-ts-client

Type Alias UnsubscribeMessageParams

UnsubscribeMessageParams: UnsubscribeMessage["params"]
diff --git a/packages/ntcore-ts-client/src/lib/ntcore-ts-client.ts b/packages/ntcore-ts-client/src/lib/ntcore-ts-client.ts index e16d2cb..dc3df04 100644 --- a/packages/ntcore-ts-client/src/lib/ntcore-ts-client.ts +++ b/packages/ntcore-ts-client/src/lib/ntcore-ts-client.ts @@ -133,6 +133,16 @@ export class NetworkTables { return this._client.messenger.socket.addConnectionListener(callback, immediateNotify); } + /** + * Adds a listener for when topics are announced + * @param callback - A callback triggered with the announced topic + * @param announceAll - Whether to trigger all topics in NetworkTables to be announced + * @returns A function to remove the listener. + */ + addOnTopicAnnouncedListener(callback: (_: NetworkTablesTopic) => void, announceAll?: boolean) { + return this._client.addOnTopicAnnouncedListener(callback, announceAll); + } + /** * Creates a new topic. * @param name - The name of the topic. diff --git a/packages/ntcore-ts-client/src/lib/pubsub/pubsub.spec.ts b/packages/ntcore-ts-client/src/lib/pubsub/pubsub.spec.ts index f4a9eb8..c745dc4 100644 --- a/packages/ntcore-ts-client/src/lib/pubsub/pubsub.spec.ts +++ b/packages/ntcore-ts-client/src/lib/pubsub/pubsub.spec.ts @@ -58,6 +58,22 @@ describe('PubSubClient', () => { expect(topic.unannounce).toHaveBeenCalled(); }); + it('handles sending announced topics to onTopicAnnounced listeners with existing topic', () => { + const topic = { name: 'test', announce: jest.fn() }; + client.registerTopic(topic as never); + let topicNameAnnounced = ''; + client.addOnTopicAnnouncedListener((announcedTopic) => (topicNameAnnounced = announcedTopic.name)); + client['onTopicAnnounce']({ id: 123, name: 'test', type: 'string' } as never); + expect(topicNameAnnounced).toBe('test'); + }); + + it('handles sending announced topics to onTopicAnnounced listeners without existing topic', () => { + let topicNameAnnounced = ''; + client.addOnTopicAnnouncedListener((announcedTopic) => (topicNameAnnounced = announcedTopic.name)); + client['onTopicAnnounce']({ id: 123, name: 'test', type: 'string' } as never); + expect(topicNameAnnounced).toBe('test'); + }); + it('reinstantates topics', () => { const topic = { name: 'test', diff --git a/packages/ntcore-ts-client/src/lib/pubsub/pubsub.ts b/packages/ntcore-ts-client/src/lib/pubsub/pubsub.ts index 724c42e..c3f6c47 100644 --- a/packages/ntcore-ts-client/src/lib/pubsub/pubsub.ts +++ b/packages/ntcore-ts-client/src/lib/pubsub/pubsub.ts @@ -1,19 +1,21 @@ import { Messenger } from '../socket/messenger'; +import { Util } from '../util/util'; +import { NetworkTablesTopic } from './topic'; -import type { NetworkTablesTopic } from './topic'; import type { AnnounceMessageParams, BinaryMessageData, NetworkTablesTypes, PropertiesMessageParams, + SubscribeMessageParams, UnannounceMessageParams, } from '../types/types'; /** The client for the PubSub protocol. */ export class PubSubClient { private readonly _messenger: Messenger; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - private topics: Map>; + private topics: Map; + private onTopicAnnouncedListeners: ((_: NetworkTablesTopic) => void)[]; private static _instances = new Map(); get messenger() { @@ -29,6 +31,7 @@ export class PubSubClient { this.onTopicProperties ); this.topics = new Map(); + this.onTopicAnnouncedListeners = []; // In the DOM, auto-cleanup if (typeof window !== 'undefined') { @@ -53,6 +56,40 @@ export class PubSubClient { return instance; } + /** + * Adds a listener for when topics are announced. + * @param callback - A callback triggered with the announced topic + * @param announceAll - Whether to trigger all topics in NetworkTables to be announced + * @returns A function to remove the listener. + */ + addOnTopicAnnouncedListener(callback: (_: NetworkTablesTopic) => void, announceAll?: boolean) { + this.onTopicAnnouncedListeners.push(callback); + if (announceAll) { + const subscribeParams: SubscribeMessageParams = { + topics: [''], + subuid: Util.generateUid(), + options: { + prefix: true, + topicsonly: true, + }, + }; + this.messenger.subscribe(subscribeParams); + } + return () => { + this.onTopicAnnouncedListeners = this.onTopicAnnouncedListeners.filter( + (storedCallback) => storedCallback !== callback + ); + }; + } + + /** + * Notifies all onTopicAnnounced subscribers of the announced topic. + * @param topic - The topic that was announced + */ + private notifyOnTopicAnnouncedSubscribers(topic: NetworkTablesTopic) { + this.onTopicAnnouncedListeners.forEach((callback) => callback(topic)); + } + /** * Reinstantiates the client by resubscribing to all previously subscribed topics * and republishing for all previously published topics. @@ -95,12 +132,17 @@ export class PubSubClient { * @param params - The announce message parameters. */ private onTopicAnnounce = (params: AnnounceMessageParams) => { - const topic = this.topics.get(params.name); - if (!topic) { + let topic = this.topics.get(params.name); + + if (!topic && this.onTopicAnnouncedListeners.length > 0) { + topic = new NetworkTablesTopic(this, params.name, Util.getNetworkTablesTypeFromTypeString(params.type)); + } else if (!topic) { console.warn(`Topic ${params.name} was announced, but does not exist`); return; } + topic.announce(params.id, params.pubuid); + this.notifyOnTopicAnnouncedSubscribers(topic); }; /** diff --git a/packages/ntcore-ts-client/src/lib/pubsub/topic.ts b/packages/ntcore-ts-client/src/lib/pubsub/topic.ts index 69ddb7a..dec894f 100644 --- a/packages/ntcore-ts-client/src/lib/pubsub/topic.ts +++ b/packages/ntcore-ts-client/src/lib/pubsub/topic.ts @@ -13,7 +13,7 @@ import type { TopicProperties, } from '../types/types'; -export class NetworkTablesTopic { +export class NetworkTablesTopic { private client: PubSubClient; private _id?: number; private readonly _name: string; @@ -184,7 +184,7 @@ export class NetworkTablesTopic { /** */ /** - * Creates a new subscriber. This should only be called by the PubSubClient. + * Creates a new subscriber. * @param callback - The callback to call when the topic value changes. * @param immediateNotify - Whether to immediately notify the subscriber of the current value. * @param options - The options for the subscriber.