synapse-react-client
Version:
[](https://badge.fury.io/js/synapse-react-client) [](https://github.com/prettier/prettie
57 lines (56 loc) • 1.55 kB
JavaScript
import n, { NOTIFICATION_TYPE_CODE as m } from "./JsonRxNotification.js";
import E, { REQUEST_TYPE_CODE as d } from "./JsonRxRequest.js";
import c, { RESPONSE_TYPE_CODE as p } from "./JsonRxResponse.js";
import i, { RESPONSE_COMPLETE_TYPE_CODE as R } from "./JsonRxResponseComplete.js";
import _, { REQUEST_COMPLETE_TYPE_CODE as u } from "./JsonRxRequestComplete.js";
class T {
/**
* Converts a JSON-encoded JSON-Rx message (which is always an array) into the appropriate JsonRxMessage subclass
*
* For more information, see the JSON-Rx specification: https://jsonjoy.com/specs/json-rx
* @param json
*/
static fromJson(r) {
if (!Array.isArray(r))
throw new Error("Invalid JSON-Rx message: not an array");
const t = r[0];
if (typeof t != "number")
throw new Error(
`Invalid JSON-Rx message: type code is not a number, received ${t}`
);
switch (t) {
case d: {
const [s, e, o, a] = r;
return new E(e, o, a);
}
case u: {
const [s, e, o, a] = r;
return new _(
e,
o,
a
);
}
case p: {
const [s, e, o] = r;
return new c(e, o);
}
case R: {
const [s, e, o] = r;
return new i(e, o);
}
case m: {
const [s, e, o] = r;
return new n(e, o);
}
default:
throw new Error(
`Invalid JSON-Rx message: unknown type code ${t}`
);
}
}
}
export {
T as default
};
//# sourceMappingURL=JsonRx.js.map