2017-03-02 13:10:10 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
|
2017-05-30 10:44:06 +00:00
|
|
|
import EventEmitter from 'react-native/Libraries/EventEmitter/EventEmitter';
|
2017-03-02 13:10:10 +00:00
|
|
|
import Log from '../utils/log';
|
|
|
|
|
|
|
|
const logs = {};
|
2017-05-26 16:55:22 +00:00
|
|
|
const SharedEventEmitter = new EventEmitter();
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-05-26 16:55:22 +00:00
|
|
|
export class Base {
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-03-10 18:12:46 +00:00
|
|
|
/**
|
|
|
|
* Return a namespaced instance of Log
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-03-02 13:10:10 +00:00
|
|
|
get log(): Log {
|
2017-03-10 18:12:46 +00:00
|
|
|
if (logs[this.namespace]) return logs[this.namespace];
|
2017-05-26 16:55:22 +00:00
|
|
|
|
|
|
|
// todo grab log level from global config provider (still todo);
|
|
|
|
return logs[this.namespace] = new Log(this.namespace, '*');
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 16:55:22 +00:00
|
|
|
/*
|
|
|
|
* Proxy functions to shared event emitter instance
|
|
|
|
* https://github.com/facebook/react-native/blob/master/Libraries/EventEmitter/EventEmitter.js
|
|
|
|
*/
|
|
|
|
|
|
|
|
get sharedEventEmitter () {
|
|
|
|
return SharedEventEmitter;
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 16:55:22 +00:00
|
|
|
get addListener() {
|
|
|
|
return SharedEventEmitter.addListener.bind(SharedEventEmitter);
|
|
|
|
}
|
2017-03-02 13:10:10 +00:00
|
|
|
|
2017-05-26 16:55:22 +00:00
|
|
|
get on() {
|
|
|
|
return SharedEventEmitter.addListener.bind(SharedEventEmitter);
|
|
|
|
}
|
2017-03-10 18:12:46 +00:00
|
|
|
|
2017-05-26 16:55:22 +00:00
|
|
|
get emit() {
|
|
|
|
return SharedEventEmitter.emit.bind(SharedEventEmitter);
|
|
|
|
}
|
2017-03-10 18:12:46 +00:00
|
|
|
|
2017-05-26 16:55:22 +00:00
|
|
|
get listeners() {
|
|
|
|
return SharedEventEmitter.listeners.bind(SharedEventEmitter);
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 16:55:22 +00:00
|
|
|
hasListeners(eventType: string): Boolean {
|
|
|
|
const subscriptions = SharedEventEmitter._subscriber.getSubscriptionsForType(eventType);
|
|
|
|
return subscriptions && subscriptions.length;
|
|
|
|
}
|
2017-03-10 18:12:46 +00:00
|
|
|
|
2017-05-26 16:55:22 +00:00
|
|
|
get removeListener() {
|
|
|
|
return SharedEventEmitter.removeListener.bind(SharedEventEmitter);
|
|
|
|
}
|
|
|
|
|
|
|
|
get removeAllListeners() {
|
|
|
|
return SharedEventEmitter.removeAllListeners.bind(SharedEventEmitter);
|
2017-03-02 13:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ReferenceBase extends Base {
|
2017-05-26 16:55:22 +00:00
|
|
|
constructor(path: string) {
|
|
|
|
super();
|
2017-03-02 13:10:10 +00:00
|
|
|
this.path = path || '/';
|
|
|
|
}
|
|
|
|
|
2017-05-06 13:33:55 +00:00
|
|
|
/**
|
|
|
|
* The last part of a Reference's path (after the last '/')
|
|
|
|
* The key of a root Reference is null.
|
|
|
|
* @type {String}
|
|
|
|
* {@link https://firebase.google.com/docs/reference/js/firebase.database.Reference#key}
|
|
|
|
*/
|
2017-03-02 13:10:10 +00:00
|
|
|
get key(): string|null {
|
|
|
|
return this.path === '/' ? null : this.path.substring(this.path.lastIndexOf('/') + 1);
|
|
|
|
}
|
|
|
|
}
|