redux-websocket-bridge
Advanced tools
Comparing version 0.2.0-0 to 0.3.0-0
@@ -14,2 +14,3 @@ 'use strict'; | ||
exports.default = createWebSocketMiddleware; | ||
exports.trimUndefined = trimUndefined; | ||
@@ -47,6 +48,29 @@ var _blobToArrayBuffer = require('./blobToArrayBuffer'); | ||
binaryType: 'arraybuffer', | ||
fold: function fold(action, webSocket) { | ||
if (action.meta && arrayify(action.meta.send).some(function (send) { | ||
return send === true || send === webSocket; | ||
})) { | ||
var meta = action.meta, | ||
actionWithoutMeta = _objectWithoutProperties(action, ['meta']); | ||
return JSON.stringify(actionWithoutMeta); | ||
} | ||
}, | ||
meta: {}, | ||
namespace: '@@websocket/', | ||
unfold: true | ||
unfold: function unfold(payload, webSocket, raw) { | ||
var action = tryParseJSON(payload); | ||
return action && _extends({}, action, { | ||
meta: _extends({}, action.meta, { | ||
webSocket: webSocket | ||
}) | ||
}); | ||
} | ||
}; | ||
function arrayify(obj) { | ||
return obj ? Array.isArray(obj) ? obj : [obj] : []; | ||
} | ||
function createWebSocketMiddleware(urlOrFactory) { | ||
@@ -57,2 +81,3 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_OPTIONS; | ||
options.binaryType = options.binaryType.toLowerCase(); | ||
options.unfold = options.unfold && (typeof options.unfold === 'function' ? options.unfold : DEFAULT_OPTIONS.unfold); | ||
@@ -90,13 +115,12 @@ var _options = options, | ||
getPayload.then(function (payload) { | ||
if (typeof payload === 'string' && options.unfold) { | ||
var action = tryParseJSON(payload); | ||
return getPayload.then(function (payload) { | ||
if (options.unfold) { | ||
var action = options.unfold(payload, webSocket, payload); | ||
// TODO: Consider optional prefix to incoming actions | ||
if ((0, _isFSA2.default)(action)) { | ||
return store.dispatch(_extends({}, action, { | ||
meta: _extends({}, action.meta, { | ||
webSocket: webSocket | ||
}) | ||
})); | ||
if (action) { | ||
if (!(0, _isFSA2.default)(action)) { | ||
throw new Error('Unfolded action is not a Flux Standard Action compliant'); | ||
} | ||
return action && store.dispatch(action); | ||
} | ||
@@ -117,8 +141,6 @@ } | ||
webSocket.send(action.payload); | ||
} else if (action.meta && (action.meta.send === true || action.meta.send === namespace || action.meta.send === webSocket)) { | ||
var _action$meta = action.meta, | ||
send = _action$meta.send, | ||
meta = _objectWithoutProperties(_action$meta, ['send']); | ||
} else { | ||
var payload = options.fold(action, webSocket); | ||
webSocket.send(JSON.stringify(_extends({}, action, { meta: meta }))); | ||
payload && webSocket.send(payload); | ||
} | ||
@@ -137,2 +159,14 @@ | ||
} | ||
function trimUndefined(map) { | ||
return Object.keys(map).reduce(function (nextMap, key) { | ||
var value = map[key]; | ||
if (typeof value !== 'undefined') { | ||
nextMap[key] = value; | ||
} | ||
return nextMap; | ||
}, {}); | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "redux-websocket-bridge", | ||
"version": "0.2.0-0", | ||
"version": "0.3.0-0", | ||
"description": "WebSocket messages to Redux action bridge", | ||
@@ -40,3 +40,4 @@ "main": "lib/index.js", | ||
"test": "jest" | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
Sorry, the diff of this file is not supported yet
29263
10
153