@graffy/client
Advanced tools
Comparing version 0.14.2-alpha1 to 0.14.2-alpha2
@@ -5,3 +5,3 @@ { | ||
"author": "aravind (https://github.com/aravindet)", | ||
"version": "0.14.2-alpha1", | ||
"version": "0.14.2-alpha2", | ||
"main": "index.js", | ||
@@ -17,5 +17,5 @@ "source": "src/index.js", | ||
"@babel/runtime-corejs3": "^7.8.4", | ||
"@graffy/common": "0.14.2-alpha1", | ||
"@graffy/stream": "0.14.2-alpha1" | ||
"@graffy/common": "0.14.2-alpha2", | ||
"@graffy/stream": "0.14.2-alpha2" | ||
} | ||
} |
import makeStream from '@graffy/stream'; | ||
import { makeId } from '@graffy/common'; | ||
import Socket from './Socket'; | ||
@@ -7,40 +7,17 @@ export default (url, getOptions) => store => { | ||
let socket; | ||
const handlers = {}; | ||
let socket = new Socket(url, { onUnhandled }); | ||
function connect() { | ||
socket = new WebSocket(url); | ||
socket.onmessage = receive; | ||
function onUnhandled(id) { | ||
socket.send([id, 'unwatch']); | ||
} | ||
connect(); | ||
/* | ||
TODO: | ||
- Request buffering while connection isn't open | ||
- Reconnection, backoff, health heuristics | ||
*/ | ||
function receive(event) { | ||
const [id, ...data] = JSON.parse(event.data); | ||
if (handlers[id]) { | ||
handlers[id](...data); | ||
} else { | ||
// We received an unexpected push. | ||
socket.send(JSON.stringify(['unwatch', id])); | ||
} | ||
} | ||
function once(op, payload, options) { | ||
const id = makeId(); | ||
socket.send(JSON.stringify([op, id, payload, getOptions(op, options)])); | ||
return new Promise((resolve, reject) => { | ||
handlers[id] = (error, result) => { | ||
delete handlers[id]; | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(result); | ||
} | ||
}; | ||
const id = socket.start( | ||
[op, payload, getOptions(op, options)], | ||
(error, result) => { | ||
socket.stop(id); | ||
error ? reject(error) : resolve(result); | ||
}, | ||
); | ||
}); | ||
@@ -53,19 +30,19 @@ } | ||
store.on('watch', (query, options) => { | ||
const id = makeId(); | ||
const op = 'watch'; | ||
socket.send(JSON.stringify([op, id, query, getOptions(op, options)])); | ||
return makeStream((push, end) => { | ||
handlers[id] = (error, result) => { | ||
if (error) { | ||
delete handlers[id]; | ||
end(error); | ||
return; | ||
} | ||
push(result); | ||
}; | ||
const id = socket.start( | ||
[op, query, getOptions(op, options)], | ||
(error, result) => { | ||
if (error) { | ||
socket.stop(id); | ||
end(error); | ||
return; | ||
} | ||
push(result); | ||
}, | ||
); | ||
return () => { | ||
socket.send(JSON.stringify(['unwatch', id])); | ||
delete handlers[id]; | ||
socket.send([id, 'unwatch']); | ||
socket.stop(id); | ||
}; | ||
@@ -72,0 +49,0 @@ }); |
@@ -10,9 +10,5 @@ "use strict"; | ||
var _stringify = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/json/stringify")); | ||
var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice")); | ||
var _stream = _interopRequireDefault(require("@graffy/stream")); | ||
var _common = require("@graffy/common"); | ||
var _Socket = _interopRequireDefault(require("./Socket")); | ||
@@ -22,43 +18,16 @@ var _default = function _default(url, getOptions) { | ||
if (!WebSocket) throw Error('client.websocket.unavailable'); | ||
var socket; | ||
var handlers = {}; | ||
var socket = new _Socket["default"](url, { | ||
onUnhandled: onUnhandled | ||
}); | ||
function connect() { | ||
socket = new WebSocket(url); | ||
socket.onmessage = receive; | ||
function onUnhandled(id) { | ||
socket.send([id, 'unwatch']); | ||
} | ||
connect(); | ||
/* | ||
TODO: | ||
- Request buffering while connection isn't open | ||
- Reconnection, backoff, health heuristics | ||
*/ | ||
function receive(event) { | ||
var _JSON$parse = JSON.parse(event.data), | ||
id = _JSON$parse[0], | ||
data = (0, _slice["default"])(_JSON$parse).call(_JSON$parse, 1); | ||
if (handlers[id]) { | ||
handlers[id].apply(handlers, data); | ||
} else { | ||
// We received an unexpected push. | ||
socket.send((0, _stringify["default"])(['unwatch', id])); | ||
} | ||
} | ||
function once(op, payload, options) { | ||
var id = (0, _common.makeId)(); | ||
socket.send((0, _stringify["default"])([op, id, payload, getOptions(op, options)])); | ||
return new _promise["default"](function (resolve, reject) { | ||
handlers[id] = function (error, result) { | ||
delete handlers[id]; | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(result); | ||
} | ||
}; | ||
var id = socket.start([op, payload, getOptions(op, options)], function (error, result) { | ||
socket.stop(id); | ||
error ? reject(error) : resolve(result); | ||
}); | ||
}); | ||
@@ -74,9 +43,7 @@ } | ||
store.on('watch', function (query, options) { | ||
var id = (0, _common.makeId)(); | ||
var op = 'watch'; | ||
socket.send((0, _stringify["default"])([op, id, query, getOptions(op, options)])); | ||
return (0, _stream["default"])(function (push, end) { | ||
handlers[id] = function (error, result) { | ||
var id = socket.start([op, query, getOptions(op, options)], function (error, result) { | ||
if (error) { | ||
delete handlers[id]; | ||
socket.stop(id); | ||
end(error); | ||
@@ -87,7 +54,6 @@ return; | ||
push(result); | ||
}; | ||
}); | ||
return function () { | ||
socket.send((0, _stringify["default"])(['unwatch', id])); | ||
delete handlers[id]; | ||
socket.send([id, 'unwatch']); | ||
socket.stop(id); | ||
}; | ||
@@ -94,0 +60,0 @@ }); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14554
10
418
8
+ Added@graffy/common@0.14.2-alpha2(transitive)
+ Added@graffy/stream@0.14.2-alpha2(transitive)
- Removed@graffy/common@0.14.2-alpha1(transitive)
- Removed@graffy/stream@0.14.2-alpha1(transitive)
Updated@graffy/common@0.14.2-alpha2
Updated@graffy/stream@0.14.2-alpha2