UNPKG

reactotron-plugin-zustand

Version:
146 lines (142 loc) 5.13 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { WILDCARDS: () => WILDCARDS, default: () => reactotronPluginZustand }); module.exports = __toCommonJS(src_exports); var import_deepmerge = __toESM(require("deepmerge")); // src/utils.ts function omitFunctionRecursively(input, enable) { if (!enable) { return input; } return removeFunctions(input); } function removeFunctions(value) { if (typeof value !== "object" || value === null) { return value; } if (Array.isArray(value)) { return value.map(removeFunctions).filter((item) => typeof item !== "function"); } const newObj = {}; for (const key in value) { if (value.hasOwnProperty(key)) { const cleanedValue = removeFunctions(value[key]); if (typeof cleanedValue !== "function") { newObj[key] = cleanedValue; } } } return newObj; } // src/index.ts var WILDCARDS = ["*"]; function reactotronPluginZustand({ stores, omitFunctionKeys = false }) { return (reactotron) => { let subscriptions = []; return { onCommand: (command) => { var _a; if ((command == null ? void 0 : command.type) === "state.backup.request") { reactotron.send("state.backup.response", { state: stores.map((item) => ({ path: item.name, value: omitFunctionRecursively( item.store.getState(), true // always backup without functions ) })) }); } if ((command == null ? void 0 : command.type) === "state.restore.request") { command.payload.state.forEach((item) => { var _a2; const store = (_a2 = stores.find((sub) => sub.name === item.path)) == null ? void 0 : _a2.store; if (store != null) { store.setState( (state) => { var _a3; return (0, import_deepmerge.default)(state, (_a3 = item.value) != null ? _a3 : {}); } ); } }); } if ((command == null ? void 0 : command.type) === "state.values.subscribe") { const subPaths = (_a = command == null ? void 0 : command.payload) == null ? void 0 : _a.paths; if (subPaths.length === 0) { subscriptions.forEach((item) => { item.unsub(); }); reactotron.send("state.values.change", { changes: [] }); return; } const subStores = subPaths.some((item) => WILDCARDS.includes(item)) ? stores : stores.filter((item) => subPaths.includes(item.name)); const getTronState = () => subStores.map((item) => ({ path: item.name, value: omitFunctionRecursively( item.store.getState(), omitFunctionKeys ) })); reactotron.send("state.values.change", { changes: getTronState() }); subscriptions = subStores.map((item) => { return { name: item.name, store: item.store, unsub: item.store.subscribe((changes) => { const newState = getTronState().filter( (tStore) => tStore.path !== item.name ); reactotron.send("state.values.change", { changes: newState.concat({ path: item.name, value: omitFunctionRecursively(changes, omitFunctionKeys) }) }); }) }; }); } } }; }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { WILDCARDS });