UNPKG

@neurosity/sdk

Version:
22 lines (21 loc) 908 B
import { map, pipe } from "rxjs"; import { stitchChunks } from "./stitch"; /** * @hidden */ export function decodeJSONChunks({ textCodec, characteristicName, delimiter, addLog }) { return pipe(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; }), stitchChunks({ delimiter }), 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; } })); }