"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BranchState = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /** * Copyright (c) 2017-present, Facebook, Inc. * 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. */ var _realm = require("../realm.js"); var _index = require("../values/index.js"); require("../serializer/types.js"); var _utils = require("./utils"); var _errors = require("./errors.js"); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } // Branch state is used to capture branched ReactElements so they can be analyzed and compared // once all branches have been processed. This allows us to add keys to the respective ReactElement // objects depending on various heuristics (if they have the same "type" for example) // A new branch state is created on a branch status of "NEW_BRANCH" and is reset to null once the branch is no // longer new // Branch status is used for when Prepack returns an abstract value from a render // that results in a conditional path occuring. This can be problematic for reconcilation // as the reconciler then needs to understand if this is the start of a new branch, or if // it's actually deep into an existing branch. If it's a new branch, we need to apply // keys to the root JSX element so that it keeps it identity (because we're folding trees). // Furthermore, we also need to bail-out of folding class components where they have lifecycle // events, as we can't merge lifecycles of mutliple trees when branched reliably var BranchState = exports.BranchState = function () { function BranchState() { _classCallCheck(this, BranchState); this._branchesToValidate = []; } _createClass(BranchState, [{ key: "_applyBranchedLogicValue", value: function _applyBranchedLogicValue(realm, reactSerializerState, value) { var _this = this; if (value instanceof _index.StringValue || value instanceof _index.NumberValue || value instanceof _index.BooleanValue || value instanceof _index.NullValue || value instanceof _index.UndefinedValue) { // terminal values } else if (value instanceof _index.ObjectValue && (0, _utils.isReactElement)(value)) { (0, _utils.addKeyToReactElement)(realm, reactSerializerState, value); } else if (value instanceof _index.ArrayValue) { (0, _utils.mapOverArrayValue)(realm, value, function (elementValue) { _this._applyBranchedLogicValue(realm, reactSerializerState, elementValue); }); } else if (value instanceof _index.AbstractValue) { var length = value.args.length; if (length > 0) { for (var i = 0; i < length; i++) { this._applyBranchedLogicValue(realm, reactSerializerState, value.args[i]); } } } else { throw new _errors.ExpectedBailOut("Unsupported value encountered when applying branched logic to values"); } } }, { key: "applyBranchedLogic", value: function applyBranchedLogic(realm, reactSerializerState) { var reactElementType = void 0; var applyBranchedLogic = false; for (var i = 0; i < this._branchesToValidate.length; i++) { var _type = this._branchesToValidate[i].type; if (reactElementType === undefined) { reactElementType = _type; } else if (_type !== reactElementType) { // the types of the ReactElements do not match, so apply branch logic applyBranchedLogic = true; break; } } if (applyBranchedLogic) { for (var _i = 0; _i < this._branchesToValidate.length; _i++) { this._applyBranchedLogicValue(realm, reactSerializerState, this._branchesToValidate[_i].value); } } } }, { key: "captureBranchedValue", value: function captureBranchedValue(type, value) { this._branchesToValidate.push({ type: type, value: value }); return value; } }]); return BranchState; }(); //# sourceMappingURL=branching.js.map