11"use strict" ;
22
3- var NetworkTables = new function ( ) {
4-
3+ const NetworkTables = new function ( ) {
4+
5+
6+ let robotAddress ;
7+ let robotConnected ;
8+ let socketOpen ;
9+ let socket ;
10+
511 if ( ! ( "WebSocket" in window ) ) {
612 alert ( "Your browser does not support websockets, this will fail!" ) ;
713 return ;
814 }
9-
10- //
11- // javascript map implementation
12- // map functions copied from d3 (BSD license: Mike Bostock)
13- //
14-
15- var d3_map_proto = "__proto__" , d3_map_zero = "\x00" ;
16-
17- // we use encodeURIComponent/decodeURIComponent to allow weird values
18- // into the maps we create
19-
20- function d3_map_escape ( key ) {
21- return ( key += "" ) === d3_map_proto || key [ 0 ] === d3_map_zero ? d3_map_zero + encodeURIComponent ( key ) : encodeURIComponent ( key ) ;
22- }
23-
24- function d3_map_unescape ( key ) {
25- return ( key += "" ) [ 0 ] === d3_map_zero ? decodeURIComponent ( key . slice ( 1 ) ) : decodeURIComponent ( key ) ;
26- }
27-
28- var d3_map = function ( ) {
29-
30- this . _ = Object . create ( null ) ;
31-
32- this . forEach = function ( f ) {
33- for ( var key in this . _ ) f . call ( this , d3_map_unescape ( key ) , this . _ [ key ] ) ;
34- } ;
35-
36- this . get = function ( key ) {
37- return this . _ [ d3_map_escape ( key ) ] ;
38- } ;
39-
40- this . getKeys = function ( ) {
41- var keys = [ ] ;
42- for ( var key in this . _ ) keys . push ( d3_map_unescape ( key ) ) ;
43- return keys ;
44- } ;
45-
46- this . has = function ( key ) {
47- return d3_map_escape ( key ) in this . _ ;
48- } ;
49-
50- this . set = function ( key , value ) {
51- return this . _ [ d3_map_escape ( key ) ] = value ;
52- } ;
53- } ;
54-
15+
5516 //
5617 // Utility functions
5718 //
@@ -61,7 +22,7 @@ var NetworkTables = new function () {
6122 is safe to store NetworkTables keys in.
6223 */
6324 this . create_map = function ( ) {
64- return new d3_map ( ) ;
25+ return new Map ( ) ;
6526 } ;
6627
6728 /**
@@ -80,7 +41,7 @@ var NetworkTables = new function () {
8041 :returns: Escaped value
8142 */
8243 this . keySelector = function ( str ) {
83- return encodeURIComponent ( str ) . replace ( / ( [ ; & , \. \+ \* \ ~' : " \! \ ^# $ % @ \[ \] \( \ )= > \ |] ) / g, '\\$1' ) ;
44+ return encodeURIComponent ( str ) . replace ( / ( [ ; & , . + * ~ ' : " ! ^ # $ % @ \[ \] ( ) = > | ] ) / g, '\\$1' ) ;
8445 } ;
8546
8647 //
@@ -89,17 +50,17 @@ var NetworkTables = new function () {
8950
9051
9152 // functions that listen for connection changes
92- var connectionListeners = [ ] ;
93- var robotConnectionListeners = [ ] ;
53+ const connectionListeners = new Set ( ) ;
54+ const robotConnectionListeners = new Set ( ) ;
9455
9556 // functions that listen for everything
96- var globalListeners = [ ] ;
57+ const globalListeners = new Set ( ) ;
9758
9859 // functions that listen for specific keys
99- var keyListeners = new d3_map ( ) ;
60+ const keyListeners = new Map ( ) ;
10061
10162 // contents of everything in NetworkTables that we know about
102- var ntCache = new d3_map ( ) ;
63+ let ntCache = new Map ( ) ;
10364
10465 //
10566 // NetworkTables JS API
@@ -115,18 +76,13 @@ var NetworkTables = new function () {
11576 :returns: a function that will unsubscribe
11677 */
11778 this . addWsConnectionListener = function ( f , immediateNotify ) {
118- connectionListeners . push ( f ) ;
79+ connectionListeners . add ( f ) ;
11980
120- if ( immediateNotify == true ) {
81+ if ( immediateNotify === true ) {
12182 f ( socketOpen ) ;
12283 }
12384
124- return function ( ) {
125- const index = connectionListeners . indexOf ( f ) ;
126- if ( index !== - 1 ) {
127- connectionListeners . splice ( index , 1 ) ;
128- }
129- }
85+ return ( ) => connectionListeners . delete ( f ) ;
13086 } ;
13187
13288 /**
@@ -141,19 +97,14 @@ var NetworkTables = new function () {
14197 :returns: a function that will unsubscribe
14298 */
14399 this . addRobotConnectionListener = function ( f , immediateNotify ) {
144- robotConnectionListeners . push ( f ) ;
100+ robotConnectionListeners . add ( f ) ;
145101
146- if ( immediateNotify == true ) {
102+ if ( immediateNotify === true ) {
147103 f ( robotConnected ) ;
148104 }
149105
150- return function ( ) {
151- const index = robotConnectionListeners . indexOf ( f ) ;
152- if ( index !== - 1 ) {
153- robotConnectionListeners . splice ( index , 1 ) ;
154- }
155- }
156- }
106+ return ( ) => robotConnectionListeners . delete ( f ) ;
107+ } ;
157108
158109 /**
159110 Set a function that will be called whenever any NetworkTables value is changed
@@ -165,20 +116,15 @@ var NetworkTables = new function () {
165116 :returns: a function that will unsubscribe
166117 */
167118 this . addGlobalListener = function ( f , immediateNotify ) {
168- globalListeners . push ( f ) ;
119+ globalListeners . add ( f ) ;
169120
170- if ( immediateNotify == true ) {
121+ if ( immediateNotify === true ) {
171122 ntCache . forEach ( function ( k , v ) {
172123 f ( k , v , true ) ;
173124 } ) ;
174125 }
175126
176- return function ( ) {
177- const index = globalListeners . indexOf ( f ) ;
178- if ( index !== - 1 ) {
179- globalListeners . splice ( index , 1 ) ;
180- }
181- }
127+ return ( ) => globalListeners . delete ( f ) ;
182128 } ;
183129
184130 /**
@@ -192,26 +138,21 @@ var NetworkTables = new function () {
192138 :returns: a function that will unsubscribe
193139 */
194140 this . addKeyListener = function ( key , f , immediateNotify ) {
195- var listeners = keyListeners . get ( key ) ;
141+ const listeners = keyListeners . get ( key ) ;
196142 if ( listeners === undefined ) {
197- keyListeners . set ( key , [ f ] ) ;
143+ keyListeners . set ( key , new Set ( [ f ] ) ) ;
198144 } else {
199- listeners . push ( f ) ;
145+ listeners . add ( f ) ;
200146 }
201147
202- if ( immediateNotify == true ) {
203- var v = ntCache . get ( key ) ;
148+ if ( immediateNotify === true ) {
149+ const v = ntCache . get ( key ) ;
204150 if ( v !== undefined ) {
205151 f ( key , v , true ) ;
206152 }
207153 }
208154
209- return function ( ) {
210- const index = keyListeners . get ( key ) . indexOf ( f ) ;
211- if ( index !== - 1 ) {
212- keyListeners . get ( key ) . splice ( index , 1 ) ;
213- }
214- }
155+ return ( ) => keyListeners . get ( key ) . delete ( f ) ;
215156 } ;
216157
217158 /**
@@ -231,7 +172,7 @@ var NetworkTables = new function () {
231172 connected
232173 */
233174 this . getKeys = function ( ) {
234- return ntCache . getKeys ( ) ;
175+ return ntCache . keys ( ) ;
235176 } ;
236177
237178 /**
@@ -251,7 +192,7 @@ var NetworkTables = new function () {
251192 for changes to values, instead of using this function.
252193 */
253194 this . getValue = function ( key , defaultValue ) {
254- var val = ntCache . get ( key ) ;
195+ const val = ntCache . get ( key ) ;
255196 if ( val === undefined )
256197 return defaultValue ;
257198 else
@@ -313,108 +254,77 @@ var NetworkTables = new function () {
313254
314255 // backwards compatibility; deprecated
315256 this . setValue = this . putValue ;
316-
317- //
318- // NetworkTables socket code
319- //
320-
321- var socket ;
322- var socketOpen = false ;
323- var robotConnected = false ;
324- var robotAddress = null ;
325-
257+ socketOpen = false ;
258+ robotConnected = false ;
259+ robotAddress = null ;
260+
326261 // construct the websocket URI
327- var loc = window . location ;
328- var host ;
329-
330- if ( loc . protocol === "https:" ) {
331- host = "wss:" ;
332- } else {
333- host = "ws:" ;
334- }
335-
262+ const loc = window . location ;
263+ const protocol = loc . protocol === "https:" ? "wss:" : "ws:" ;
336264 // If the websocket is being served from a different host allow users
337265 // to add a data-nt-host="" attribute to the script tag loading
338266 // Networktables.
339- var ntHostElement = document . querySelector ( '[data-nt-host]' ) ;
340- if ( ntHostElement ) {
341- var ntHost = ntHostElement . getAttribute ( 'data-nt-host' ) ;
342- host += "//" + ntHost ;
343- } else {
344- host += "//" + loc . host ;
345- }
346-
347- host += "/networktables/ws" ;
267+ const ntHostElement = document . querySelector ( '[data-nt-host]' ) ;
268+ const host = ntHostElement ? ntHostElement . getAttribute ( 'data-nt-host' ) : loc . host ;
269+ const address = `${ protocol } //${ host } /networktables/ws` ;
348270
349271 function createSocket ( ) {
350272
351- socket = new WebSocket ( host ) ;
273+ socket = new WebSocket ( address ) ;
352274 if ( socket ) {
353275
354276 socket . onopen = function ( ) {
355- console . log ( "Socket opened" ) ;
277+ console . info ( "Socket opened" ) ;
356278
357279 socketOpen = true ;
358-
359- for ( var i in connectionListeners ) {
360- connectionListeners [ i ] ( true ) ;
361- }
280+
281+ connectionListeners . forEach ( f => f ( true ) ) ;
362282 } ;
363283
364284 socket . onmessage = function ( msg ) {
365- var data = JSON . parse ( msg . data ) ;
366-
285+ const data = JSON . parse ( msg . data ) ;
286+
367287 // robot connection event
368288 if ( data . r !== undefined ) {
369289 robotConnected = data . r ;
370290 robotAddress = data . a ;
371- for ( var i in robotConnectionListeners ) {
372- robotConnectionListeners [ i ] ( robotConnected ) ;
373- }
291+ robotConnectionListeners . forEach ( f => f ( robotConnected ) )
374292 } else {
375293
376294 // data changed on websocket
377- var key = data [ 'k' ] ;
378- var value = data [ 'v' ] ;
379- var isNew = data [ 'n' ] ;
380-
295+ const key = data [ 'k' ] ;
296+ const value = data [ 'v' ] ;
297+ const isNew = data [ 'n' ] ;
298+
381299 ntCache . set ( key , value ) ;
382300
383301 // notify global listeners
384- for ( var i in globalListeners ) {
385- globalListeners [ i ] ( key , value , isNew ) ;
386- }
302+ globalListeners . forEach ( f => f ( key , value , isNew ) ) ;
387303
388304 // notify key-specific listeners
389- var listeners = keyListeners . get ( key ) ;
305+ const listeners = keyListeners . get ( key ) ;
390306 if ( listeners !== undefined ) {
391- for ( var i in listeners ) {
392- listeners [ i ] ( key , value , isNew ) ;
393- }
307+ listeners . forEach ( f => f ( key , value , isNew ) ) ;
394308 }
395309 }
396310 } ;
397311
398312 socket . onclose = function ( ) {
399313
400314 if ( socketOpen ) {
401-
402- for ( var i in connectionListeners ) {
403- connectionListeners [ i ] ( false ) ;
404- }
405-
406- for ( var i in robotConnectionListeners ) {
407- robotConnectionListeners [ i ] ( false ) ;
408- }
315+
316+ connectionListeners . forEach ( f => f ( false ) ) ;
317+
318+ robotConnectionListeners . forEach ( f => f ( false ) ) ;
409319
410320 // clear ntCache, it's no longer valid
411321 // TODO: Is this true?
412- ntCache = new d3_map ( ) ;
322+ ntCache = new Map ( ) ;
413323
414324 socketOpen = false ;
415325 robotConnected = false ;
416326 robotAddress = null ;
417- console . log ( "Socket closed" ) ;
327+ console . info ( "Socket closed" ) ;
418328 }
419329
420330 // respawn the websocket
@@ -424,6 +334,6 @@ var NetworkTables = new function () {
424334 }
425335
426336 createSocket ( ) ;
427- }
337+ } ;
428338
429339
0 commit comments