-
Notifications
You must be signed in to change notification settings - Fork 66
Add abortable loading for PageWithTable #415
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
Open
fschinkel
wants to merge
1
commit into
releases/26.2
Choose a base branch
from
features/fschinkel/26.2/PageWithTable-abortable-loading
base: releases/26.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,3 +94,90 @@ This is useful for composite fields that don't have its own empty state like `Gr | |
|
|
||
| To influence or disable the computation of the `empty` property, you can set the property `checkEmpty` or override the method `_computeEmpty()`. | ||
| Please see the JsDoc of the `FormFieldModel.checkEmpty` for details. | ||
|
|
||
| == abortableContext (Scout JS) | ||
|
|
||
| When working with rest requests often there is the need to abort previously created requests again. | ||
| As typically all requests made using Scout JS are created using `ajax` the developer does not have access to the abortable call and therefore, is unable to use e.g. an `AbortController` to abort these calls. | ||
| To address this issue the `abortableContext` was introduced. With this one can run a callback in the context of an `AbortController`. | ||
| All abortable elements that are registered while this context is present may than be aborted using the `AbortController`. | ||
| In addition, all calls created using the `ajax` utility auto register themselves in the current context if there is one. | ||
|
|
||
| [source,typescript,opts=novalidate] | ||
| ---- | ||
| protected async _loadFooAndBarAbortable(id: string | number, abortController: AbortController): Promise<{ foo: object, bar: object }> { | ||
| return await abortableContext.runInContext( | ||
| () => this._loadFooAndBar(id), | ||
| abortController | ||
| ); | ||
| } | ||
|
|
||
| protected async _loadFooAndBar(id: string | number): Promise<{ foo: object, bar: object }> { | ||
| // if run inside an abortableContext the ajax calls are auto registered | ||
| const fooPromise = ajax.getDataObject(`api/foo/${id}`); | ||
| const barPromise = ajax.getDataObject(`api/bar/${id}`); | ||
|
|
||
| return { | ||
| foo: await fooPromise, | ||
| bar: await barPromise | ||
| }; | ||
| } | ||
| ---- | ||
|
|
||
| Other abortables can be registered in the current context using `abortableContext.registerAbortableInCurrentContext(abortable)`. | ||
| If one creates a new abortable context while running inside another abortable context, the inner context is registered in the outer one to ensure that the inner context is aborted when the outer context is aborted. | ||
|
|
||
| [source,typescript,opts=novalidate] | ||
| ---- | ||
| const outerController = new AbortController(); | ||
| abortableContext.runInContext( | ||
| () => { | ||
| const innerController = new AbortController(); | ||
| abortableContext.runInContext( | ||
| () => { | ||
| const innerAbortable = new MyAbortable(); | ||
| abortableContext.registerAbortableInCurrentContext(innerAbortable); | ||
| }, | ||
| innerController | ||
| ); | ||
| }, | ||
| outerController | ||
| ); | ||
|
|
||
| // aborts the innerController and therefore the innerAbortable as well | ||
| outerController.abort(); | ||
| ---- | ||
|
|
||
| == PageWithTable: abortable loading (Scout JS) | ||
|
|
||
| The `PageWithTable` now supports abortable loading. In order to support this the `PageWithTable` creates an `abortableContext` to run its `_loadTableData(searchFilter)` in. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: To support this, |
||
| For a typical implementation where data is loaded via rest, the call is registered automatically. | ||
|
|
||
| [source,typescript,opts=novalidate] | ||
| ---- | ||
| protected override _loadTableData(searchFilter: any): JQuery.Promise<any> { | ||
| return ajax.postDataObject('api/foo/list', this._withMaxRowCountContribution(searchFilter)); | ||
| } | ||
| ---- | ||
|
|
||
| As only synchronously created calls are registered automatically, implementations that make several calls subsequently must create new abortable contexts for each asynchronously created rest call to ensure all calls are aborted correctly. | ||
| If this is not done, the `PageWithTable` will still behave correctly, i.e. will not show resulting data when loading was aborted. | ||
| So creating a new abortable context for a subsequent call is not necessary for the user experience but prevents unnecessary calls being made. | ||
| To create such a new abortable context all implementations of `PageWithTable` can use the protected member `_abortController`. | ||
|
|
||
| [source,typescript,opts=novalidate] | ||
| ---- | ||
| protected override _loadTableData(searchFilter: any): JQuery.Promise<any> { | ||
| return $.when(this._loadTableDataAsync(searchFilter)); | ||
| } | ||
|
|
||
| protected async _loadTableDataAsync(searchFilter: any): Promise<any> { | ||
| const abortController = this._abortController; | ||
| const searchRestrictions = await ajax.postDataObject('search-api/util/build-search-restrictions', this._withMaxRowCountContribution(searchFilter)); | ||
| const foos = await abortableContext.runInContext( | ||
| () => ajax.postDataObject('api/foo/list', {searchRestrictions}), | ||
| abortController | ||
| ); | ||
| return foos; | ||
| } | ||
| ---- | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
To address this issue the
abortableContextwas introduced. It allows code to be executed within the scope of anAbortController.