From 639dbf2361665808a950107f76c1ca3bd33159cb Mon Sep 17 00:00:00 2001 From: jmoenig Date: Mon, 16 Dec 2019 10:28:49 +0100 Subject: [PATCH] refactored IDE >> addMessageListenerForAll(callback) --- HISTORY.md | 1 + src/gui.js | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 8703a610..44679950 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -19,6 +19,7 @@ ### 2019-12-16 * gui, objects: added ability to add general message listeners for "any" message * gui: added IDE >> getMessages() to Snap! API +* gui: refactored IDE >> addMessageListenerForAll(callback) ### 2019-12-15 * gui, threads: new Snap! API: programmatically broadcast messages and optionally wait from outside Snap! diff --git a/src/gui.js b/src/gui.js index 7620b3c7..f716f654 100644 --- a/src/gui.js +++ b/src/gui.js @@ -6110,19 +6110,22 @@ IDE_Morph.prototype.broadcast = function(message, callback) { }); }; +IDE_Morph.prototype.addMessageListenerForAll = function (callback) { + // associate a monadic callback with all broadcasts. + // whenever a message is broadcast the callback is called + // with the current message as argument + this.addMessageListener('', callback); +}; + IDE_Morph.prototype.addMessageListener = function (message, callback) { // associate a callback function with a broadcast message, // whenever the message is broadcast, the callback is executed, // you can add multiple callbacks to a message, they will be // executed in the order you added them. - // Note: Only passing a callback or associating a callback with - // an empty string attaches the callback to "any" message, taking - // the actual message as argument + // Note: ssociating a callback with an empty string attaches the + // callback to "any" message, taking the actual message as argument var funcs; - if (message instanceof Function && isNil(callback)) { - callback = message; - message = ''; - } else if (!isString(message)) { + if (!isString(message)) { throw new Error('message must be a String'); } funcs = this.stage.messageCallbacks[message];