From 36f87501cd174104e6b75a18b7c16d83ec74edaa Mon Sep 17 00:00:00 2001 From: Mike McNeil Date: Fri, 6 Apr 2018 00:04:04 -0500 Subject: [PATCH] Rename to Cloud.listen() and Cloud.ignore(), add aliases, and improve error msgs. --- dist/cloud.js | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/dist/cloud.js b/dist/cloud.js index 9459d20..02ccfd4 100644 --- a/dist/cloud.js +++ b/dist/cloud.js @@ -398,6 +398,12 @@ // ``` // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + if (options.methods.listen) { + throw new Error('Cannot .setup() Cloud SDK: `.listen()` is reserved. It cannot be used as the name for a method.'); + } + if (options.methods.ignore) { + throw new Error('Cannot .setup() Cloud SDK: `.ignore()` is reserved. It cannot be used as the name for a method.'); + } if (options.methods.on) { throw new Error('Cannot .setup() Cloud SDK: `.on()` is reserved. It cannot be used as the name for a method.'); } @@ -1756,19 +1762,19 @@ // Now attach the configured endpoint methods _.extend(Cloud, methods); - // And finally, `.on()` and `.off()`: + // And finally, `.listen()` and `.ignore()` (+`on` and `off` aliases) // (keeping track of bindings privately) var _boundSocketEventNames = []; /** - * Cloud.on() + * Cloud.listen() * * Listen for a particular kind of socket events, and trigger a function * any time one of them is received. * * > This is almost identical to `io.socket.on()`, except that: * > • (A) it prevents listening to the same event name more than once. - * > (This restriction only exists for `Cloud.on()` calls though-- + * > (This restriction only exists for `Cloud.listen()` calls though-- * > it's not aware of other listeners you might have bound in other * > ways.) * > • (B) it also supports passing in a dictionary in lieu of a function @@ -1785,14 +1791,14 @@ * @param {String} socketEventName * @param {Function|Dictionary} handleSocketMsg */ - Cloud.on = function(socketEventName, handleSocketMsg) { - if (!socketEventName || !_.isString(socketEventName)) { throw new Error('Invalid usage for `Cloud.on()`: Must pass in a valid first argument (a string; the name of the socket event to listen for -- i.e. the variety of incoming WebSocket messages to receive and handle).'); } - if (!handleSocketMsg) { throw new Error('Invalid usage for `Cloud.on()`: Must pass in a second argument (the function to run every time this WebSocket event is received).'); } + Cloud.listen = function(socketEventName, handleSocketMsg) { + if (!socketEventName || !_.isString(socketEventName)) { throw new Error('Invalid usage for `Cloud.listen()`: Must pass in a valid first argument (a string; the name of the socket event to listen for -- i.e. the variety of incoming WebSocket messages to receive and handle).'); } + if (!handleSocketMsg) { throw new Error('Invalid usage for `Cloud.listen()`: Must pass in a second argument (the function to run every time this WebSocket event is received).'); } - if (!io || !io.socket) { throw new Error('Could not bind a cloud event listener with `Cloud.on()`: WebSocket support is not currently available (`io.socket` is not available). Make sure `sails.io.js` is being injected in a