Skip to content

Commit a6a445a

Browse files
committed
Add flow to storage.js
1 parent 5cc041e commit a6a445a

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

lib/modules/storage.js

Lines changed: 20 additions & 19 deletions
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,7 +14,7 @@ class StorageRef extends ReferenceBase {
1314
this.storage = storage;
1415
}
1516

16-
downloadUrl() {
17+
downloadUrl(): Promise<Object> {
1718
const path = this.pathToString();
1819
return promisify('downloadUrl', FirestackStorage)(this.storage.storageUrl, path);
1920
}
@@ -23,7 +24,7 @@ class StorageRef extends ReferenceBase {
2324
* @param {String} downloadPath Where to store the file
2425
* @return {Promise}
2526
*/
26-
download (downloadPath, cb) {
27+
download (downloadPath: string, cb: (evt: Object) => Object): Promise<Object> {
2728
let callback = cb;
2829
if (!callback || typeof callback !== 'function') {
2930
callback = (evt) => {};
@@ -38,7 +39,7 @@ class StorageRef extends ReferenceBase {
3839
return promisify('downloadFile', FirestackStorage)(this.storage.storageUrl, path, downloadPath)
3940
.then((res) => {
4041
console.log('res --->', res);
41-
listeners.forEach(this.storage._removeListener);
42+
listeners.forEach(listener => listener.remove());
4243
return res;
4344
})
4445
.catch(err => {
@@ -47,8 +48,11 @@ class StorageRef extends ReferenceBase {
4748
}
4849
}
4950

51+
type StorageOptionsType = {
52+
storageBucket?: ?string,
53+
};
5054
export default class Storage extends Base {
51-
constructor(firestack, options={}) {
55+
constructor(firestack: Object, options:StorageOptionsType={}) {
5256
super(firestack, options);
5357

5458
if (this.options.storageBucket) {
@@ -58,8 +62,8 @@ export default class Storage extends Base {
5862
this.refs = {};
5963
}
6064

61-
ref(...path) {
62-
const key = this._pathKey(path);
65+
ref(...path: Array<string>): StorageRef {
66+
const key = this._pathKey(...path);
6367
if (!this.refs[key]) {
6468
const ref = new StorageRef(this, path);
6569
this.refs[key] = ref;
@@ -74,10 +78,10 @@ export default class Storage extends Base {
7478
* @param {object} metadata An object containing metadata
7579
* @return {Promise}
7680
*/
77-
uploadFile(name, filepath, metadata={}, cb) {
81+
uploadFile(name: string, filepath: string, metadata: Object={}, cb: (evt: Object) => Object): Promise<Object> {
7882
let callback = cb;
7983
if (!callback || typeof callback !== 'function') {
80-
callback = (evt) => {}
84+
callback = (evt: Object) => ({});
8185
}
8286

8387
filepath = filepath.replace("file://", "");
@@ -88,29 +92,26 @@ export default class Storage extends Base {
8892
listeners.push(this._addListener('upload_resumed', callback));
8993
return promisify('uploadFile', FirestackStorage)(this.storageUrl, name, filepath, metadata)
9094
.then((res) => {
91-
listeners.forEach(this._removeListener);
95+
listeners.forEach(listener => listener.remove());
9296
return res;
9397
});
9498
}
9599

96-
getRealPathFromURI(uri) {
100+
getRealPathFromURI(uri: string): Promise<string> {
97101
return promisify('getRealPathFromURI', FirestackStorage)(uri);
98102
}
99103

100-
_addListener(evt, cb) {
101-
return FirestackStorageEvt.addListener(evt, cb);
104+
_addListener(evt: string, cb: (evt: Object) => Object): {remove: () => void} {
105+
let listener = FirestackStorageEvt.addListener(evt, cb);
106+
return listener;
102107
}
103108

104-
_removeListener(evt) {
105-
return FirestackStorageEvt.removeListener(evt);
106-
}
107-
108-
setStorageUrl(url) {
109+
setStorageUrl(url: string): void {
109110
// return promisify('setStorageUrl', FirestackStorage)(url);
110111
this.storageUrl = `gs://${url}`;
111112
}
112113

113-
_pathKey(...path) {
114+
_pathKey(...path: Array<string>): string {
114115
return path.join('-');
115116
}
116117

@@ -126,7 +127,7 @@ export default class Storage extends Base {
126127
'FILETYPE_DIRECTORY': FirestackStorage.FILETYPE_DIRECTORY
127128
};
128129

129-
get namespace() {
130+
get namespace(): string {
130131
return 'firestack:storage'
131132
}
132133
}

0 commit comments

Comments
 (0)