[preview] Cache and share preview snapshots between all subscribers #612
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.
This is a refactor of the preview machinery that enables sharing of preview snapshots among all components that previews fields documents.
Previously, each component/part would have its own version of the document that it previewed, this caused a lot of double fetching, delayed UI updates, etc.
The major change introduced by this PR is that now all parts of the system that requests to preview fields of a document share the same cached representation of the document. For example, if ComponentA wants to preview the fields
fieldA
andfieldB
on document with iddoc-123
, a cache entry is made for the document with iddoc-123
, and we fetch and subscribe to changes infieldA
andfieldB
ofdoc-123
. Later on, ComponentB may want to previewfieldB
, andfieldC
on the same document. Now we only fetch and subscribe to changes infieldC
ofdoc-123
. The cached snapshot is shared between both components. If both of these components unsubscribe, we shut down the listeners and no longer update the cached snapshot. However, we still keep the cached snapshot in memory for later. This comes in handy if e.g. ComponentA re-subscribes to the same document/fields. We then immediately emit the last previous known snapshot, while fetching (syncing) the fields again and subscribing to changes in the background.I also renamed a few functions/files and added a few deprecation warnings to the public API. This API is AFAIK only used by us, and not documented so it shouldn't matter much, and the warnings are there mainly to make it easier to spot our own internal usage.
There is still room for improvements code-wise here, but I've tested this branch quite extensively and feels pretty confident that it works and doesn't contain any regressions (famous last words, I know 😅).