Skip to content

Commit

Permalink
[internals][types] Fix a couple of minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbianca committed Jan 5, 2018
1 parent 80cb54e commit d1f2b3f
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 27 deletions.
4 changes: 0 additions & 4 deletions lib/modules/admob/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ export default class AdMob extends ModuleBase {
rewarded(adUnit: string): RewardedVideo {
return new RewardedVideo(this, adUnit);
}

get namespace(): string {
return 'firebase:admob';
}
}

export const statics = {
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/auth/User.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
getNativeModule(this._auth)/**
/**
* @flow
* User representation wrapper
*/
Expand Down
4 changes: 0 additions & 4 deletions lib/modules/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,6 @@ export default class Auth extends ModuleBase {
return this._user;
}

get namespace(): string {
return 'firebase:auth';
}

/**
* KNOWN UNSUPPORTED METHODS
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/core/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import type {

const FirebaseCoreModule = NativeModules.RNFirebase;

class FirebaseCore {
class Firebase {
admob: AdMobModule;
analytics: AnalyticsModule;
auth: AuthModule;
Expand Down Expand Up @@ -114,4 +114,4 @@ class FirebaseCore {
}
}

export default new FirebaseCore();
export default new Firebase();
11 changes: 6 additions & 5 deletions lib/modules/database/disconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* @flow
* Disconnect representation wrapper
*/
import { typeOf } from './../../utils';
import { typeOf } from '../../utils';
import { getNativeModule } from '../../utils/native';
import type Database from './';
import type Reference from './reference';

Expand Down Expand Up @@ -32,7 +33,7 @@ export default class Disconnect {
* @returns {*}
*/
set(value: string | Object): Promise<void> {
return this._database._native.onDisconnectSet(this.path, { type: typeOf(value), value });
return getNativeModule(this._database).onDisconnectSet(this.path, { type: typeOf(value), value });
}

/**
Expand All @@ -41,22 +42,22 @@ export default class Disconnect {
* @returns {*}
*/
update(values: Object): Promise<void> {
return this._database._native.onDisconnectUpdate(this.path, values);
return getNativeModule(this._database).onDisconnectUpdate(this.path, values);
}

/**
* @url https://firebase.google.com/docs/reference/js/firebase.database.OnDisconnect#remove
* @returns {*}
*/
remove(): Promise<void> {
return this._database._native.onDisconnectRemove(this.path);
return getNativeModule(this._database).onDisconnectRemove(this.path);
}

/**
* @url https://firebase.google.com/docs/reference/js/firebase.database.OnDisconnect#cancel
* @returns {*}
*/
cancel(): Promise<void> {
return this._database._native.onDisconnectCancel(this.path);
return getNativeModule(this._database).onDisconnectCancel(this.path);
}
}
2 changes: 1 addition & 1 deletion lib/modules/database/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
getNativeModule(this)/**
/**
* @flow
* Database representation wrapper
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/ModuleBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { initialiseLogger } from './log';
import { initialiseNativeModule } from './native';

import type App from '../modules/core/firebase-app';
import type { FirebaseModuleConfig } from '../types';
import type { FirebaseModuleConfig, FirebaseNamespace } from '../types';

export default class ModuleBase {
_app: App;
namespace: FirebaseNamespace;

/**
*
Expand All @@ -24,6 +25,7 @@ export default class ModuleBase {
}
const { moduleName } = config;
this._app = app;
this.namespace = config.namespace;

// check if native module exists as all native
initialiseNativeModule(this, config);
Expand Down
14 changes: 10 additions & 4 deletions lib/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ import type ModuleBase from './ModuleBase';

// clean up time

const NATIVE_LOGGERS: { [ModuleBase]: Object } = {};
const NATIVE_LOGGERS: { [string]: Object } = {};

export const getLogger = (module: ModuleBase) => NATIVE_LOGGERS[module];
const getModuleKey = (module: ModuleBase): string => `${module.app.name}:${module.namespace}`;

export const getLogger = (module: ModuleBase) => {
const key = getModuleKey(module);
return NATIVE_LOGGERS[key];
};

export const initialiseLogger = (module: ModuleBase, logNamespace: string) => {
if (!NATIVE_LOGGERS[module]) {
NATIVE_LOGGERS[module] = require('bows')(`🔥 ${logNamespace.toUpperCase()}`);
const key = getModuleKey(module);
if (!NATIVE_LOGGERS[key]) {
NATIVE_LOGGERS[key] = require('bows')(`🔥 ${logNamespace.toUpperCase()}`);
}
};

Expand Down
14 changes: 9 additions & 5 deletions lib/utils/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MULTI_APP_MODULES = [
'RNFirebaseStorage',
];

const NATIVE_MODULES: { [ModuleBase]: Object } = {};
const NATIVE_MODULES: { [string]: Object } = {};

/**
* Prepends appName arg to all native method calls
Expand All @@ -37,13 +37,17 @@ const nativeWithApp = (appName: string, NativeModule: Object): Object => {
return native;
};

const getModuleKey = (module: ModuleBase): string => `${module.app.name}:${module.namespace}`;

export const getNativeModule = (module: ModuleBase): Object => {
return NATIVE_MODULES[module];
const key = getModuleKey(module);
return NATIVE_MODULES[key];
};

export const initialiseNativeModule = (module: ModuleBase, config: FirebaseModuleConfig): Object => {
const { moduleName, namespace } = config;
const nativeModule = NativeModules[moduleName];
const key = getModuleKey(module);

if (!nativeModule && namespace !== 'utils') {
throw new Error(INTERNALS.STRINGS.ERROR_MISSING_MODULE(namespace, moduleName));
Expand All @@ -52,12 +56,12 @@ export const initialiseNativeModule = (module: ModuleBase, config: FirebaseModul
// used by the modules that extend ModuleBase
// to access their native module counterpart
if (!MULTI_APP_MODULES.includes(moduleName)) {
NATIVE_MODULES[module] = nativeModule;
NATIVE_MODULES[key] = nativeModule;
} else {
NATIVE_MODULES[module] = nativeWithApp(module.app.name, nativeModule);
NATIVE_MODULES[key] = nativeWithApp(module.app.name, nativeModule);
}

initialiseNativeModuleEventEmitter(module, config);

return NATIVE_MODULES[module];
return NATIVE_MODULES[key];
};

0 comments on commit d1f2b3f

Please sign in to comment.