Skip to content

Commit 31aebc6

Browse files
committed
Add flow to storage.js
1 parent bee8871 commit 31aebc6

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
import { NativeModules, NativeEventEmitter } from 'react-native';
23

34
import { promisify, noop } from '../utils';
@@ -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
}
@@ -24,7 +25,7 @@ class StorageRef extends ReferenceBase {
2425
* @param listener
2526
* @return {Promise}
2627
*/
27-
download(downloadPath: string, listener: Function = noop) {
28+
download(downloadPath: string, listener: Function = noop): Promise<Object> {
2829
const path = this.pathToString();
2930
const listeners = [
3031
this.storage._addListener('download_progress', listener),
@@ -35,7 +36,7 @@ class StorageRef extends ReferenceBase {
3536
return promisify('downloadFile', FirestackStorage)(this.storage.storageUrl, path, downloadPath)
3637
.then((res) => {
3738
console.log('res --->', res);
38-
listeners.forEach(this.storage._removeListener);
39+
listeners.forEach(listener => listener.remove());
3940
return res;
4041
})
4142
.catch((downloadError) => {
@@ -45,8 +46,11 @@ class StorageRef extends ReferenceBase {
4546
}
4647
}
4748

49+
type StorageOptionsType = {
50+
storageBucket?: ?string,
51+
};
4852
export default class Storage extends Base {
49-
constructor(firestack, options = {}) {
53+
constructor(firestack: Object, options:StorageOptionsType={}) {
5054
super(firestack, options);
5155

5256
if (this.options.storageBucket) {
@@ -56,8 +60,8 @@ export default class Storage extends Base {
5660
this.refs = {};
5761
}
5862

59-
ref(...path) {
60-
const key = this._pathKey(path);
63+
ref(...path: Array<string>): StorageRef {
64+
const key = this._pathKey(...path);
6165
if (!this.refs[key]) {
6266
const ref = new StorageRef(this, path);
6367
this.refs[key] = ref;
@@ -73,7 +77,7 @@ export default class Storage extends Base {
7377
* @param listener
7478
* @return {Promise}
7579
*/
76-
uploadFile(name: string, filePath: string, metadata: Object = {}, listener: Function = noop) {
80+
uploadFile(name: string, filePath: string, metadata: Object = {}, listener: Function = noop): Promise<Object> {
7781
const _filePath = filePath.replace('file://', '');
7882
const listeners = [
7983
this._addListener('upload_paused', listener),
@@ -83,29 +87,26 @@ export default class Storage extends Base {
8387

8488
return promisify('uploadFile', FirestackStorage)(this.storageUrl, name, _filePath, metadata)
8589
.then((res) => {
86-
listeners.forEach(this._removeListener);
90+
listeners.forEach(listener => listener.remove());
8791
return res;
8892
});
8993
}
9094

91-
getRealPathFromURI(uri) {
95+
getRealPathFromURI(uri: string): Promise<string> {
9296
return promisify('getRealPathFromURI', FirestackStorage)(uri);
9397
}
9498

95-
_addListener(evt, cb) {
96-
return FirestackStorageEvt.addListener(evt, cb);
99+
_addListener(evt: string, cb: (evt: Object) => Object): {remove: () => void} {
100+
let listener = FirestackStorageEvt.addListener(evt, cb);
101+
return listener;
97102
}
98103

99-
_removeListener(evt) {
100-
return FirestackStorageEvt.removeListener(evt);
101-
}
102-
103-
setStorageUrl(url) {
104+
setStorageUrl(url: string): void {
104105
// return promisify('setStorageUrl', FirestackStorage)(url);
105106
this.storageUrl = `gs://${url}`;
106107
}
107108

108-
_pathKey(...path) {
109+
_pathKey(...path: Array<string>): string {
109110
return path.join('-');
110111
}
111112

@@ -121,8 +122,8 @@ export default class Storage extends Base {
121122
FILETYPE_DIRECTORY: FirestackStorage.FILETYPE_DIRECTORY,
122123
};
123124

124-
get namespace() {
125-
return 'firestack:storage';
125+
get namespace(): string {
126+
return 'firestack:storage'
126127
}
127128
}
128129

0 commit comments

Comments
 (0)