From ce6d002c7d06f18d04c94b1dfea2768364582b8f Mon Sep 17 00:00:00 2001 From: Daniil Markov Date: Sun, 22 Sep 2024 10:01:01 +1000 Subject: [PATCH] fix: add type check for `lastRNBFTask.cancel` method This commit addresses an issue where calling `this.lastRNBFTask.cancel` could result in a "is not a function" error. The fix adds a type check to ensure that cancel is a function before attempting to call it. --- index.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 96d8d2bd..4a59f544 100644 --- a/index.js +++ b/index.js @@ -129,10 +129,12 @@ export default class Pdf extends Component { if ((nextSource.uri !== curSource.uri)) { // if has download task, then cancel it. - if (this.lastRNBFTask) { - this.lastRNBFTask.cancel(err => { - this._loadFromSource(this.props.source); - }); + if (this.lastRNBFTask ) { + if (typeof this.lastRNBFTask.cancel === 'function') { + this.lastRNBFTask.cancel(err => { + this._loadFromSource(this.props.source); + }); + } this.lastRNBFTask = null; } else { this._loadFromSource(this.props.source); @@ -148,8 +150,10 @@ export default class Pdf extends Component { componentWillUnmount() { this._mounted = false; if (this.lastRNBFTask) { - // this.lastRNBFTask.cancel(err => { - // }); + if (typeof this.lastRNBFTask.cancel === 'function') { + this.lastRNBFTask.cancel(err => { + }); + } this.lastRNBFTask = null; } @@ -253,8 +257,10 @@ export default class Pdf extends Component { _downloadFile = async (source, cacheFile) => { if (this.lastRNBFTask) { - this.lastRNBFTask.cancel(err => { - }); + if (typeof this.lastRNBFTask.cancel === 'function') { + this.lastRNBFTask.cancel(err => { + }); + } this.lastRNBFTask = null; }