From 44a33520038c9f82ee2c89e556e2c946fa68f92e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 19 Jul 2019 16:46:40 -0400 Subject: [PATCH] fix: socket.io methods calling callbacks twice if method returns promise --- src/socket.io/index.js | 16 ++++++++-------- test/socket.io.js | 10 +++++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 1e101953ba..4bd2362e13 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -131,7 +131,6 @@ function onMessage(socket, payload) { return socket.disconnect(); } - var cbCalled = false; async.waterfall([ function (next) { checkMaintenance(socket, next); @@ -147,19 +146,20 @@ function onMessage(socket, payload) { } }, function (next) { - const returned = methodToCall(socket, params, next); + let callbackCalled = false; + function nextOnce(err, res) { + if (callbackCalled) { return; } + callbackCalled = true; + next(err, res); + } + const returned = methodToCall(socket, params, nextOnce); if (returned && typeof returned.then === 'function') { returned.then((payload) => { - next(null, payload); + nextOnce(null, payload); }, next); } }, ], function (err, result) { - if (cbCalled) { - return; - } - - cbCalled = true; callback(err ? { message: err.message } : null, result); }); } diff --git a/test/socket.io.js b/test/socket.io.js index 20dcdd7ed3..9bb2d53e43 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -124,6 +124,14 @@ describe('socket.io', function () { }); }); + it('should get more unread topics', function (done) { + io.emit('topics.loadMoreSortedTopics', { after: 0, count: 10, direction: 1, sort: 'unread' }, function (err, result) { + assert.ifError(err); + console.log(result); + done(); + }); + }); + it('should ban a user', function (done) { var socketUser = require('../src/socket.io/user'); socketUser.banUsers({ uid: adminUid }, { uids: [regularUid], reason: 'spammer' }, function (err) { @@ -182,7 +190,7 @@ describe('socket.io', function () { }); }); - describe('create/delete', function () { + describe('user create/delete', function () { var uid; it('should create a user', function (done) { socketAdmin.user.createUser({ uid: adminUid }, { username: 'foo1' }, function (err, _uid) {