Skip to content

Commit 7b18309

Browse files
committed
cleanup utils
1 parent b569ff7 commit 7b18309

File tree

10 files changed

+63
-49
lines changed

10 files changed

+63
-49
lines changed

lib/firestack.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
* @providesModule Firestack
33
* @flow
44
*/
5-
6-
75
import { NativeModules, NativeEventEmitter } from 'react-native';
86

97
import Log from './utils/log';
10-
import promisify from './utils/promisify';
8+
import { promisify } from './utils';
119
import Singleton from './utils/singleton';
1210

1311
// modules
@@ -32,9 +30,8 @@ export default class Firestack extends Singleton {
3230
/**
3331
*
3432
* @param options
35-
* @param name - TODO support naming multiple instances
3633
*/
37-
constructor(options: Object, name: string) {
34+
constructor(options: Object) {
3835
const instance = super(options);
3936

4037
instance.options = options || {};

lib/modules/auth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @flow
22
import { NativeModules, NativeEventEmitter } from 'react-native';
33

4+
import User from './user';
45
import { Base } from './base';
5-
import { default as User } from './user';
6-
import promisify from '../utils/promisify';
6+
import { promisify } from '../utils';
77

88
const FirestackAuth = NativeModules.FirestackAuth;
99
const FirestackAuthEvt = new NativeEventEmitter(FirestackAuth);

lib/modules/base.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* @flow
33
*/
4-
import { NativeModules, NativeEventEmitter, AsyncStorage } from 'react-native';
4+
import { NativeModules, NativeEventEmitter } from 'react-native';
55

6-
import Log from '../utils/log'
6+
import Log from '../utils/log';
77
import EventEmitter from './../utils/eventEmitter';
88

99
const FirestackModule = NativeModules.Firestack;
@@ -32,7 +32,7 @@ export class Base extends EventEmitter {
3232

3333
// TODO unused - do we need this anymore?
3434
_addConstantExports(constants) {
35-
Object.keys(constants).forEach(name => {
35+
Object.keys(constants).forEach((name) => {
3636
FirestackModule[name] = constants[name];
3737
});
3838
}

lib/modules/database/disconnect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import { NativeModules } from 'react-native';
4-
import promisify from './../../utils/promisify';
4+
import { promisify } from './../../utils';
55
import Reference from './reference';
66

77
const FirestackDatabase = NativeModules.FirestackDatabase;

lib/modules/database/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { NativeModules, NativeEventEmitter } from 'react-native';
77
const FirestackDatabase = NativeModules.FirestackDatabase;
88
const FirestackDatabaseEvt = new NativeEventEmitter(FirestackDatabase);
99

10-
import promisify from './../../utils/promisify';
10+
import { promisify } from './../../utils';
1111

1212
import { Base } from './../base';
1313
import Reference from './reference.js';

lib/modules/database/reference.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @flow
33
*/
44
import { NativeModules } from 'react-native';
5-
import promisify from './../../utils/promisify';
5+
import { promisify } from './../../utils';
66
import { ReferenceBase } from './../base';
77
import Snapshot from './snapshot.js';
88
import Disconnect from './disconnect.js';

lib/modules/messaging.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NativeModules, NativeEventEmitter } from 'react-native';
22
import { Base } from './base';
3-
import promisify from '../utils/promisify';
3+
import { promisify } from '../utils';
44

55
const FirestackCloudMessaging = NativeModules.FirestackCloudMessaging;
66
const FirestackCloudMessagingEvt = new NativeEventEmitter(FirestackCloudMessaging);

lib/modules/presence.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import invariant from 'invariant'
2-
import promisify from '../utils/promisify'
3-
import { Base, ReferenceBase } from './base'
1+
import invariant from 'invariant';
2+
import { promisify } from '../utils';
3+
import { Base, ReferenceBase } from './base';
44

55
class PresenceRef extends ReferenceBase {
66
constructor(presence, ref, pathParts) {
@@ -18,33 +18,33 @@ class PresenceRef extends ReferenceBase {
1818
}
1919

2020
setOnline() {
21-
this.ref.setAt({online: true})
21+
this.ref.setAt({ online: true });
2222
this._connectedRef.on('value', (snapshot) => {
2323
const val = snapshot.val();
2424
if (val) {
2525
// add self to connection list
2626
// this.ref.push()
2727
this.ref.setAt({
28-
online: true
29-
})
30-
.then(() => {
31-
this._disconnect();
32-
33-
this._onConnect.forEach(fn => {
34-
if (fn && typeof fn === 'function') {
35-
fn.bind(this)(this.ref);
36-
}
37-
})
28+
online: true,
3829
})
30+
.then(() => {
31+
this._disconnect();
32+
33+
this._onConnect.forEach((fn) => {
34+
if (fn && typeof fn === 'function') {
35+
fn.bind(this)(this.ref);
36+
}
37+
});
38+
});
3939
}
4040
});
4141
return this;
4242
}
4343

4444
setOffline() {
4545
if (this.ref) {
46-
this.ref.setAt({online: false})
47-
.then(() => this.ref.off('value'))
46+
this.ref.setAt({ online: false })
47+
.then(() => this.ref.off('value'));
4848
this.presence.off(this._pathParts);
4949
}
5050
return this;
@@ -53,10 +53,10 @@ class PresenceRef extends ReferenceBase {
5353
_disconnect() {
5454
if (this.ref) {
5555
this.ref.onDisconnect()
56-
.setValue({online: false});
56+
.setValue({ online: false });
5757
// set last online time
5858
this.lastOnlineRef.onDisconnect()
59-
.setValue(this.firestack.ServerValue.TIMESTAMP)
59+
.setValue(this.firestack.ServerValue.TIMESTAMP);
6060
}
6161
}
6262

@@ -72,7 +72,7 @@ class PresenceRef extends ReferenceBase {
7272
}
7373

7474
export default class Presence extends Base {
75-
constructor(firestack, options={}) {
75+
constructor(firestack, options = {}) {
7676
super(firestack, options);
7777

7878
this.instances = {};
@@ -85,7 +85,7 @@ export default class Presence extends Base {
8585
const pathKey = this._presenceKey(path);
8686
if (!this.instances[pathKey]) {
8787
const _ref = this.firestack.database.ref(pathKey);
88-
this.log.debug('Created new presence object for ', pathKey)
88+
this.log.debug('Created new presence object for ', pathKey);
8989
const inst = new PresenceRef(this, _ref, path);
9090

9191
this.instances[pathKey] = inst;
@@ -110,6 +110,6 @@ export default class Presence extends Base {
110110
}
111111

112112
get namespace() {
113-
return 'firestack:presence'
113+
return 'firestack:presence';
114114
}
115115
}

lib/utils/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,34 @@ export function reverseKeyValues(object) {
1010
}
1111
return output;
1212
}
13+
14+
/**
15+
* No operation func
16+
*/
17+
export function noop() {
18+
}
19+
20+
21+
// internal promise handler
22+
const _handler = (resolve, reject, err, resp) => {
23+
// resolve / reject after events etc
24+
setImmediate(() => {
25+
if (err) return reject(err);
26+
return resolve(resp);
27+
});
28+
};
29+
30+
/**
31+
* Wraps a native module method to support promises.
32+
* @param fn
33+
* @param NativeModule
34+
*/
35+
export function promisify(fn, NativeModule) {
36+
return (...args) => {
37+
return new Promise((resolve, reject) => {
38+
const _fn = typeof fn === 'function' ? fn : NativeModule[fn];
39+
if (!_fn || typeof _fn !== 'function') return reject(new Error('Missing function for promisify.'));
40+
return _fn.apply(NativeModule, [...args, _handler.bind(_handler, resolve, reject)]);
41+
});
42+
};
43+
}

lib/utils/promisify.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)