reactotron-redux
Version:
A Reactotron plugin for Redux.
63 lines (62 loc) • 1.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = createSubscriptionHandler;
var _pathObject = _interopRequireDefault(require("./helpers/pathObject"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function createSubscriptionHandler(reactotron, onReduxStoreCreation) {
let subscriptions = [];
function setSubscriptions(subs) {
subscriptions = subs;
}
function getChanges() {
// If we don't have reactotron, dont have a store or getState isn't a function then get out. Now.
if (!reactotron || !reactotron.reduxStore || typeof reactotron.reduxStore.getState !== "function") {
return [];
}
const state = reactotron.reduxStore.getState();
const changes = [];
subscriptions.forEach(path => {
let cleanedPath = path;
let starredPath = false;
if (path && path.endsWith("*")) {
// Handle the star!
starredPath = true;
cleanedPath = path.substr(0, path.length - 2);
}
const values = (0, _pathObject.default)(cleanedPath, state);
if (starredPath && cleanedPath && values) {
changes.push(...Object.entries(values).map(val => ({
path: `${cleanedPath}.${val[0]}`,
value: val[1]
})));
} else {
changes.push({
path: cleanedPath,
value: values
});
}
});
return changes;
}
function sendSubscriptions() {
const changes = getChanges();
reactotron.stateValuesChange(changes);
}
function sendSubscriptionsIfNeeded() {
const changes = getChanges();
if (changes.length > 0) {
reactotron.stateValuesChange(changes);
}
}
onReduxStoreCreation(() => {
reactotron.reduxStore.subscribe(sendSubscriptionsIfNeeded);
});
return {
sendSubscriptions,
sendSubscriptionsIfNeeded,
setSubscriptions
};
}
//# sourceMappingURL=subscriptionsHandler.js.map