How remove the listener events?
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
})
@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...
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)
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.
@selvaprogrammer DeviceEventEmitter is deprecated now