[data view editor] Form state refactor - create service, replace useEffect with observables#142421
Conversation
|
@Dosant I've modified the code to eliminate the possibility of stale data between flyout invocations. I saw the timestamp field being populated without an index pattern - this has been fixed. I've reduced the number of |
If the consequence of this PR are additional (not needed) requests and thus reduced performance then we can either address the issue in this PR or merge this PR into a feature branch and resolve the issue in the followup and once its resolved we can merge the feature branch into main. |
|
|
||
| private getIndicesMemory: Record<string, Promise<MatchedItem[]>> = {}; | ||
| debounceGetIndices = async (props: { pattern: string; showAllIndices?: boolean | undefined }) => { | ||
| const debouncedRequest = debounce(this.dataViews.getIndices, 60000, { |
There was a problem hiding this comment.
I think I don't understand why we wrap this into debounce. since this function would be called only once - I guess there is no need to debounce it.
Was the intention to make the cache live for 60 seconds? Then I think it doesn't work this way and instead cache is alive for as long as the instance of the service is alive. Because with every getIndicesMemory call we get the cached promise from memory instead of calling the debounced function again
There was a problem hiding this comment.
ah, good catch! No, debounce isn't needed. I've removed it. I think its okay for us to cache the response for the life of the flyout.
There was a problem hiding this comment.
Ah, Sounds good then 👍
Should we clean up the cache in case of an error?
There was a problem hiding this comment.
Should we clean up the cache in case of an error?
Definitely.
|
|
||
| this.getIndicesMemory[key] = | ||
| this.getIndicesMemory[key] || | ||
| this.dataViews.getIndices({ ...props, isRollupIndex: await this.getIsRollupIndex() }); |
There was a problem hiding this comment.
This probably won't be visible for the end user, but because of this await, we'd have a delay between putting the promise into the cache and accessing the cache.
This code could be read like this:
if (!this.getIndicesMemory[key]) {
const isRollup = await this.getIsRollupIndex(); // <---- breaking the sync execution. gives a chance for other code to run "in parallel"
this.getIndicesMemory[key] = this.dataViews.getIndices({ ...props, isRollupIndex: isRollup });
}
this means that there is a moment when getIndicesMemory can be called again and there is no promise yet in the cahche and then we do a redundant request.
this looks uglier, but probably can be fixed like this:
if (!this.getIndicesMemory[key]) {
this.getIndicesMemory[key] = this.getIsRollupIndex().then(isRollup => this.dataViews.getIndices({ ...props, isRollupIndex: isRollup }))
}
There was a problem hiding this comment.
we can also leave it as is, if we think this isn't a problem at all
There was a problem hiding this comment.
Practically speaking I think the code is fine but I like the formally correct solution you've proposed.
|
Recent code changes lgtm. I tried to smoke check and noticed that I can't edit -> save data view anymore. I couldn't reproduce in main: can.t.save.mov |
…a into refactor_data_view_create
|
@Dosant Addressed and added test |
💚 Build Succeeded
Metrics [docs]Module Count
Async chunks
Page load bundle
Unknown metric groupsESLint disabled in files
ESLint disabled line counts
Total ESLint disabled count
History
To update your PR or re-run it, just comment with: |
Dosant
left a comment
There was a problem hiding this comment.
Tested, seemed to be working well :)
…ffect with observables (elastic#142421) * replace useEffect with observables as much as possible * cleanup, fix timestamp field / allow hidden bug
…rvice and out of react state management (#143604) ## Summary This PR completes the data view editor state service. Of note: - All form business logic is now in service although validation is still handled by the form lib - Service is initialized with dependencies, form config, and initial state - Service state is updated via simple method calls - Service state updates are provided to React code via observables using `useObservable` - Service is provided via parent component which uses `useRef` so the service is created once per flyout creation IMO the diff isn't very helpful. It might be easier to review the changed files in completion. Follow up to #142421 Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Summary
Note: I attempted further refactoring but ran into problems. These changes are sufficient to provide a number of bug fixes.
Follow up to and in support of #142362