All files / core/view View.ts

91.86% Statements 79/86
89.66% Branches 26/29
92.31% Functions 12/13
91.76% Lines 78/85
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292                                12x 12x 12x 12x 12x 12x 12x 12x 12x 12x                                   12x     305x               305x 305x   305x 305x           305x   305x 305x     305x         305x         305x         305x                   305x           305x           12x 1739x           12x 396x             12x 1578x 1578x     573x       457x     1121x           12x 165x           12x 410x               12x       165x 165x                             165x 155x 155x 184x 184x 30x 154x   147x 147x     155x   10x   165x                     12x         1326x       9x       9x           1326x 1326x           1326x   1326x           1326x   1326x                     12x 410x 410x 410x 379x 379x 141x     410x 58x   410x                           12x         1736x     1736x           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 { IndexedFilter } from './filter/IndexedFilter';
import { ViewProcessor } from './ViewProcessor';
import { ChildrenNode } from '../snap/ChildrenNode';
import { CacheNode } from './CacheNode';
import { ViewCache } from './ViewCache';
import { EventGenerator } from './EventGenerator';
import { assert } from '@firebase/util';
import { Operation, OperationType } from '../operation/Operation';
import { Change } from './Change';
import { PRIORITY_INDEX } from '../snap/indexes/PriorityIndex';
import { Query } from '../../api/Query';
import { EventRegistration } from './EventRegistration';
import { Node } from '../snap/Node';
import { Path } from '../util/Path';
import { WriteTreeRef } from '../WriteTree';
import { CancelEvent, Event } from './Event';
 
/**
 * A view represents a specific location and query that has 1 or more event registrations.
 *
 * It does several things:
 *  - Maintains the list of event registrations for this location/query.
 *  - Maintains a cache of the data visible for this location/query.
 *  - Applies new operations (via applyOperation), updates the cache, and based on the event
 *    registrations returns the set of events to be raised.
 * @constructor
 */
export class View {
  private processor_: ViewProcessor;
  private viewCache_: ViewCache;
  private eventRegistrations_: EventRegistration[] = [];
  private eventGenerator_: EventGenerator;
 
  /**
   *
   * @param {!Query} query_
   * @param {!ViewCache} initialViewCache
   */
  constructor(private query_: Query, initialViewCache: ViewCache) {
    const params = this.query_.getQueryParams();
 
    const indexFilter = new IndexedFilter(params.getIndex());
    const filter = params.getNodeFilter();
 
    /**
     * @type {ViewProcessor}
     * @private
     */
    this.processor_ = new ViewProcessor(filter);
 
    const initialServerCache = initialViewCache.getServerCache();
    const initialEventCache = initialViewCache.getEventCache();
 
    // Don't filter server node with other filter than index, wait for tagged listen
    const serverSnap = indexFilter.updateFullNode(
      ChildrenNode.EMPTY_NODE,
      initialServerCache.getNode(),
      null
    );
    const eventSnap = filter.updateFullNode(
      ChildrenNode.EMPTY_NODE,
      initialEventCache.getNode(),
      null
    );
    const newServerCache = new CacheNode(
      serverSnap,
      initialServerCache.isFullyInitialized(),
      indexFilter.filtersNodes()
    );
    const newEventCache = new CacheNode(
      eventSnap,
      initialEventCache.isFullyInitialized(),
      filter.filtersNodes()
    );
 
    /**
     * @type {!ViewCache}
     * @private
     */
    this.viewCache_ = new ViewCache(newEventCache, newServerCache);
 
    /**
     * @type {!EventGenerator}
     * @private
     */
    this.eventGenerator_ = new EventGenerator(this.query_);
  }
 
  /**
   * @return {!Query}
   */
  getQuery(): Query {
    return this.query_;
  }
 
  /**
   * @return {?Node}
   */
  getServerCache(): Node | null {
    return this.viewCache_.getServerCache().getNode();
  }
 
  /**
   * @param {!Path} path
   * @return {?Node}
   */
  getCompleteServerCache(path: Path): Node | null {
    const cache = this.viewCache_.getCompleteServerSnap();
    if (cache) {
      // If this isn't a "loadsAllData" view, then cache isn't actually a complete cache and
      // we need to see if it contains the child we're interested in.
      if (
        this.query_.getQueryParams().loadsAllData() ||
        (!path.isEmpty() && !cache.getImmediateChild(path.getFront()).isEmpty())
      ) {
        return cache.getChild(path);
      }
    }
    return null;
  }
 
