dash-renderer
Version:
render dash components in react
214 lines (204 loc) • 12.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _ramda = require("ramda");
var _callbacks = require("../actions/callbacks");
var _dependencies = require("../actions/dependencies");
var _dependencies_ts = require("../actions/dependencies_ts");
var _actions = require("../actions");
var _paths = require("../actions/paths");
var _persistence = require("../persistence");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var observer = {
observer: _ref => {
var dispatch = _ref.dispatch,
getState = _ref.getState;
var _getState = getState(),
executed = _getState.callbacks.executed;
function applyProps(id, updatedProps) {
var _getState2 = getState(),
layout = _getState2.layout,
paths = _getState2.paths;
var itempath = (0, _paths.getPath)(paths, id);
if (!itempath) {
return false;
}
// This is a callback-generated update.
// Check if this invalidates existing persisted prop values,
// or if persistence changed, whether this updates other props.
updatedProps = (0, _persistence.prunePersistence)((0, _ramda.path)(itempath, layout), updatedProps, dispatch);
// In case the update contains whole components, see if any of
// those components have props to update to persist user edits.
var _applyPersistence = (0, _persistence.applyPersistence)({
props: updatedProps
}, dispatch),
props = _applyPersistence.props;
dispatch((0, _actions.updateProps)({
itempath,
props,
source: 'response',
renderType: 'callback'
}));
return props;
}
var requestedCallbacks = [];
var storedCallbacks = [];
executed.forEach(cb => {
var _cb$predecessors;
var predecessors = (0, _ramda.concat)((_cb$predecessors = cb.predecessors) !== null && _cb$predecessors !== void 0 ? _cb$predecessors : [], [cb.callback]);
var _cb$callback = cb.callback,
clientside_function = _cb$callback.clientside_function,
output = _cb$callback.output,
executionResult = cb.executionResult;
if ((0, _ramda.isNil)(executionResult)) {
return;
}
var data = executionResult.data,
error = executionResult.error,
payload = executionResult.payload;
if (data !== undefined) {
Object.entries(data).forEach(_ref2 => {
var _ref3 = _slicedToArray(_ref2, 2),
id = _ref3[0],
props = _ref3[1];
var parsedId = (0, _dependencies.parseIfWildcard)(id);
var _getState3 = getState(),
graphs = _getState3.graphs,
oldLayout = _getState3.layout,
oldPaths = _getState3.paths;
// Components will trigger callbacks on their own as required (eg. derived)
var appliedProps = applyProps(parsedId, props);
// Add callbacks for modified inputs
requestedCallbacks = (0, _ramda.concat)(requestedCallbacks, (0, _ramda.flatten)((0, _ramda.map)(prop => (0, _dependencies_ts.getCallbacksByInput)(graphs, oldPaths, parsedId, prop, true), (0, _ramda.keys)(props))).map(rcb => _objectSpread(_objectSpread({}, rcb), {}, {
predecessors
})));
var basePath = (0, _paths.getPath)(oldPaths, parsedId);
if (!basePath) {
return;
}
var oldObj = (0, _ramda.path)(basePath, oldLayout);
var childrenProps = (0, _ramda.pathOr)('defaultValue', [oldObj.namespace, oldObj.type], window.__dashprivate_childrenProps);
var handlePaths = function handlePaths(children, oldChildren, oldChildrenPath) {
var filterRoot = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var oPaths = getState().paths;
var paths = (0, _paths.computePaths)(children, oldChildrenPath, oPaths);
dispatch((0, _actions.setPaths)(paths));
// Get callbacks for new layout (w/ execution group)
requestedCallbacks = (0, _ramda.concat)(requestedCallbacks, (0, _dependencies_ts.getLayoutCallbacks)(graphs, paths, children, {
chunkPath: oldChildrenPath,
filterRoot
}).map(rcb => _objectSpread(_objectSpread({}, rcb), {}, {
predecessors
})));
// Wildcard callbacks with array inputs (ALL / ALLSMALLER) need to trigger
// even due to the deletion of components
requestedCallbacks = (0, _ramda.concat)(requestedCallbacks, (0, _dependencies_ts.getLayoutCallbacks)(graphs, oldPaths, oldChildren, {
removedArrayInputsOnly: true,
newPaths: paths,
chunkPath: oldChildrenPath,
filterRoot
}).map(rcb => _objectSpread(_objectSpread({}, rcb), {}, {
predecessors
})));
};
var recomputed = false;
['children'].concat(childrenProps).forEach(childrenProp => {
if (recomputed) {
return;
}
if (childrenProp.includes('[]')) {
var _childrenProp$split$m = childrenProp.split('[]').map(p => p.split('.').filter(e => e)),
_childrenProp$split$m2 = _slicedToArray(_childrenProp$split$m, 1),
frontPath = _childrenProp$split$m2[0];
var frontObj = (0, _ramda.path)(frontPath, appliedProps);
if (!frontObj) {
return;
}
// Crawl layout needs the ns/type
handlePaths(_objectSpread(_objectSpread({}, oldObj), {}, {
props: _objectSpread(_objectSpread({}, oldObj.props), appliedProps)
}), oldObj, basePath, (0, _ramda.keys)(appliedProps));
// Only do it once for the component.
recomputed = true;
} else {
var childrenPropPath = childrenProp.split('.');
var children = (0, _ramda.path)(childrenPropPath, appliedProps);
if (!children) {
return;
}
var oldChildrenPath = (0, _ramda.concat)((0, _paths.getPath)(oldPaths, parsedId), ['props'].concat(childrenPropPath));
var oldChildren = (0, _ramda.path)(oldChildrenPath, oldLayout);
handlePaths(children, oldChildren, oldChildrenPath);
}
});
// persistence edge case: if you explicitly update the
// persistence key, other props may change that require us
// to fire additional callbacks
var addedProps = (0, _ramda.pickBy)((_, k) => !(k in props), appliedProps);
if (!(0, _ramda.isEmpty)(addedProps)) {
var _getState4 = getState(),
currentGraphs = _getState4.graphs,
paths = _getState4.paths;
requestedCallbacks = (0, _ramda.concat)(requestedCallbacks, (0, _dependencies_ts.includeObservers)(id, addedProps, currentGraphs, paths).map(rcb => _objectSpread(_objectSpread({}, rcb), {}, {
predecessors
})));
}
});
// Add information about potentially updated outputs vs. updated outputs,
// this will be used to drop callbacks from execution groups when no output
// matching the downstream callback's inputs were modified
storedCallbacks.push(_objectSpread(_objectSpread({}, cb), {}, {
executionMeta: {
allProps: (0, _ramda.map)(_dependencies_ts.combineIdAndProp, (0, _ramda.flatten)(cb.getOutputs(getState().paths))),
updatedProps: (0, _ramda.flatten)((0, _ramda.map)(_ref4 => {
var _ref5 = _slicedToArray(_ref4, 2),
id = _ref5[0],
value = _ref5[1];
return (0, _ramda.map)(property => (0, _dependencies_ts.combineIdAndProp)({
id,
property
}), (0, _ramda.keys)(value));
}, (0, _ramda.toPairs)(data)))
}
}));
}
if (error !== undefined) {
var message;
if (cb.callback.no_output) {
var inpts = (0, _ramda.keys)(cb.changedPropIds).join(', ');
message = "Callback error with no output from input ".concat(inpts);
} else {
var outputs = payload ? (0, _ramda.map)(_dependencies_ts.combineIdAndProp, (0, _ramda.flatten)([payload.outputs])).join(', ') : output;
message = "Callback error updating ".concat(outputs);
}
if (clientside_function) {
var ns = clientside_function.namespace,
fn = clientside_function.function_name;
message += " via clientside function ".concat(ns, ".").concat(fn);
}
(0, _actions.handleAsyncError)(error, message, dispatch);
storedCallbacks.push(_objectSpread(_objectSpread({}, cb), {}, {
executionMeta: {
allProps: (0, _ramda.map)(_dependencies_ts.combineIdAndProp, (0, _ramda.flatten)(cb.getOutputs(getState().paths))),
updatedProps: []
}
}));
}
});
dispatch((0, _callbacks.aggregateCallbacks)([executed.length ? (0, _callbacks.removeExecutedCallbacks)(executed) : null, executed.length ? (0, _callbacks.addCompletedCallbacks)(executed.length) : null, storedCallbacks.length ? (0, _callbacks.addStoredCallbacks)(storedCallbacks) : null, requestedCallbacks.length ? (0, _callbacks.addRequestedCallbacks)(requestedCallbacks) : null]));
},
inputs: ['callbacks.executed']
};
var _default = exports.default = observer;