@neurosity/sdk
Version:
Neurosity SDK
26 lines (25 loc) • 1.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeJSONChunks = void 0;
const rxjs_1 = require("rxjs");
const stitch_1 = require("./stitch");
/**
* @hidden
*/
function decodeJSONChunks({ textCodec, characteristicName, delimiter, addLog }) {
return (0, rxjs_1.pipe)((0, rxjs_1.map)((arrayBuffer) => {
const decoded = textCodec.decode(arrayBuffer);
addLog(`Received chunk with buffer size of ${arrayBuffer.byteLength} and decoded size ${decoded.length} for ${characteristicName} characteristic: \n${decoded}`);
return decoded;
}), (0, stitch_1.stitchChunks)({ delimiter }), (0, rxjs_1.map)((payload) => {
var _a;
try {
return JSON.parse(payload);
}
catch (error) {
addLog(`Failed to parse JSON for ${characteristicName} characteristic. Falling back to unparsed string. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
return payload;
}
}));
}
exports.decodeJSONChunks = decodeJSONChunks;