✨ Add removeMessage to OctoPrint socket (#4708)

This commit is contained in:
Charlie Powell authored and GitHub committed 2023-01-16 17:15:20 +01:00
1 parent ed03f77b04
commit 7f3bd32d88
2 files changed
+29

No files matched your search

+17
View File
@@ -60,6 +60,23 @@
:param string message: The type of message for which to register
:param function handler: The handler function
.. js:function:: OctoPrintClient.socker.removeMessage(message, handler)
Removes the ``handler`` for messages of type ``message``.
.. code-block:: javascript
const handler = (message) => {
// do something with the message object
};
OctoPrint.socket.onMessage("*", handler);
// Use the same reference to the handler function to remove it again
OctoPrint.socket.removeMessage("*", handler);
:param string message: The type of message for which to remove the handler
:param function handler: The handler function
.. js:function:: OctoPrintClient.socket.sendMessage(type, payload)
Sends a message of type ``type`` with the provided ``payload`` to the server.
@@ -220,6 +220,18 @@
return this;
};
OctoPrintSocketClient.prototype.removeMessage = function (message, handler) {
if (!this.registeredHandlers.hasOwnProperty(message)) {
// No handlers registered, do nothing
return;
}
const index = OctoPrint.socket.registeredHandlers[message].indexOf(handler);
if (index > -1) {
OctoPrint.socket.registeredHandlers[message].splice(index, 1);
}
};
OctoPrintSocketClient.prototype.onReconnectAttempt = function (trial) {};
OctoPrintSocketClient.prototype.onReconnectFailed = function () {};
OctoPrintSocketClient.prototype.onConnected = function () {};