UNPKG

parse

Version:
146 lines (145 loc) 4.92 kB
"use strict"; var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map"); var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor"); var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); _Object$defineProperty(exports, "__esModule", { value: true }); exports.clearAllState = clearAllState; exports.commitServerChanges = commitServerChanges; exports.duplicateState = duplicateState; exports.enqueueTask = enqueueTask; exports.estimateAttribute = estimateAttribute; exports.estimateAttributes = estimateAttributes; exports.getObjectCache = getObjectCache; exports.getPendingOps = getPendingOps; exports.getServerData = getServerData; exports.getState = getState; exports.initializeState = initializeState; exports.mergeFirstPendingState = mergeFirstPendingState; exports.popPendingState = popPendingState; exports.pushPendingState = pushPendingState; exports.removeState = removeState; exports.setPendingOp = setPendingOp; exports.setServerData = setServerData; var _create = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/create")); var ObjectStateMutations = _interopRequireWildcard(require("./ObjectStateMutations")); function _interopRequireWildcard(e, t) { if ("function" == typeof _WeakMap) var r = new _WeakMap(), n = new _WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = _Object$defineProperty) && _Object$getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } // Use Object.create(null) to create an object without prototype chain // This prevents prototype pollution attacks let objectState = (0, _create.default)(null); function getState(obj) { const classData = objectState[obj.className]; if (classData) { return classData[obj.id] || null; } return null; } function initializeState(obj, initial) { let state = getState(obj); if (state) { return state; } if (!objectState[obj.className]) { // Use Object.create(null) for nested objects too objectState[obj.className] = (0, _create.default)(null); } if (!initial) { initial = ObjectStateMutations.defaultState(); } state = objectState[obj.className][obj.id] = initial; return state; } function removeState(obj) { const state = getState(obj); if (state === null) { return null; } delete objectState[obj.className][obj.id]; return state; } function getServerData(obj) { const state = getState(obj); if (state) { return state.serverData; } return {}; } function setServerData(obj, attributes) { const serverData = initializeState(obj).serverData; ObjectStateMutations.setServerData(serverData, attributes); } function getPendingOps(obj) { const state = getState(obj); if (state) { return state.pendingOps; } return [{}]; } function setPendingOp(obj, attr, op) { const pendingOps = initializeState(obj).pendingOps; ObjectStateMutations.setPendingOp(pendingOps, attr, op); } function pushPendingState(obj) { const pendingOps = initializeState(obj).pendingOps; ObjectStateMutations.pushPendingState(pendingOps); } function popPendingState(obj) { const pendingOps = initializeState(obj).pendingOps; return ObjectStateMutations.popPendingState(pendingOps); } function mergeFirstPendingState(obj) { const pendingOps = getPendingOps(obj); ObjectStateMutations.mergeFirstPendingState(pendingOps); } function getObjectCache(obj) { const state = getState(obj); if (state) { return state.objectCache; } return {}; } function estimateAttribute(obj, attr) { const serverData = getServerData(obj); const pendingOps = getPendingOps(obj); return ObjectStateMutations.estimateAttribute(serverData, pendingOps, obj, attr); } function estimateAttributes(obj) { const serverData = getServerData(obj); const pendingOps = getPendingOps(obj); return ObjectStateMutations.estimateAttributes(serverData, pendingOps, obj); } function commitServerChanges(obj, changes) { const state = initializeState(obj); ObjectStateMutations.commitServerChanges(state.serverData, state.objectCache, changes); } function enqueueTask(obj, task) { const state = initializeState(obj); return state.tasks.enqueue(task); } function clearAllState() { objectState = (0, _create.default)(null); } function duplicateState(source, dest) { dest.id = source.id; }