All files / src Database.js

100% Statements 48/48
90% Branches 18/20
100% Functions 15/15
100% Lines 48/48
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              1x 1x 1x                                     24x       2x       1x 1x       1x 1x       1x                         2x       2x       1x       8x 8x 32x 32x     8x       4x   4x 3x     4x       4x   4x 1x   3x     4x 3x     4x       4x   4x 3x     4x       4x   4x 1x   3x     4x 3x     4x       1x 1x 8x 8x 8x   1x 7x 7x      
/**
 * @providesModule react-native-bridge-firebase/Database
 */
import forEach from 'lodash/forEach';
import Events from 'EventEmitter';
import RNBridgeFirebase from './RNBridgeFirebase';
 
const { EventEmitter, EVENT, ERROR, DATA_CHILD } = RNBridgeFirebase;
export const childHandler = new Events();
export const valueHandler = new Events();
 
export default class Database {
 
  static EVENT = EVENT;
 
  static ERROR = ERROR;
 
  /**
   * CHILD_ADDED
   * CHILD_REMOVED
   * CHILD_CHANGED
   * CHILD_MOVED
   */
  static DATA_CHILD = DATA_CHILD;
 
  path = '';
 
  constructor(path: String) {
    this.path = /^\/.+/gi.test(path) ? path : `/${path}`;
  }
 
  child(path: String) {
    return new Database(`${this.path}/${path}`);
  }
 
  getKey() {
    const regex = /\/?([^/]+)$/.exec(this.path);
    return regex ? regex[1] : '';
  }
 
  async addChild() {
    const key = await RNBridgeFirebase.addDataChild(this.path);
    return this.child(key);
  }
 
  async setValue(value) {
    await RNBridgeFirebase.setDataValue(this.path, { value });
  }
 
  /**
   *  reply = {
   *    key: String,
   *    hasChildren: Boolean,
   *    exists: Boolean,
   *    childrenCount: Integer,
   *    priority: String,
   *  }
   */
  async getValue(path: String) {
    const reply = await RNBridgeFirebase.getDataValue(
      path ? `${this.path}/${path}` : this.path
    );
 
    return reply.value;
  }
 
  async removeValue() {
    return await RNBridgeFirebase.removeDataValue(this.path);
  }
 
  static hasChildListener(path: String) {
    let total = 0;
    forEach(DATA_CHILD, (type) => {
      const eventName = `${path}::${type}`;
      total += childHandler.listeners(eventName).length;
    });
 
    return !!total;
  }
 
  async addValueListener(handler: Function) {
    const eventName = `${this.path}`;
 
    if (valueHandler.listeners(eventName).length < 1) {
      await RNBridgeFirebase.addDataValueListener(this.path);
    }
 
    return valueHandler.addListener(eventName, handler);
  }
 
  async removeValueListener(handler: Function) {
    const eventName = this.path;
 
    if (handler === undefined) {
      valueHandler.removeAllListeners(eventName);
    } else {
      valueHandler.removeListener(eventName, handler);
    }
 
    if (valueHandler.listeners(eventName).length < 1) {
      await RNBridgeFirebase.removeDataValueListener(this.path);
    }
 
    return true;
  }
 
  async addChildListener(type: Number, handler: Function) {
    const eventName = `${this.path}::${type}`;
 
    if (!Database.hasChildListener(this.path)) {
      await RNBridgeFirebase.addDataChildListener(this.path);
    }
 
    return childHandler.addListener(eventName, handler);
  }
 
  async removeChildListener(type: Number, handler: Function) {
    const eventName = `${this.path}::${type}`;
 
    if (handler === undefined) {
      childHandler.removeAllListeners(eventName);
    } else {
      childHandler.removeListener(eventName, handler);
    }
 
    if (!Database.hasChildListener(this.path)) {
      await RNBridgeFirebase.removeDataChildListener(this.path);
    }
 
    return true;
  }
}
 
Eif (EventEmitter) {
  EventEmitter.addListener(EVENT.DATA_CHILD_CHANGED, (data) => {
    const { key, value, path, type } = data;
    const eventName = `${path}::${type}`;
    childHandler.emit(eventName, { key, value });
  });
  EventEmitter.addListener(EVENT.DATA_VALUE_CHANGED, (data) => {
    const eventName = data.path;
    valueHandler.emit(eventName, data.value);
  });
}