diff --git a/lib/handlers/bin.js b/lib/handlers/bin.js index dd8c693d..d19a16da 100644 --- a/lib/handlers/bin.js +++ b/lib/handlers/bin.js @@ -453,7 +453,7 @@ module.exports = Observable.extend({ if (renderFn) { renderFn(); } else { - _this.emit('created', req.bin); + _this.emit('created', bin); _this.renderCreated(req, res, bin); } } diff --git a/lib/spike/index.js b/lib/spike/index.js index d787ac99..df932b0d 100644 --- a/lib/spike/index.js +++ b/lib/spike/index.js @@ -60,8 +60,8 @@ utils.makeEvent = function (eventName, data) { if (!eventName) return ''; var str = ''; if (typeof data !== 'string') data = JSON.stringify(data); - if (data) str += 'data: ' + (''+data).split('\n').join('\ndata: ') + '\n'; - return str + 'event: ' + eventName + '\n\n'; + if (data) str += 'data: ' + (''+data).split('\n').join('\ndata: '); + return 'event: ' + eventName + '\n' + str + '\n\n'; }; /** @@ -186,10 +186,9 @@ module.exports = { /** * Update spikes for URLs like /rem/last to keep the clients up to date with - * the revision. It disassociates the connection (userSession.res) from the - * old bin's pool (with the oldBinKey) via removeConnection. It then gets or - * creates a session for the bin, the userSession's connection to the new bin's - * pool, and updates the userSession's key. + * the correct bin. It disassociates the connection (userSession.res) from the + * old bin's pool via removeConnection. It then gets or creates a session for + * the new bin and updates the userSession's key. */ updateUserSpikes: function (user, newBin) { var newKey = utils.keyForBin(newBin); @@ -199,9 +198,8 @@ module.exports = { // Update sessions for the user bins if the bin has changed sessionsFromUser[user].forEach(function (userSession) { var oldBinKey = userSession.key; - // Is this the same bin? + // Is this the same bin? If so, keep it, we're cool. if (oldBinKey === newKey) return; - // Remove the session for the current utils.removeConnection(oldBinKey, userSession.res); // Create/get a new session for the new bin and add the connection to the // new session's res pool @@ -211,6 +209,7 @@ module.exports = { userSession.res.req.bin = newBin; userSession.key = newKey; }); + }, /** @@ -220,7 +219,7 @@ module.exports = { if (!newBin || !newBin.url) { // FIXME this is just patching a problem, the source of which I'm not sure - console.error('spike/index.js#bumpRevision - missing newBin', newBin); + console.error('spike/index.js#bump-revision - missing newBin', newBin); return; } @@ -233,8 +232,6 @@ module.exports = { // Move connections from one session to another oldSession.res.forEach(function (res) { - // Remove the connection for the old bin - utils.removeConnection(oldKey, res); // Add the connection to the new session's list newSession.res.push(res); // Upgrade the connection with the new bin @@ -246,8 +243,13 @@ module.exports = { res.end(); res.req.emit('close'); } + return false; }); + // There should be no more connections to this bin, so byebye! + oldSession.res = []; + delete sessions[oldKey]; + }, /**