@ethicdevs/react-global-state-hooks-debugger
Version:
A small websocket based debugger for use with the react-global-state-hooks library
50 lines (49 loc) • 1.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeGetDebuggerLogger = void 0;
const react_global_state_hooks_1 = require("@ethicdevs/react-global-state-hooks");
const json_decycle_1 = __importDefault(require("json-decycle"));
const types_1 = require("./types");
const ws_client_1 = require("./ws-client");
function logActionType(action) {
return ["action", Date.now(), JSON.stringify(action, json_decycle_1.default.decycle())];
}
function logStateType(state) {
return ["state", Date.now(), JSON.stringify(state, json_decycle_1.default.decycle())];
}
function makeGetDebuggerLogger(options) {
let wsClient = (0, ws_client_1.makeWsClient)(options === null || options === void 0 ? void 0 : options.wsUri);
function getDebuggerLogger(loggerType) {
switch (loggerType) {
case react_global_state_hooks_1.LoggerType.Dispatch: {
return {
logAction(action) {
if (wsClient != null) {
wsClient.send(logActionType(action));
}
},
};
}
case react_global_state_hooks_1.LoggerType.State: {
return {
logState(state) {
if (wsClient != null) {
wsClient.send(logStateType(state));
}
},
};
}
}
}
function cleanupDebuggerLogger() {
if (wsClient != null && wsClient.readyState <= types_1.WS_READY_STATE.Open) {
wsClient.stop();
wsClient = null; // so it get's garbage collected
}
}
return [getDebuggerLogger, cleanupDebuggerLogger];
}
exports.makeGetDebuggerLogger = makeGetDebuggerLogger;