From 5e0021126159156d2a36c93a8496778e49969f2e Mon Sep 17 00:00:00 2001 From: Rod Date: Fri, 16 Feb 2018 13:47:49 -0500 Subject: [PATCH] have amqp-base refuse to disconnect a connection it was passed unless it is explicitly asked to; add test for this behavior --- lib/amqp-base.coffee | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/amqp-base.coffee b/lib/amqp-base.coffee index 641daab..d356c90 100644 --- a/lib/amqp-base.coffee +++ b/lib/amqp-base.coffee @@ -4,7 +4,10 @@ class AmqpBase constructor:(@connection)-> if @connection? + @connection_shared = true @_on_connect() + else + @connection_shared = false # Establish a new connection to the specified broker. # @@ -65,18 +68,26 @@ class AmqpBase callback = undefined return @connection - disconnect:(callback)=> - if @connection?.disconnect? - @_on_disconnect ()=> - @connection.disconnect() - @connection = undefined - callback?(undefined, true) - return true + disconnect:(force, callback)=> + if typeof force is 'function' and not callback? + callback = force + force = undefined + if @connection_shared and not force + callback(new Error("This class did not create the current connection and hence will not disconnect from it unless `true` is passed as the `force` parameter.")) else - @_on_disconnect ()=> - @connection = undefined - callback?(undefined, false) - return false + if @connection?.disconnect? + @_on_disconnect ()=> + @connection.disconnect() + @connection_shared = false + @connection = undefined + callback?(undefined, true) + return true + else + @_on_disconnect ()=> + @connection_shared = false + @connection = undefined + callback?(undefined, false) + return false # hook for subclasses to clear or set state on connect _on_connect:(callback)->callback?()