all files / firebase/modules/database/ transaction.js

71.79% Statements 28/39
38.1% Branches 8/21
77.78% Functions 7/9
72.97% Lines 27/37
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                                                                                                                                            10×                                                                                                                                        
/**
 * @flow
 * Database Transaction representation wrapper
 */
import { getAppEventName, SharedEventEmitter } from '../../utils/events';
import { getLogger } from '../../utils/log';
import { getNativeModule } from '../../utils/native';
import type Database from './';
 
let transactionId = 0;
 
/**
 * Uses the push id generator to create a transaction id
 * @returns {number}
 * @private
 */
const generateTransactionId = (): number => transactionId++;
 
/**
 * @class TransactionHandler
 */
export default class TransactionHandler {
  _database: Database;
  _transactions: { [number]: Object };
 
  constructor(database: Database) {
    this._transactions = {};
    this._database = database;
 
    SharedEventEmitter.addListener(
      getAppEventName(this._database, 'database_transaction_event'),
      this._handleTransactionEvent.bind(this)
    );
  }
 
  /**
   * Add a new transaction and start it natively.
   * @param reference
   * @param transactionUpdater
   * @param onComplete
   * @param applyLocally
   */
  add(
    reference: Object,
    transactionUpdater: Function,
    onComplete?: Function,
    applyLocally?: boolean = false
  ) {
    const id = generateTransactionId();
 
    this._transactions[id] = {
      id,
      reference,
      transactionUpdater,
      onComplete,
      applyLocally,
      completed: false,
      started: true,
    };
 
    getNativeModule(this._database).transactionStart(
      reference.path,
      id,
      applyLocally
    );
  }
 
  /**
   *  INTERNALS
   */
 
  /**
   *
   * @param event
   * @returns {*}
   * @private
   */
  _handleTransactionEvent(event: Object = {}) {
    switch (event.type) {
      case 'update':
        return this._handleUpdate(event);
      case 'error':
        return this._handleError(event);
      case 'complete':
        return this._handleComplete(event);
      default:
        getLogger(this._database).warn(
          `Unknown transaction event type: '${event.type}'`,
          event
        );
        return undefined;
    }
  }
 
  /**
   *
   * @param event
   * @private
   */
  _handleUpdate(event: Object = {}) {
    let newValue;
    const { id, value } = event;
 
    try {
      const transaction = this._transactions[id];
      Iif (!transaction) return;
 
      newValue = transaction.transactionUpdater(value);
    } finally {
      let abort = false;
 
      if (newValue === undefined) {
        abort = true;
      }
 
      getNativeModule(this._database).transactionTryCommit(id, {
        value: newValue,
        abort,
      });
    }
  }
 
  /**
   *
   * @param event
   * @private
   */
  _handleError(event: Object = {}) {
    const transaction = this._transactions[event.id];
    if (transaction && !transaction.completed) {
      transaction.completed = true;
      try {
        transaction.onComplete(event.error, false, null);
      } finally {
        setImmediate(() => {
          delete this._transactions[event.id];
        });
      }
    }
  }
 
  /**
   *
   * @param event
   * @private
   */
  _handleComplete(event: Object = {}) {
    const transaction = this._transactions[event.id];
    Eif (transaction && !transaction.completed) {
      transaction.completed = true;
      try {
        transaction.onComplete(
          null,
          event.committed,
          Object.assign({}, event.snapshot)
        );
      } finally {
        setImmediate(() => {
          delete this._transactions[event.id];
        });
      }
    }
  }
}