YUKI Hiroshi
null+****@clear*****
Wed Oct 16 16:54:51 JST 2013
YUKI Hiroshi 2013-10-16 16:54:51 +0900 (Wed, 16 Oct 2013) New Revision: c5412681bad81bd9f8bde9c4fb66d63affc8c4b9 https://github.com/droonga/express-droonga/commit/c5412681bad81bd9f8bde9c4fb66d63affc8c4b9 Message: Don't listen "*.notification" events before subscribing or after unsubscribing Modified files: lib/adapter/socket.io.js Modified: lib/adapter/socket.io.js (+23 -9) =================================================================== --- lib/adapter/socket.io.js 2013-10-16 16:44:35 +0900 (b4da7a7) +++ lib/adapter/socket.io.js 2013-10-16 16:54:51 +0900 (4e2d4a8) @@ -33,7 +33,7 @@ exports.register = function(application, server, params) { if (!server) throw new Error('A server instance is required!'); - function createClientMessageHandler(commandName, socket) { + function createClientMessageHandler(commandName, socket, onResponseCallback) { return (function(data, clientOptions) { var commandDefinition = unifiedCommandSet[commandName]; if (!commandDefinition) @@ -75,6 +75,9 @@ exports.register = function(application, server, params) { options.event = responseEvent; } + if (onResponseCallback && typeof onResponseCallback == 'function') + onResponseCallback(); + var wrappedSocket = new wrapper.SocketIOClientSocketWrapper(socket, options); if (commandDefinition[responseHandler]) { try { @@ -159,19 +162,29 @@ exports.register = function(application, server, params) { if (command.PublishSubscribe.isInstance(registeredCommand.definition)) { var subscribeEvent = registeredCommand.name + '.subscribe'; var unsubscribeEvent = registeredCommand.name + '.unsubscribe'; + var notificationEvent = registeredCommand.name + '.notification'; + var notificationHandler = createNotificationHandler(notificationEvent, socket); + socket.on(subscribeEvent, - createClientMessageHandler(subscribeEvent, socket)); + createClientMessageHandler(subscribeEvent, socket, function() { + if (handlers[notificationEvent]) + return; + connection.on(notificationEvent, notificationHandler); + handlers[notificationEvent] = notificationHandler; + })); socket.on(unsubscribeEvent, - createClientMessageHandler(unsubscribeEvent, socket)); + createClientMessageHandler(unsubscribeEvent, socket, function() { + if (!handlers[notificationEvent]) + return; + connection.removeListener(notificationEvent, notificationHandler); + delete handlers[notificationEvent]; + })); + unifiedCommandSet[subscribeEvent] = registeredCommand.definition; unifiedCommandSet[unsubscribeEvent] = registeredCommand.definition; - - var notificationEvent = registeredCommand.name + '.notification'; - var notificationHandler = createNotificationHandler(notificationEvent, socket); - connection.on(notificationEvent, notificationHandler); unifiedCommandSet[notificationEvent] = registeredCommand.definition; + events.push(notificationEvent); - handlers[notificationEvent] = notificationHandler; } else { socket.on(registeredCommand.name, createClientMessageHandler(registeredCommand.name, socket)); @@ -183,7 +196,8 @@ exports.register = function(application, server, params) { socket.on('disconnect', function() { events.forEach(function(event) { - connection.removeListener(event, handlers[event]); + if (handlers[event]) + connection.removeListener(event, handlers[event]); }); events = undefined; handlers = undefined; -------------- next part -------------- HTML����������������������������...Download