Skip to content

Commit 018c0b5

Browse files
authored
Merge pull request #156 from devshackio/various-fixes
Various fixes
2 parents f034c1f + 74b27aa commit 018c0b5

File tree

9 files changed

+62
-58
lines changed

9 files changed

+62
-58
lines changed

android/src/main/java/io/fullstack/firestack/FirestackStorage.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,10 @@ public void downloadFile(final String urlStr,
113113
@Override
114114
public void doInBackground(StreamDownloadTask.TaskSnapshot taskSnapshot, InputStream inputStream) throws IOException {
115115
int indexOfLastSlash = localFile.lastIndexOf("/");
116-
String pathMinusFileName = localFile.substring(0, indexOfLastSlash) + "/";
117-
String filename = localFile.substring(indexOfLastSlash+1);
116+
String pathMinusFileName = indexOfLastSlash>0 ? localFile.substring(0, indexOfLastSlash) + "/" : "/";
117+
String filename = indexOfLastSlash>0 ? localFile.substring(indexOfLastSlash+1) : localFile;
118118
File fileWithJustPath = new File(pathMinusFileName);
119-
if (!fileWithJustPath.mkdirs()) {
120-
Log.e(TAG, "Directory not created");
121-
WritableMap error = Arguments.createMap();
122-
error.putString("message", "Directory not created");
123-
callback.invoke(error);
124-
return;
125-
}
119+
fileWithJustPath.mkdirs();
126120
File fileWithFullPath = new File(pathMinusFileName, filename);
127121
FileOutputStream output = new FileOutputStream(fileWithFullPath);
128122
int bufferSize = 1024;

android/src/main/java/io/fullstack/firestack/FirestackUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void todoNote(final String tag, final String name, final Callback
4040
public static void sendEvent(final ReactContext context,
4141
final String eventName,
4242
final WritableMap params) {
43-
if (context.hasActiveCatalystInstance()) {
43+
if (context != null && context.hasActiveCatalystInstance()) {
4444
context
4545
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
4646
.emit(eventName, params);

firestack.android.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
2-
* @providesModule Firestack
32
* @flow
43
*/
54
import Firestack from './lib/firestack'
65

7-
export default Firestack
6+
export default Firestack

firestack.ios.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/**
2-
* @providesModule Firestack
32
* @flow
43
*/
54
import Firestack from './lib/firestack'

lib/firestack.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class Firestack extends Singleton {
3434
* @param options
3535
* @param name - TODO support naming multiple instances
3636
*/
37-
constructor(options: Object, name: string) {
37+
constructor(options: Object, name?: string) {
3838
const instance = super(options);
3939

4040
instance.options = options || {};
@@ -192,7 +192,7 @@ export default class Firestack extends Singleton {
192192
/**
193193
* Redux store
194194
**/
195-
get store(): Object {
195+
get store(): ?Object {
196196
return this._store;
197197
}
198198

lib/modules/auth.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ export default class Auth extends Base {
138138
* Sign the user in with a third-party authentication provider
139139
* @return {Promise} A promise resolved upon completion
140140
*/
141-
signInWithCredential(credential): Promise<Object> {
141+
signInWithCredential(credential: Object): Promise<Object> {
142142
return promisify('signInWithProvider', FirestackAuth)(credential.provider, credential.token, credential.secret);
143143
}
144144

145145
/**
146146
* Re-authenticate a user with a third-party authentication provider
147147
* @return {Promise} A promise resolved upon completion
148148
*/
149-
reauthenticateUser(credential): Promise<Object> {
149+
reauthenticateUser(credential: Object): Promise<Object> {
150150
return promisify('reauthenticateWithCredentialForProvider', FirestackAuth)(credential.provider, credential.token, credential.secret);
151151
}
152152

lib/modules/base.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ const FirestackModuleEvt = new NativeEventEmitter(FirestackModule);
1111

1212
const logs = {};
1313

14+
type FirestackOptions = {};
1415
export class Base extends EventEmitter {
15-
constructor(firestack, options = {}) {
16+
constructor(firestack: Object, options: FirestackOptions = {}) {
1617
super();
1718
this.firestack = firestack;
1819
this.eventHandlers = {};
@@ -22,7 +23,7 @@ export class Base extends EventEmitter {
2223
}
2324

2425
// Logger
25-
get log() {
26+
get log(): Log {
2627
if (!logs[this.namespace]) {
2728
const debug = this.firestack._debug;
2829
logs[this.namespace] = new Log(this.namespace, debug);
@@ -38,7 +39,7 @@ export class Base extends EventEmitter {
3839
}
3940

4041
// TODO unused - do we need this anymore?
41-
_addToFirestackInstance(...methods) {
42+
_addToFirestackInstance(...methods: Array<string>) {
4243
methods.forEach(name => {
4344
this.firestack[name] = this[name].bind(this);
4445
})
@@ -47,15 +48,17 @@ export class Base extends EventEmitter {
4748
/**
4849
* app instance
4950
**/
50-
get app() {
51+
get app(): Object {
5152
return this.firestack.app;
5253
}
5354

54-
whenReady(fn) {
55-
return this.firestack.configurePromise.then(fn);
55+
whenReady(promise: Promise<*>): Promise<*> {
56+
return this.firestack.configurePromise.then((result) => {
57+
return promise;
58+
});
5659
}
5760

58-
get namespace() {
61+
get namespace(): string {
5962
return 'firestack:base';
6063
}
6164

@@ -88,25 +91,21 @@ export class Base extends EventEmitter {
8891
}
8992

9093
export class ReferenceBase extends Base {
91-
constructor(firestack, path) {
94+
constructor(firestack: Object, path: Array<string> | string) {
9295
super(firestack);
9396

94-
this.path = Array.isArray(path) ?
95-
path :
96-
(typeof path == 'string' ?
97-
[path] : []);
97+
this.path = Array.isArray(path) ? path : (typeof path == 'string' ? [path] : []);
9898

9999
// sanitize path, just in case
100-
this.path = this.path
101-
.filter(str => str !== "");
100+
this.path = this.path.filter(str => str !== '');
102101
}
103102

104-
get key() {
103+
get key(): string {
105104
const path = this.path;
106105
return path.length === 0 ? '/' : path[path.length - 1];
107106
}
108107

109-
pathToString() {
108+
pathToString(): string {
110109
let path = this.path;
111110
let pathStr = (path.length > 0 ? path.join('/') : '/');
112111
if (pathStr[0] != '/') {

lib/modules/storage.js

+37-24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* @flow */
12

23
import {NativeModules, NativeEventEmitter} from 'react-native';
34
const FirestackStorage = NativeModules.FirestackStorage;
@@ -13,17 +14,22 @@ class StorageRef extends ReferenceBase {
1314
this.storage = storage;
1415
}
1516

16-
downloadUrl() {
17+
downloadUrl(): Promise<Object> {
1718
const path = this.pathToString();
18-
return promisify('downloadUrl', FirestackStorage)(this.storage.storageUrl, path);
19+
this.log.debug('downloadUrl(', path, ')');
20+
return promisify('downloadUrl', FirestackStorage)(this.storage.storageUrl, path)
21+
.catch(err => {
22+
this.log.error('Error downloading URL for ', path, '. Error: ', err);
23+
throw err;
24+
});
1925
}
2026

2127
/**
2228
* Downloads a reference to the device
2329
* @param {String} downloadPath Where to store the file
2430
* @return {Promise}
2531
*/
26-
download (downloadPath, cb) {
32+
download (downloadPath: string, cb: (evt: Object) => Object): Promise<Object> {
2733
let callback = cb;
2834
if (!callback || typeof callback !== 'function') {
2935
callback = (evt) => {};
@@ -35,20 +41,25 @@ class StorageRef extends ReferenceBase {
3541
listeners.push(this.storage._addListener('download_resumed', callback));
3642

3743
const path = this.pathToString();
44+
this.log.debug('download(', path, ') -> ', downloadPath);
3845
return promisify('downloadFile', FirestackStorage)(this.storage.storageUrl, path, downloadPath)
3946
.then((res) => {
40-
console.log('res --->', res);
41-
listeners.forEach(this.storage._removeListener);
47+
this.log.debug('res --->', res);
48+
listeners.forEach(listener => listener.remove());
4249
return res;
4350
})
4451
.catch(err => {
45-
console.log('Got an error ->', err);
46-
})
52+
this.log.error('Error downloading ', path, ' to ', downloadPath, '. Error: ', err);
53+
throw err;
54+
});
4755
}
4856
}
4957

58+
type StorageOptionsType = {
59+
storageBucket?: ?string,
60+
};
5061
export default class Storage extends Base {
51-
constructor(firestack, options={}) {
62+
constructor(firestack: Object, options:StorageOptionsType={}) {
5263
super(firestack, options);
5364

5465
if (this.options.storageBucket) {
@@ -58,8 +69,8 @@ export default class Storage extends Base {
5869
this.refs = {};
5970
}
6071

61-
ref(...path) {
62-
const key = this._pathKey(path);
72+
ref(...path: Array<string>): StorageRef {
73+
const key = this._pathKey(...path);
6374
if (!this.refs[key]) {
6475
const ref = new StorageRef(this, path);
6576
this.refs[key] = ref;
@@ -74,43 +85,45 @@ export default class Storage extends Base {
7485
* @param {object} metadata An object containing metadata
7586
* @return {Promise}
7687
*/
77-
uploadFile(name, filepath, metadata={}, cb) {
88+
uploadFile(name: string, filepath: string, metadata: Object={}, cb: (evt: Object) => Object): Promise<Object> {
89+
this.log.debug('uploadFile(', filepath, ') -> ', name);
7890
let callback = cb;
7991
if (!callback || typeof callback !== 'function') {
80-
callback = (evt) => {}
92+
callback = (evt: Object) => ({});
8193
}
8294

83-
filepath = filepath.replace("file://", "");
95+
filepath = filepath.replace('file://', '');
8496

8597
const listeners = [];
8698
listeners.push(this._addListener('upload_progress', callback));
8799
listeners.push(this._addListener('upload_paused', callback));
88100
listeners.push(this._addListener('upload_resumed', callback));
89101
return promisify('uploadFile', FirestackStorage)(this.storageUrl, name, filepath, metadata)
90102
.then((res) => {
91-
listeners.forEach(this._removeListener);
103+
listeners.forEach(listener => listener.remove());
92104
return res;
105+
})
106+
.catch(err => {
107+
this.log.error('Error uploading file ', name, ' to ', filepath, '. Error: ', err);
108+
throw err;
93109
});
94110
}
95111

96-
getRealPathFromURI(uri) {
112+
getRealPathFromURI(uri: string): Promise<string> {
97113
return promisify('getRealPathFromURI', FirestackStorage)(uri);
98114
}
99115

100-
_addListener(evt, cb) {
101-
return FirestackStorageEvt.addListener(evt, cb);
102-
}
103-
104-
_removeListener(evt) {
105-
return FirestackStorageEvt.removeListener(evt);
116+
_addListener(evt: string, cb: (evt: Object) => Object): {remove: () => void} {
117+
let listener = FirestackStorageEvt.addListener(evt, cb);
118+
return listener;
106119
}
107120

108-
setStorageUrl(url) {
121+
setStorageUrl(url: string): void {
109122
// return promisify('setStorageUrl', FirestackStorage)(url);
110123
this.storageUrl = `gs://${url}`;
111124
}
112125

113-
_pathKey(...path) {
126+
_pathKey(...path: Array<string>): string {
114127
return path.join('-');
115128
}
116129

@@ -126,7 +139,7 @@ export default class Storage extends Base {
126139
'FILETYPE_DIRECTORY': FirestackStorage.FILETYPE_DIRECTORY
127140
};
128141

129-
get namespace() {
142+
get namespace(): string {
130143
return 'firestack:storage'
131144
}
132145
}

lib/utils/window-or-global.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
// https://github.com/purposeindustries/window-or-global
33
module.exports = (typeof self === 'object' && self.self === self && self) ||
44
(typeof global === 'object' && global.global === global && global) ||
5-
{}
5+
this

0 commit comments

Comments
 (0)