  /**
   * @return {boolean}
   */
  isEmpty(): boolean {
    return this.eventRegistrations_.length === 0;
  }
 
  /**
   * @param {!EventRegistration} eventRegistration
   */
  addEventRegistration(eventRegistration: EventRegistration) {
    this.eventRegistrations_.push(eventRegistration);
  }
 
  /**
   * @param {?EventRegistration} eventRegistration If null, remove all callbacks.
   * @param {Error=} cancelError If a cancelError is provided, appropriate cancel events will be returned.
   * @return {!Array.<!Event>} Cancel events, if cancelError was provided.
   */
  removeEventRegistration(
    eventRegistration: EventRegistration | null,
    cancelError?: Error
  ): Event[] {
    const cancelEvents: CancelEvent[] = [];
    Iif (cancelError) {
      assert(
        eventRegistration == null,
        'A cancel should cancel all event registrations.'
      );
      const path = this.query_.path;
      this.eventRegistrations_.forEach(function(registration) {
        cancelError /** @type {!Error} */ = cancelError;
        const maybeEvent = registration.createCancelEvent(cancelError, path);
        if (maybeEvent) {
          cancelEvents.push(maybeEvent);
        }
      });
    }
 
    if (eventRegistration) {
      let remaining = [];
      for (let i = 0; i < this.eventRegistrations_.length; ++i) {
        const existing = this.eventRegistrations_[i];
        if (!existing.matches(eventRegistration)) {
          remaining.push(existing);
        } else if (eventRegistration.hasAnyCallback()) {
          // We're removing just this one
          remaining = remaining.concat(this.eventRegistrations_.slice(i + 1));
          break;
        }
      }
      this.eventRegistrations_ = remaining;
    } else {
      this.eventRegistrations_ = [];
    }
    return cancelEvents;
  }
 
  /**
   * Applies the given Operation, updates our cache, and returns the appropriate events.
   *
   * @param {!Operation} operation
   * @param {!WriteTreeRef} writesCache
   * @param {?Node} completeServerCache
   * @return {!Array.<!Event>}
   */
  applyOperation(
    operation: Operation,
    writesCache: WriteTreeRef,
    completeServerCache: Node | null
  ): Event[] {
    if (
      operation.type === OperationType.MERGE &&
      operation.source.queryId !== null
    ) {
      assert(
        this.viewCache_.getCompleteServerSnap(),
        'We should always have a full cache before handling merges'
      );
      assert(
        this.viewCache_.getCompleteEventSnap(),
        'Missing event cache, even though we have a server cache'
      );
    }
 
    const oldViewCache = this.viewCache_;
    const result = this.processor_.applyOperation(
      oldViewCache,
      operation,
      writesCache,
      completeServerCache
    );
    this.processor_.assertIndexed(result.viewCache);
 
    assert(
      result.viewCache.getServerCache().isFullyInitialized() ||
        !oldViewCache.getServerCache().isFullyInitialized(),
      'Once a server snap is complete, it should never go back'
    );
 
    this.viewCache_ = result.viewCache;
 
    return this.generateEventsForChanges_(
      result.changes,
      result.viewCache.getEventCache().getNode(),
      null
    );
  }
 
  /**
   * @param {!EventRegistration} registration
   * @return {!Array.<!Event>}
   */
  getInitialEvents(registration: EventRegistration): Event[] {
    const eventSnap = this.viewCache_.getEventCache();
    const initialChanges: Change[] = [];
    if (!eventSnap.getNode().isLeafNode()) {
      const eventNode = eventSnap.getNode() as ChildrenNode;
      eventNode.forEachChild(PRIORITY_INDEX, function(key, childNode) {
        initialChanges.push(Change.childAddedChange(key, childNode));
      });
    }
    if (eventSnap.isFullyInitialized()) {
      initialChanges.push(Change.valueChange(eventSnap.getNode()));
    }
    return this.generateEventsForChanges_(
      initialChanges,
      eventSnap.getNode(),
      registration
    );
  }
 
  /**
   * @private
   * @param {!Array.<!Change>} changes
   * @param {!Node} eventCache
   * @param {EventRegistration=} eventRegistration
   * @return {!Array.<!Event>}
   */
  generateEventsForChanges_(
    changes: Change[],
    eventCache: Node,
    eventRegistration?: EventRegistration
  ): Event[] {
    const registrations = eventRegistration
      ? [eventRegistration]
      : this.eventRegistrations_;
    return this.eventGenerator_.generateEventsForChanges(
      changes,
      eventCache,
      registrations
    );
  }
}