Repository URL to install this package:
|
Version:
1.2.0 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.dispatchMonitorAction = dispatchMonitorAction;
exports.isMonitorAction = void 0;
var mobx = _interopRequireWildcard(require("xmobx/mobx"));
var _exotic = require("../../exotic");
var _jsan = require("jsan");
var _remotedevUtils = require("remotedev-utils");
var _utils = require("./utils");
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
const isMonitorAction = store => store.__isRemotedevAction === true; // very nasty
exports.isMonitorAction = isMonitorAction;
function dispatch(store, {
type,
arguments: args
}) {
if ((0, _exotic.isFunction)(store[type])) {
(0, _utils.silently)(() => {
store[type](...args);
}, store);
}
}
function dispatchRemotely(devTools, store, payload) {
try {
(0, _remotedevUtils.evalMethod)(payload, store);
} catch (syntaxException) {
devTools.error(syntaxException.message);
}
}
function toggleAction(store, id, strState) {
const liftedState = (0, _jsan.parse)(strState);
const idx = liftedState.skippedActionIds.indexOf(id);
const skipped = idx !== -1;
const start = liftedState.stagedActionIds.indexOf(id);
if (start === -1) {
return liftedState;
}
(0, _utils.setValue)(store, liftedState.computedStates[start - 1].state);
for (let i = skipped ? start : start + 1; i < liftedState.stagedActionIds.length; i++) {
if (i !== start && liftedState.skippedActionIds.indexOf(liftedState.stagedActionIds[i]) !== -1) {
continue;
} // it's already skipped
dispatch(store, liftedState.actionsById[liftedState.stagedActionIds[i]].action);
liftedState.computedStates[i].state = mobx.toJS(store);
}
if (skipped) {
liftedState.skippedActionIds.splice(idx, 1);
} else {
liftedState.skippedActionIds.push(id);
}
return liftedState;
}
function dispatchMonitorAction(store, devTools, onlyActions) {
// eslint-disable-next-line
// debugger
const initValue = mobx.toJS(store);
devTools.init(initValue, (0, _remotedevUtils.getMethods)(store));
return message => {
if (message.type === 'DISPATCH') {
switch (message.payload.type) {
case 'RESET':
devTools.init((0, _utils.setValue)(store, initValue));
return;
case 'COMMIT':
devTools.init(mobx.toJS(store));
return;
case 'ROLLBACK':
devTools.init((0, _utils.setValue)(store, (0, _jsan.parse)(message.state)));
return;
case 'JUMP_TO_STATE':
case 'JUMP_TO_ACTION':
(0, _utils.setValue)(store, (0, _jsan.parse)(message.state));
return;
case 'TOGGLE_ACTION':
if (!onlyActions) {
console.warn('`onlyActions` parameter should be `true` to skip actions: ' + 'https://github.com/zalmoxisus/mobx-remotedev#remotedevstore-config');
return;
}
devTools.send(null, toggleAction(store, message.payload.id, message.state));
return;
case 'IMPORT_STATE':
{
const nextLiftedState = message.payload.nextLiftedState;
const computedStates = nextLiftedState.computedStates;
(0, _utils.setValue)(store, computedStates[computedStates.length - 1].state);
devTools.send(null, nextLiftedState);
}
}
} else if (message.type === 'ACTION') {
dispatchRemotely(devTools, store, message.payload);
}
};
}