Comparing version 0.7.0 to 0.7.1
{ | ||
"name": "sonic-js", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "ws client library for the Sonic protocol", | ||
@@ -5,0 +5,0 @@ "main": "src/lib.js", |
@@ -46,2 +46,3 @@ /* eslint-env node, mocha */ | ||
}; | ||
var cancelCb = false; | ||
var emitter = client.stream(query); | ||
@@ -53,2 +54,3 @@ | ||
assert.equal(client.ws.length, 0); | ||
assert(cancelCb, 'cancelCb was not called!'); | ||
done(); | ||
@@ -59,3 +61,5 @@ }); | ||
}); | ||
emitter.cancel(); | ||
emitter.cancel(function() { | ||
cancelCb = true; | ||
}); | ||
}); | ||
@@ -71,2 +75,3 @@ }); | ||
}; | ||
var cancelCb = false; | ||
var closeable = client.run(query, function(err, d) { | ||
@@ -77,2 +82,3 @@ if (err || d.length !== 0) { | ||
assert.equal(client.ws.length, 0); | ||
assert(cancelCb, 'cancelCb was not called!'); | ||
done(); | ||
@@ -82,4 +88,6 @@ } | ||
assert.equal(client.ws.length, 1); | ||
closeable.cancel(); | ||
closeable.cancel(function() { | ||
cancelCb = true; | ||
}); | ||
}); | ||
}); |
@@ -8,2 +8,4 @@ 'use strict'; | ||
var utils = require('./util'); | ||
var SonicMessage = utils.SonicMessage; | ||
var noop = function() {}; | ||
@@ -18,9 +20,20 @@ // this is an ugly hack to prevent browseryfied `ws` module to throw errors at runtime | ||
function cancel(ws) { | ||
function cancel(ws, _cb) { | ||
var cb = typeof _cb === 'function' ? _cb : noop; | ||
function doSend() { | ||
if (BrowserWebSocket) { | ||
try { | ||
ws.send(SonicMessage.CANCEL); | ||
cb(); | ||
} catch (e) { | ||
cb(e); | ||
} | ||
} else { | ||
ws.send(SonicMessage.CANCEL, cb); | ||
} | ||
} | ||
if (ws.readyState === WebSocket.OPEN) { | ||
ws.send(JSON.stringify({ e: 'C' })); | ||
doSend(); | ||
} else { | ||
ws.on('open', function() { | ||
ws.send(JSON.stringify({ e: 'C' })); | ||
}); | ||
ws.on('open', doSend); | ||
} | ||
@@ -114,6 +127,6 @@ } | ||
if (BrowserWebSocket) { | ||
ws.send(JSON.stringify({ e: 'A' })); | ||
ws.send(SonicMessage.ACK); | ||
checkMsg(); | ||
} else { | ||
ws.send(JSON.stringify({ e: 'A' }), checkMsg); | ||
ws.send(SonicMessage.ACK, checkMsg); | ||
} | ||
@@ -192,4 +205,4 @@ break; | ||
emitter.cancel = function() { | ||
cancel(ws); | ||
emitter.cancel = function(cb) { | ||
cancel(ws, cb); | ||
}; | ||
@@ -221,4 +234,4 @@ | ||
return { | ||
cancel: function() { | ||
cancel(ws); | ||
cancel: function(cb) { | ||
cancel(ws, cb); | ||
} | ||
@@ -225,0 +238,0 @@ }; |
@@ -57,3 +57,7 @@ 'use strict'; | ||
toMsg: toMsg, | ||
toProgress: toProgress | ||
toProgress: toProgress, | ||
SonicMessage: { | ||
ACK: JSON.stringify({ e: 'A' }), | ||
CANCEL: JSON.stringify({ e: 'C' }) | ||
} | ||
}; |
43553
704