From 1d632f277feaa3071fe1b1ec8e1dab866078095f Mon Sep 17 00:00:00 2001 From: chanced Date: Sun, 31 Oct 2021 15:36:12 -0400 Subject: [PATCH] add error protocol --- error.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 error.js diff --git a/error.js b/error.js new file mode 100644 index 0000000..4bc8942 --- /dev/null +++ b/error.js @@ -0,0 +1,34 @@ +import * as Y from "yjs"; // eslint-disable-line +import * as encoding from "lib0/encoding"; +import * as decoding from "lib0/decoding"; + +/** + * @param {encoding.Encoder} encoder + * @param {number} code + * @param {string} message + */ +export const writeError = (encoder, code, message) => { + encoding.writeVarUint(encoder, code); + encoding.writeVarString(encoder, message); +}; + +/** + * @callback ErrorHandler + * @param {any} y + * @param {number} code + * @param {string} message + */ + +/** + * + * @param {decoding.Decoder} decoder + * @param {Y.Doc} y + * @param {ErrorHandler} errorHandler + */ +export const readError = (decoder, y, errorHandler) => { + errorHandler( + y, + decoding.readVarUint(decoder), + decoding.readVarString(decoder) + ); +};