All files / core/operation Merge.ts

91.3% Statements 21/23
83.33% Branches 5/6
75% Functions 3/4
90.91% Lines 20/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                12x 12x 12x 12x                     12x   36x     36x 36x 36x           12x 17x 3x 3x   2x 1x   1x           14x       14x             12x                     12x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { Operation, OperationSource, OperationType } from './Operation';
import { Overwrite } from './Overwrite';
import { Path } from '../util/Path';
import { assert } from '@firebase/util';
import { ImmutableTree } from '../util/ImmutableTree';
import { Node } from '../snap/Node';
 
/**
 * @param {!OperationSource} source
 * @param {!Path} path
 * @param {!ImmutableTree.<!Node>} children
 * @constructor
 * @implements {Operation}
 */
export class Merge implements Operation {
  /** @inheritDoc */
  type = OperationType.MERGE;
 
  constructor(
    /**@inheritDoc */ public source: OperationSource,
    /**@inheritDoc */ public path: Path,
    /**@inheritDoc */ public children: ImmutableTree<Node>
  ) {}
 
  /**
   * @inheritDoc
   */
  operationForChild(childName: string): Operation {
    if (this.path.isEmpty()) {
      const childTree = this.children.subtree(new Path(childName));
      if (childTree.isEmpty()) {
        // This child is unaffected
        return null;
      } else Eif (childTree.value) {
        // We have a snapshot for the child in question.  This becomes an overwrite of the child.
        return new Overwrite(this.source, Path.Empty, childTree.value);
      } else {
        // This is a merge at a deeper level
        return new Merge(this.source, Path.Empty, childTree);
      }
    } else {
      assert(
        this.path.getFront() === childName,
        "Can't get a merge for a child not on the path of the operation"
      );
      return new Merge(this.source, this.path.popFront(), this.children);
    }
  }
 
  /**
   * @inheritDoc
   */
  toString(): string {
    return (
      'Operation(' +
      this.path +
      ': ' +
      this.source.toString() +
      ' merge: ' +
      this.children.toString() +
      ')'
    );
  }
}