Skip to content

Commit d7bce57

Browse files
committed
Listeners return a function that will unsubscribe when called
1 parent e9e0c9b commit d7bce57

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

pynetworktables2js/js/networktables.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ var NetworkTables = new function () {
119119
if (immediateNotify == true) {
120120
f(socketOpen);
121121
}
122+
123+
return function() {
124+
const index = connectionListeners.indexOf(f);
125+
if (index !== -1) {
126+
connectionListeners.splice(index, 1);
127+
}
128+
}
122129
};
123130

124131
/**
@@ -137,6 +144,13 @@ var NetworkTables = new function () {
137144
if (immediateNotify == true) {
138145
f(robotConnected);
139146
}
147+
148+
return function() {
149+
const index = robotConnectionListeners.indexOf(f);
150+
if (index !== -1) {
151+
robotConnectionListeners.splice(index, 1);
152+
}
153+
}
140154
}
141155

142156
/**
@@ -155,6 +169,13 @@ var NetworkTables = new function () {
155169
f(k, v, true);
156170
});
157171
}
172+
173+
return function() {
174+
const index = globalListeners.indexOf(f);
175+
if (index !== -1) {
176+
globalListeners.splice(index, 1);
177+
}
178+
}
158179
};
159180

160181
/**
@@ -180,6 +201,13 @@ var NetworkTables = new function () {
180201
f(key, v, true);
181202
}
182203
}
204+
205+
return function() {
206+
const index = keyListeners.get(key).indexOf(f);
207+
if (index !== -1) {
208+
keyListeners.get(key).splice(index, 1);
209+
}
210+
}
183211
};
184212

185213
/**

0 commit comments

Comments
 (0)