diff --git a/src/modules/gql-module/abortCallback.ts b/src/modules/gql-module/abortCallback.ts index 46eb72fb..6d072297 100644 --- a/src/modules/gql-module/abortCallback.ts +++ b/src/modules/gql-module/abortCallback.ts @@ -1,13 +1,21 @@ class AbortCallback { + private isAborted: boolean callback: Promise onAbort: () => void constructor(callback: Promise, onAbort: () => void) { + this.isAborted = false this.callback = callback this.onAbort = onAbort } then(onSuccess: (data: any) => any, onError?: (error: any) => any) { + if (this.isAborted) { + const dummyPromise = new Promise(() => {}) + + return new AbortCallback(dummyPromise, this.onAbort) + } + return new AbortCallback(this.callback.then(onSuccess, onError), this.onAbort) } @@ -20,6 +28,7 @@ class AbortCallback { } abort() { + this.isAborted = true this.onAbort() } } diff --git a/src/modules/gql-module/abortPromise.ts b/src/modules/gql-module/abortPromise.ts index d8e57e2d..e512b983 100644 --- a/src/modules/gql-module/abortPromise.ts +++ b/src/modules/gql-module/abortPromise.ts @@ -16,9 +16,12 @@ const dummyPromise = new Promise(() => {}) class AbortPromise { private promise: Promise private isAborted: boolean + private onAbort?: () => void - constructor(callback: AbortPromiseCallback) { + constructor(callback: AbortPromiseCallback, onAbort?: () => void) { this.isAborted = false + this.onAbort = onAbort + this.promise = new Promise(callback) .then((data) => { try { @@ -58,6 +61,7 @@ class AbortPromise { } }) } + const allPromises = Promise.all(promises).then((data) => { if (isAborted) { return dummyPromise as Promise @@ -95,6 +99,10 @@ class AbortPromise { abort() { this.isAborted = true + + if (typeof this.onAbort === 'function') { + this.onAbort() + } } } diff --git a/src/modules/gql-module/abortRequest.ts b/src/modules/gql-module/abortRequest.ts index e385bc02..9d35cf6e 100644 --- a/src/modules/gql-module/abortRequest.ts +++ b/src/modules/gql-module/abortRequest.ts @@ -1,4 +1,4 @@ -import AbortCallback from './abortCallback' +import AbortPromise from './abortPromise' type ModifyCallback = (value: Data) => ModifiedData | PromiseLike @@ -18,14 +18,12 @@ type PendingRequest = { const requestsQueue: Record = {} -const dummyPromise = new Promise(() => {}) - // Returns fetch promise that can be aborted // If we create several promises, only one request will be executed class AbortRequest { private controller = new AbortController() request: Promise - promise: Promise + promise: AbortPromise body: string isAborted: boolean @@ -63,42 +61,32 @@ class AbortRequest { } } - this.promise = this.request - .then((data) => { - try { - if (this.isAborted) { - return dummyPromise as Promise - } - - if (typeof onSuccess === 'function') { - return onSuccess(data) as Promise - } + this.promise = new AbortPromise(async (resolve, reject) => { + try { + const result = await this.request - return data as Promise - } - catch (error) { - return Promise.reject(error) - } - }) - .catch((error) => { - if (this.isAborted) { - return dummyPromise as Promise + if (typeof onSuccess === 'function') { + return resolve(onSuccess(result) as ModifiedData) } - return Promise.reject(error) - }) + return resolve(result as ModifiedData) + } + catch (error) { + return reject(error) + } + }, this.abort.bind(this)) } then(onSuccess: FirstCallback, onError?: FirstCallback) { - return new AbortCallback(this.promise.then(onSuccess, onError), this.abort.bind(this)) + return this.promise.then(onSuccess, onError) } catch(callback: FirstCallback) { - return new AbortCallback(this.promise.catch(callback), this.abort.bind(this)) + return this.promise.catch(callback) } finally(callback: EmptyCallback) { - return new AbortCallback(this.promise.finally(callback), this.abort.bind(this)) + return this.promise.finally(callback) } abort() {