Vydia / react-native-background-upload

Upload files in your React Native app even while it's backgrounded. Supports Android and iOS, including camera roll assets.
MIT License
726 stars 332 forks source link

How remove the listener events? #199

Open luigbren opened 4 years ago

luigbren commented 4 years ago

How can I delete the EventListener (Progress, Complete) when they finish their work? Because, I upload a file, when it is completed in the notifications it shows the message "completed" and when I upload another file, it doesn't do anything, it doesn't upload the file, I have to close the application and come back

Returns an EventSubscription. To remove the listener, call remove() on the EventSubscription. ????

.then((uploadId) => {
                Upload.addListener('progress', uploadId, (data) => {
                    console.log(`Progress: ${data.progress}%`)
                    if (data.progress == 100) {
                        //Remove EventListener
                    }
                })
                Upload.addListener('error', uploadId, (data) => {
                    console.log(`Error: ${data.error}%`)
                })
                Upload.addListener('cancelled', uploadId, (data) => {
                    console.log(`Cancelled!`)
                })
                Upload.addListener('completed', uploadId, (data) => {
                    // data includes responseCode: number and responseBody: Object
                    console.log('Completed!')
                    //Remove EventListener
                })
reime005 commented 4 years ago

@luigbren To remove a listener, you would do something like const listener = Upload.addListener(....) and then listener.remove(). I saw that the TypeScript definitions may not be correct here...

selvaprogrammer commented 4 years ago

Overwrite the package was the only solution for remove events. Ex : add this line in packages index.js file export const removeListener = () => { return DeviceEventEmitter.removeAllListeners(); }; export default { startUpload, cancelUpload, addListener, getFileInfo, removeListener }; and add these line in index.d.ts file static removeListener(): void;

Upload.removeListener() call this function in inside service it after complete response or before service call (based upon your requirement)

selvaprogrammer commented 4 years ago

Another solution i have found : import DeviceEventEmitter in current scene. Upload.addListener('progress', uploadId, (data) => { console.log(Progress: ${data.progress}%); if (data.progress === 100) DeviceEventEmitter.removeCurrentListener(); }); Upload.addListener('error', uploadId, (data) => { console.log(Error: ${data.error}%); DeviceEventEmitter.removeCurrentListener(); }); Upload.addListener('completed', uploadId, (data) => { let some = JSON.parse(data.responseBody); DeviceEventEmitter.removeCurrentListener(); });

removeCurrentListener removes that listener . Try it.

meidikawardana commented 2 years ago

@selvaprogrammer DeviceEventEmitter is deprecated now