Skip to content

Commit 0de8a6b

Browse files
authored
Merge pull request #37 from akainth015/unsubscription
Unsubscription functions
2 parents e9e0c9b + 854600e commit 0de8a6b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pynetworktables2js/js/networktables.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,21 @@ var NetworkTables = new function () {
112112
that indicates whether the websocket is connected
113113
:param immediateNotify: If true, the function will be immediately called
114114
with the current status of the websocket
115+
:returns: a function that will unsubscribe
115116
*/
116117
this.addWsConnectionListener = function(f, immediateNotify) {
117118
connectionListeners.push(f);
118119

119120
if (immediateNotify == true) {
120121
f(socketOpen);
121122
}
123+
124+
return function() {
125+
const index = connectionListeners.indexOf(f);
126+
if (index !== -1) {
127+
connectionListeners.splice(index, 1);
128+
}
129+
}
122130
};
123131

124132
/**
@@ -130,13 +138,21 @@ var NetworkTables = new function () {
130138
that indicates whether the robot is connected
131139
:param immediateNotify: If true, the function will be immediately called
132140
with the current robot connection state
141+
:returns: a function that will unsubscribe
133142
*/
134143
this.addRobotConnectionListener = function(f, immediateNotify) {
135144
robotConnectionListeners.push(f);
136145

137146
if (immediateNotify == true) {
138147
f(robotConnected);
139148
}
149+
150+
return function() {
151+
const index = robotConnectionListeners.indexOf(f);
152+
if (index !== -1) {
153+
robotConnectionListeners.splice(index, 1);
154+
}
155+
}
140156
}
141157

142158
/**
@@ -146,6 +162,7 @@ var NetworkTables = new function () {
146162
for entry, value: value of entry, isNew: If true, the entry has just been created
147163
:param immediateNotify: If true, the function will be immediately called
148164
with the current value of all keys
165+
:returns: a function that will unsubscribe
149166
*/
150167
this.addGlobalListener = function(f, immediateNotify) {
151168
globalListeners.push(f);
@@ -155,6 +172,13 @@ var NetworkTables = new function () {
155172
f(k, v, true);
156173
});
157174
}
175+
176+
return function() {
177+
const index = globalListeners.indexOf(f);
178+
if (index !== -1) {
179+
globalListeners.splice(index, 1);
180+
}
181+
}
158182
};
159183

160184
/**
@@ -165,6 +189,7 @@ var NetworkTables = new function () {
165189
for entry, value: value of entry, isNew: If true, the entry has just been created
166190
:param immediateNotify: If true, the function will be immediately called
167191
with the current value of the specified key
192+
:returns: a function that will unsubscribe
168193
*/
169194
this.addKeyListener = function(key, f, immediateNotify) {
170195
var listeners = keyListeners.get(key);
@@ -180,6 +205,13 @@ var NetworkTables = new function () {
180205
f(key, v, true);
181206
}
182207
}
208+
209+
return function() {
210+
const index = keyListeners.get(key).indexOf(f);
211+
if (index !== -1) {
212+
keyListeners.get(key).splice(index, 1);
213+
}
214+
}
183215
};
184216

185217
/**

0 commit comments

Comments
 (0)