-
Notifications
You must be signed in to change notification settings - Fork 61
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
How can environment.retain() be used with the network layer to disable relay garbage collection? #114
Comments
I don't know if handling this on the network layer is a good idea. How we'd know which operation we should retain or dispose? |
We can implement it as a feature on top of the QueryRenderer or fetchWithMiddleware function (relay's fetch func) (eg: a ttl). The query is then retained for the ttl period. If the ttl provided to the QueryRenderer is larger than the TTL for the cache we'd never face the issues were the store was updated and the network cache overwrote these updates. How we'd know which operation we should retain or dispose?
Assume this scenario:
Note: |
Oh, understood! Thanks for the explanation @a-tokyo! It seems a really cool idea. |
@felippepuhle Awesome!! (: I was checking the codebase to see how I can implement it but faced a couple of issues that I hope I can overcome soon:
example of how the retain code should look like: /* @flow */
import { createOperationDescriptor, getRequest } from 'relay-runtime';
/**
* Retains data in the relay store to avoid garbage collection
*
* useful in case of subscriptions
*
* https://stackoverflow.com/questions/58022925/disable-relayjs-garbage-collection
*/
const retainInStore = ({
getEnvironment,
query,
variables,
}: {
getEnvironment: () => Environment,
query: string,
variables?: Object,
}): Disposable => {
// Create a relay request from the query
const request = getRequest(query);
// Create an operation descriptor for the query
const operation = createOperationDescriptor(request, variables);
// Tell Relay to retain this operation so any data referenced by it isn't garbage collected and return the disposable
return getEnvironment().retain(operation);
};
export default retainInStore; if (opts.ttl) {
const { dispose } = retainInStore({ getEnvironment, query, variables });
const timeout = setTimeout(() => { dispose() }, ttl);
// we can call clearTimeout(timeout); when we want to cleanup
} |
Description
How can we achieve using relay's environment.retain() to retain the data of a certain query and disable garbage collection?
Reasoning:
Example from relay documentation:
The text was updated successfully, but these errors were encountered: