@@ -71,46 +71,75 @@ export default class StorageRef extends ReferenceBase {
7171 /**
7272 * Downloads a reference to the device
7373 * @param {String } filePath Where to store the file
74- * @param listener
7574 * @return {Promise }
7675 */
77- downloadFile ( filePath : string , listener : Function = noop ) : Promise < Object > {
76+ downloadFile ( filePath : string ) : Promise < Object > {
7877 this . log . debug ( 'download(' , this . path , ') -> ' , filePath ) ;
79- this . storage . _addListener ( this . path , 'state_changed' , listener ) ;
8078
81- return promisify ( 'downloadFile' , FirestackStorage ) ( this . path , filePath )
82- . then ( ( res ) => {
83- this . storage . _removeListener ( this . path , 'state_changed' , listener ) ;
84- return res ;
85- } )
86- . catch ( ( err ) => {
87- this . log . error ( 'Error downloading ' , this . path , ' to ' , filePath , '. Error: ' , err ) ;
88- throw err ;
89- } ) ;
79+ let downloadTask = promisify ( 'downloadFile' , FirestackStorage ) ( this . path , filePath ) ;
80+ downloadTask . cancel = ( ) => {
81+ //TODO
82+ throw new Error ( '.cancel() is not currently supported by react-native-firestack' ) ;
83+ }
84+ downloadTask . on = ( event , nextOrObserver , error , complete ) => {
85+ //TODO: nextOrObserver as an object
86+ if ( nextOrObserver ) this . storage . _addListener ( this . path , 'state_changed' , nextOrObserver ) ;
87+ if ( error ) this . storage . _addListener ( this . path , 'download_failure' , error ) ;
88+ if ( complete ) this . storage . _addListener ( this . path , 'download_success' , complete ) ;
89+ return ( ) => {
90+ if ( nextOrObserver ) this . storage . _removeListener ( this . path , 'state_changed' , nextOrObserver ) ;
91+ if ( error ) this . storage . _removeListener ( this . path , 'download_failure' , error ) ;
92+ if ( complete ) this . storage . _removeListener ( this . path , 'download_success' , complete ) ;
93+ }
94+ }
95+ downloadTask . pause = ( ) => {
96+ //TODO
97+ throw new Error ( '.pause() is not currently supported by react-native-firestack' ) ;
98+ }
99+ downloadTask . resume = ( ) => {
100+ //TODO
101+ throw new Error ( '.resume() is not currently supported by react-native-firestack' ) ;
102+ }
103+
104+ return downloadTask ;
90105 }
91106
92- //TODO: Change to return UploadTask
93107 /**
94108 * Upload a file path
95109 * @param {string } filePath The local path of the file
96110 * @param {object } metadata An object containing metadata
97- * @param listener
98111 * @return {Promise }
99112 */
100- putFile ( filePath : Object , metadata : Object = { } , listener : Function = noop ) : /*UploadTask*/ Promise < Object > {
113+ putFile ( filePath : Object , metadata : Object = { } ) : Promise < Object > {
101114 const _filePath = filePath . replace ( 'file://' , '' ) ;
102115 this . log . debug ( 'putFile(' , _filePath , ') -> ' , this . path ) ;
103116
104- this . storage . _addListener ( this . path , 'state_changed' , listener ) ;
105-
106- return promisify ( 'putFile' , FirestackStorage ) ( this . path , _filePath , metadata )
107- . then ( ( res ) => {
108- this . storage . _removeListener ( this . path , 'state_changed' , listener ) ;
109- return res ;
110- } )
111- . catch ( ( err ) => {
112- this . log . error ( 'Error uploading file ' , this . path , ' to ' , _filePath , '. Error: ' , err ) ;
113- throw err ;
114- } ) ;
117+ //TODO: There's probably a better way of doing this, but I couldn't figure out the best way to extend a promise
118+ let uploadTask = promisify ( 'putFile' , FirestackStorage ) ( this . path , _filePath , metadata ) ;
119+ uploadTask . cancel = ( ) => {
120+ //TODO
121+ throw new Error ( '.cancel() is not currently supported by react-native-firestack' ) ;
122+ }
123+ uploadTask . on = ( event , nextOrObserver , error , complete ) => {
124+ //TODO: nextOrObserver as an object
125+ if ( nextOrObserver ) this . storage . _addListener ( this . path , 'state_changed' , nextOrObserver ) ;
126+ if ( error ) this . storage . _addListener ( this . path , 'upload_failure' , error ) ;
127+ if ( complete ) this . storage . _addListener ( this . path , 'upload_success' , complete ) ;
128+ return ( ) => {
129+ if ( nextOrObserver ) this . storage . _removeListener ( this . path , 'state_changed' , nextOrObserver ) ;
130+ if ( error ) this . storage . _removeListener ( this . path , 'upload_failure' , error ) ;
131+ if ( complete ) this . storage . _removeListener ( this . path , 'upload_success' , complete ) ;
132+ }
133+ }
134+ uploadTask . pause = ( ) => {
135+ //TODO
136+ throw new Error ( '.pause() is not currently supported by react-native-firestack' ) ;
137+ }
138+ uploadTask . resume = ( ) => {
139+ //TODO
140+ throw new Error ( '.resume() is not currently supported by react-native-firestack' ) ;
141+ }
142+
143+ return uploadTask ;
115144 }
116145}
0 commit comments