UNPKG

reactotron-plugin-zustand

Version:
91 lines (89 loc) 3.54 kB
"use strict"; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); // src/plugin.js module.exports = (pluginConfig) => (reactotron) => { var _a; reactotron.unsubscriptions = []; reactotron.stores = ((_a = pluginConfig == null ? void 0 : pluginConfig.stores) == null ? void 0 : _a.map((store) => __spreadProps(__spreadValues({}, store), { observable: false }))) || []; let changes = []; let subscriptions = []; function resetStores() { var _a2; reactotron.stores = ((_a2 = pluginConfig == null ? void 0 : pluginConfig.stores) == null ? void 0 : _a2.map((store) => __spreadProps(__spreadValues({}, store), { observable: false }))) || []; } function unsubscribeStores() { reactotron.unsubscriptions.forEach((store) => store()); } function updateState(name, state) { if (!reactotron) { return []; } const temp = Object.assign([], changes); const stateIndex = temp.findIndex((state2) => state2.path === name); if (stateIndex > -1) { temp[stateIndex].value = state; } else temp.push({ path: name, value: state }); return temp; } return { onCommand: (command) => { var _a2, _b, _c; if ((command == null ? void 0 : command.type) === "state.values.subscribe") { subscriptions = (_a2 = command == null ? void 0 : command.payload) == null ? void 0 : _a2.paths; if (((_c = (_b = command == null ? void 0 : command.payload) == null ? void 0 : _b.paths) == null ? void 0 : _c.length) === 0) { changes = []; resetStores(); unsubscribeStores(); reactotron == null ? void 0 : reactotron.send("state.values.change", { changes }); return; } reactotron.stores.filter( // Only add subscribe from stores added on reactotron client OR not observable. (store) => ( // (!store.observable && subscriptions.includes(store.name)) || !store.observable && subscriptions.includes(store.name) || subscriptions.some( (sub) => ["", "*", "all", "root"].includes(sub) ) ) ).forEach((store) => { var _a3; store.observable = true; const newState = updateState(store.name, store.zustand.getState()); changes = newState; reactotron.stateValuesChange(newState); reactotron.unsubscriptions.push( (_a3 = store == null ? void 0 : store.zustand) == null ? void 0 : _a3.subscribe((state) => { const newState2 = updateState(store.name, state); changes = newState2; reactotron.stateValuesChange(changes); }) ); }); } }, features: {} }; };