Skip to content

Commit 6aedf85

Browse files
authored
Merge pull request #7 from devshackio/v3
Various fixes
2 parents 31c9ccf + 16fd792 commit 6aedf85

File tree

8 files changed

+69
-66
lines changed

8 files changed

+69
-66
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void todoNote(final String tag, final String name, final Callback
3838
* send a JS event
3939
**/
4040
public static void sendEvent(final ReactContext context, final String eventName, final WritableMap params) {
41-
if (context.hasActiveCatalystInstance()) {
41+
if (context != null && context.hasActiveCatalystInstance()) {
4242
context
4343
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
4444
.emit(eventName, params);

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,10 @@ public void downloadFile(final String urlStr,
111111
@Override
112112
public void doInBackground(StreamDownloadTask.TaskSnapshot taskSnapshot, InputStream inputStream) throws IOException {
113113
int indexOfLastSlash = localFile.lastIndexOf("/");
114-
String pathMinusFileName = localFile.substring(0, indexOfLastSlash) + "/";
115-
String filename = localFile.substring(indexOfLastSlash + 1);
114+
String pathMinusFileName = indexOfLastSlash>0 ? localFile.substring(0, indexOfLastSlash) + "/" : "/";
115+
String filename = indexOfLastSlash>0 ? localFile.substring(indexOfLastSlash+1) : localFile;
116116
File fileWithJustPath = new File(pathMinusFileName);
117-
if (!fileWithJustPath.mkdirs()) {
118-
Log.e(TAG, "Directory not created");
119-
WritableMap error = Arguments.createMap();
120-
error.putString("message", "Directory not created");
121-
callback.invoke(error);
122-
return;
123-
}
117+
fileWithJustPath.mkdirs();
124118
File fileWithFullPath = new File(pathMinusFileName, filename);
125119
FileOutputStream output = new FileOutputStream(fileWithFullPath);
126120
int bufferSize = 1024;
@@ -162,17 +156,17 @@ public void onSuccess(final StorageMetadata storageMetadata) {
162156
callback.invoke(null, data);
163157
}
164158
})
165-
.addOnFailureListener(new OnFailureListener() {
166-
@Override
167-
public void onFailure(@NonNull Exception exception) {
168-
final int errorCode = 1;
169-
WritableMap data = Arguments.createMap();
170-
StorageException storageException = StorageException.fromException(exception);
171-
data.putString("description", storageException.getMessage());
172-
data.putInt("code", errorCode);
173-
callback.invoke(makeErrorPayload(errorCode, exception));
174-
}
175-
});
159+
.addOnFailureListener(new OnFailureListener() {
160+
@Override
161+
public void onFailure(@NonNull Exception exception) {
162+
final int errorCode = 1;
163+
WritableMap data = Arguments.createMap();
164+
StorageException storageException = StorageException.fromException(exception);
165+
data.putString("description", storageException.getMessage());
166+
data.putInt("code", errorCode);
167+
callback.invoke(makeErrorPayload(errorCode, exception));
168+
}
169+
});
176170
}
177171
}).addOnFailureListener(new OnFailureListener() {
178172
@Override

firestack.android.js

Lines changed: 1 addition & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export default class Firestack extends Singleton {
209209
/**
210210
* Redux store
211211
**/
212-
get store(): Object {
212+
get store(): ?Object {
213213
return this._store;
214214
}
215215

lib/modules/base.js

Lines changed: 15 additions & 16 deletions
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

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* @flow */
12
import { NativeModules, NativeEventEmitter } from 'react-native';
23

34
import { promisify, noop } from '../utils';
@@ -13,9 +14,14 @@ 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
/**
@@ -24,8 +30,9 @@ class StorageRef extends ReferenceBase {
2430
* @param listener
2531
* @return {Promise}
2632
*/
27-
download(downloadPath: string, listener: Function = noop) {
33+
download(downloadPath: string, listener: Function = noop): Promise<Object> {
2834
const path = this.pathToString();
35+
this.log.debug('download(', path, ') -> ', downloadPath);
2936
const listeners = [
3037
this.storage._addListener('download_progress', listener),
3138
this.storage._addListener('download_paused', listener),
@@ -34,19 +41,22 @@ class StorageRef extends ReferenceBase {
3441

3542
return promisify('downloadFile', FirestackStorage)(this.storage.storageUrl, path, downloadPath)
3643
.then((res) => {
37-
console.log('res --->', res);
38-
listeners.forEach(this.storage._removeListener);
44+
this.log.debug('res --->', res);
45+
listeners.forEach(listener => listener.remove());
3946
return res;
4047
})
41-
.catch((downloadError) => {
42-
console.log('Got an error ->', downloadError);
43-
return Promise.reject(downloadError);
48+
.catch(err => {
49+
this.log.error('Error downloading ', path, ' to ', downloadPath, '. Error: ', err);
50+
throw err;
4451
});
4552
}
4653
}
4754

55+
type StorageOptionsType = {
56+
storageBucket?: ?string,
57+
};
4858
export default class Storage extends Base {
49-
constructor(firestack, options = {}) {
59+
constructor(firestack: Object, options:StorageOptionsType={}) {
5060
super(firestack, options);
5161

5262
if (this.options.storageBucket) {
@@ -56,8 +66,8 @@ export default class Storage extends Base {
5666
this.refs = {};
5767
}
5868

59-
ref(...path) {
60-
const key = this._pathKey(path);
69+
ref(...path: Array<string>): StorageRef {
70+
const key = this._pathKey(...path);
6171
if (!this.refs[key]) {
6272
const ref = new StorageRef(this, path);
6373
this.refs[key] = ref;
@@ -73,8 +83,9 @@ export default class Storage extends Base {
7383
* @param listener
7484
* @return {Promise}
7585
*/
76-
uploadFile(name: string, filePath: string, metadata: Object = {}, listener: Function = noop) {
86+
uploadFile(name: string, filePath: string, metadata: Object = {}, listener: Function = noop): Promise<Object> {
7787
const _filePath = filePath.replace('file://', '');
88+
this.log.debug('uploadFile(', _filepath, ') -> ', name);
7889
const listeners = [
7990
this._addListener('upload_paused', listener),
8091
this._addListener('upload_resumed', listener),
@@ -83,29 +94,30 @@ export default class Storage extends Base {
8394

8495
return promisify('uploadFile', FirestackStorage)(this.storageUrl, name, _filePath, metadata)
8596
.then((res) => {
86-
listeners.forEach(this._removeListener);
97+
listeners.forEach(listener => listener.remove());
8798
return res;
99+
})
100+
.catch(err => {
101+
this.log.error('Error uploading file ', name, ' to ', filepath, '. Error: ', err);
102+
throw err;
88103
});
89104
}
90105

91-
getRealPathFromURI(uri) {
106+
getRealPathFromURI(uri: string): Promise<string> {
92107
return promisify('getRealPathFromURI', FirestackStorage)(uri);
93108
}
94109

95-
_addListener(evt, cb) {
96-
return FirestackStorageEvt.addListener(evt, cb);
97-
}
98-
99-
_removeListener(evt) {
100-
return FirestackStorageEvt.removeListener(evt);
110+
_addListener(evt: string, cb: (evt: Object) => Object): {remove: () => void} {
111+
let listener = FirestackStorageEvt.addListener(evt, cb);
112+
return listener;
101113
}
102114

103-
setStorageUrl(url) {
115+
setStorageUrl(url: string): void {
104116
// return promisify('setStorageUrl', FirestackStorage)(url);
105117
this.storageUrl = `gs://${url}`;
106118
}
107119

108-
_pathKey(...path) {
120+
_pathKey(...path: Array<string>): string {
109121
return path.join('-');
110122
}
111123

@@ -121,8 +133,8 @@ export default class Storage extends Base {
121133
FILETYPE_DIRECTORY: FirestackStorage.FILETYPE_DIRECTORY,
122134
};
123135

124-
get namespace() {
125-
return 'firestack:storage';
136+
get namespace(): string {
137+
return 'firestack:storage'
126138
}
127139
}
128140

lib/utils/window-or-global.js

Lines changed: 1 addition & 1 deletion
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-
this
5+
this;

0 commit comments

Comments
 (0)