-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to abort a kfetch call. #20700
Conversation
💔 Build Failed |
src/ui/public/kfetch/index.js
Outdated
return { | ||
fetching, | ||
abort, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about attaching abort
to the promise like:
fetching.abort = abort;
That way we can also get rid of isAbortable
since all requests become abortable and we don't need a secondary api (for abortable requests).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally considered this approach but I had a few concerns about monkey-patching a native API:
-
If a native
abort
method is introduced to promises at some point in the future then we'll need to ensure it either has identical functionality, or make a decision as to whether to cut over to the official method and ensure BWC or to keep the monkey-patched method and have unexpected behavior. -
Monkey-patching has the potential to confuse consumers as well since this method isn't in the spec, prompting questions like "Is this really a native promise?" and "What other non-spec methods are attached to this promise?" It requires the user to understand the implementation in order to have confidence about what's changed about this promise.
These concerns are mostly based on principle so if you think it's more pragmatic to just monkey-patch the promise, let me know your thoughts because I can definitely be persuaded! OTOH if you think these concerns are worthy then I think creating a secondary API makes sense, because we're highlighting the fact that the consumer is consciously opting into secondary behavior. And realistically, do you think it will be used very often? Do we have many use cases for aborting a fetch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are very good points. I was mostly concerned about having two APIs but as you said, there won't be many use cases for aborting fetch, and in those cases it will be a conscious effort.
I like the approach you suggested with a separate kfetchAbortable
method.
|
src/ui/public/kfetch/index.js
Outdated
const abortController = new AbortController(); | ||
signal = abortController.signal; | ||
abort = abortController.abort.bind(abortController); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe abstract this into a separate createAbortable
method that returns { signal, abort }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!
Thanks for reminding me! Yes I need to do that. |
Alternatively, we could also create a method specifically for creating abortable fetch promises. This way we wouldn't need to alter the export function kfetchAbortable(fetchOptions, kibanaOptions) {
const abortController = new AbortController();
const signal = abortController.signal;
const abort = abortController.abort.bind(abortController);
const fetching = kfetch({ ...fetchOptions, signal }, kibanaOptions);
return {
fetching,
abort,
};
} |
@nreese @sqren Ready for another look! CC @jen-huang I've implemented the idea we talked about. |
src/ui/public/kfetch/kfetch.js
Outdated
} | ||
|
||
resolve(res.json()); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need the Promise constructor anymore, do we? Eg. just revert to what we had (unless you prefer this style ofc :) )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't mind, I would like to keep it in this style because it makes it just a bit more explicit that a promise is being returned (at least to me!)
fetching, | ||
abort, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very clean. I like it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. very nice and easy to follow
code review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Feel free to merge regardless of the nits.
💔 Build Failed |
💔 Build Failed |
Failure:
Screenshot: |
Retest |
💚 Build Succeeded |
* Split kfetch module into kfetch and kfetchAbortable sub-modules. * Add abortcontroller-polyfill.
I really like where this ended up! |
This will be needed for rollups support, since we'll use
kfetch
to make the search request to the rollups API endpoint, and courier'scallClient
function requires the ability to abort the promises representing the in-flight search requests.I tried to add a test asserting that
abort
actually stopped the promise from resolving, but I don't think thefetch-mock
module supports this: