All files / core/operation AckUserWrite.ts

100% Statements 20/20
100% Branches 4/4
100% Functions 3/3
100% Lines 19/19
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                                12x 12x 12x     12x   1033x     1033x                 1033x 1033x 1033x           12x 616x 604x       604x         12x 8x         8x   4x 4x     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 { assert } from '@firebase/util';
import { Path } from '../util/Path';
import { Operation, OperationSource, OperationType } from './Operation';
import { ImmutableTree } from '../util/ImmutableTree';
 
export class AckUserWrite implements Operation {
  /** @inheritDoc */
  type = OperationType.ACK_USER_WRITE;
 
  /** @inheritDoc */
  source = OperationSource.User;
 
  /**
   *
   * @param {!Path} path
   * @param {!ImmutableTree<!boolean>} affectedTree A tree containing true for each affected path. Affected paths can't overlap.
   * @param {!boolean} revert
   */
  constructor(
    /**@inheritDoc */ public path: Path,
    /**@inheritDoc */ public affectedTree: ImmutableTree<boolean>,
    /**@inheritDoc */ public revert: boolean
  ) {}
 
  /**
   * @inheritDoc
   */
  operationForChild(childName: string): AckUserWrite {
    if (!this.path.isEmpty()) {
      assert(
        this.path.getFront() === childName,
        'operationForChild called for unrelated child.'
      );
      return new AckUserWrite(
        this.path.popFront(),
        this.affectedTree,
        this.revert
      );
    } else if (this.affectedTree.value != null) {
      assert(
        this.affectedTree.children.isEmpty(),
        'affectedTree should not have overlapping affected paths.'
      );
      // All child locations are affected as well; just return same operation.
      return this;
    } else {
      const childTree = this.affectedTree.subtree(new Path(childName));
      return new AckUserWrite(Path.Empty, childTree, this.revert);
    }
  }
}