"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getState = getState; exports.initializeState = initializeState; exports.removeState = removeState; exports.getServerData = getServerData; exports.setServerData = setServerData; exports.getPendingOps = getPendingOps; exports.setPendingOp = setPendingOp; exports.pushPendingState = pushPendingState; exports.popPendingState = popPendingState; exports.mergeFirstPendingState = mergeFirstPendingState; exports.getObjectCache = getObjectCache; exports.estimateAttribute = estimateAttribute; exports.estimateAttributes = estimateAttributes; exports.commitServerChanges = commitServerChanges; exports.enqueueTask = enqueueTask; exports.clearAllState = clearAllState; exports.duplicateState = duplicateState; var ObjectStateMutations = _interopRequireWildcard(require("./ObjectStateMutations")); 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; } } /** * Copyright (c) 2015-present, Parse, LLC. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * * @flow */ let objectState /*: { [className: string]: { [id: string]: State } }*/ = {}; function getState(obj /*: ObjectIdentifier*/ ) /*: ?State*/ { const classData = objectState[obj.className]; if (classData) { return classData[obj.id] || null; } return null; } function initializeState(obj /*: ObjectIdentifier*/ , initial /*:: ?: State*/ ) /*: State*/ { let state = getState(obj); if (state) { return state; } if (!objectState[obj.className]) { objectState[obj.className] = {}; } if (!initial) { initial = ObjectStateMutations.defaultState(); } state = objectState[obj.className][obj.id] = initial; return state; } function removeState(obj /*: ObjectIdentifier*/ ) /*: ?State*/ { const state = getState(obj); if (state === null) { return null; } delete objectState[obj.className][obj.id]; return state; } function getServerData(obj /*: ObjectIdentifier*/ ) /*: AttributeMap*/ { const state = getState(obj); if (state) { return state.serverData; } return {}; } function setServerData(obj /*: ObjectIdentifier*/ , attributes /*: AttributeMap*/ ) { const serverData = initializeState(obj).serverData; ObjectStateMutations.setServerData(serverData, attributes); } function getPendingOps(obj /*: ObjectIdentifier*/ ) /*: Array*/ { const state = getState(obj); if (state) { return state.pendingOps; } return [{}]; } function setPendingOp(obj /*: ObjectIdentifier*/ , attr /*: string*/ , op /*: ?Op*/ ) { const pendingOps = initializeState(obj).pendingOps; ObjectStateMutations.setPendingOp(pendingOps, attr, op); } function pushPendingState(obj /*: ObjectIdentifier*/ ) { const pendingOps = initializeState(obj).pendingOps; ObjectStateMutations.pushPendingState(pendingOps); } function popPendingState(obj /*: ObjectIdentifier*/ ) /*: OpsMap*/ { const pendingOps = initializeState(obj).pendingOps; return ObjectStateMutations.popPendingState(pendingOps); } function mergeFirstPendingState(obj /*: ObjectIdentifier*/ ) { const pendingOps = getPendingOps(obj); ObjectStateMutations.mergeFirstPendingState(pendingOps); } function getObjectCache(obj /*: ObjectIdentifier*/ ) /*: ObjectCache*/ { const state = getState(obj); if (state) { return state.objectCache; } return {}; } function estimateAttribute(obj /*: ObjectIdentifier*/ , attr /*: string*/ ) /*: mixed*/ { const serverData = getServerData(obj); const pendingOps = getPendingOps(obj); return ObjectStateMutations.estimateAttribute(serverData, pendingOps, obj.className, obj.id, attr); } function estimateAttributes(obj /*: ObjectIdentifier*/ ) /*: AttributeMap*/ { const serverData = getServerData(obj); const pendingOps = getPendingOps(obj); return ObjectStateMutations.estimateAttributes(serverData, pendingOps, obj.className, obj.id); } function commitServerChanges(obj /*: ObjectIdentifier*/ , changes /*: AttributeMap*/ ) { const state = initializeState(obj); ObjectStateMutations.commitServerChanges(state.serverData, state.objectCache, changes); } function enqueueTask(obj /*: ObjectIdentifier*/ , task /*: () => Promise*/ ) /*: Promise*/ { const state = initializeState(obj); return state.tasks.enqueue(task); } function clearAllState() { objectState = {}; } function duplicateState(source /*: {id: string}*/ , dest /*: {id: string}*/ ) { dest.id = source.id; }