Skip to content
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

Allow manually associating caches with reactive variables. #7350

Merged
merged 3 commits into from
Nov 20, 2020

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Nov 20, 2020

In the context of a larger cache read operation, accessing a reactive variable automatically associates the variable with the cache, so future updates to the variable can be broadcast to the cache.

Outside this context, it may not be possible for the variable to discover the currently active cache. For example, there is no currently active cache in asynchronous ApolloLink code (#7338).

Since we recently added a reactiveVar.forgetCache(cache) method in #7279, I think it makes sense to have a manual way to do the opposite, which I have decided to call reactiveVar.attachCache(cache).

Although this attachment is manual at the moment, in a hypothetical future where the client/cache manage the persistence of reactive variables, it should be easy to attach the appropriate cache to those variables when they are first initialized, which would enable reactive updates from literally anywhere, with no additional effort by the developer.

In the context of a larger cache read operation, accessing a reactive
variable automatically associates the variable with the cache, so future
updates to the variable can be broadcast to the cache.

Outside this context, it may not be possible for the variable to discover
the currently active cache. For example, there is no currently active
cache in asynchronous ApolloLink code (#7338).

Since we recently added a `reactiveVar.forgetCache(cache)` in #7279, I
think it makes sense to have a manual way to do the opposite, which I have
decided to call `reactiveVar.attachCache(cache)`.

Although this attachment is manual at the moment, in a hypothetical future
where the client/cache manage the persistence of reactive variables, it
should be easy to attach the appropriate cache to those variables when
they are first initialized, which would enable reactive updates from
literally anywhere, with no additional effort by the developer.
Copy link
Member

@hwillson hwillson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea @benjamn 👍

@benjamn
Copy link
Member Author

benjamn commented Nov 20, 2020

Hmm, on second thought, I'm not sure this really helps with #7338, since updating the reactive variable only invalidates a query if the reactive variable was consumed while reading that query previously. We can make sure updating the variable will call cache.broadcastWatches(), but if nothing has changed in the cache, none of the watching queries will be notified.

I still think this is a worthwhile addition, but some additional work will be necessary to register reactive variable dependencies in the link chain (which is completely separate from the cache).

@benjamn benjamn merged commit 8a5b8c8 into release-3.3 Nov 20, 2020
@benjamn benjamn deleted the reactiveVar.attachCache branch November 20, 2020 20:18
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants