【问题标题】:Disable relayjs garbage collection禁用relayjs垃圾收集
【发布时间】:2020-01-21 04:35:25
【问题描述】:

有没有办法禁用 relayjs 垃圾回收(版本 5.0.0 或 6.0.0)?

我们仍在使用经典的 relayjs,它会缓存会话中的所有数据。这使得在获取新数据的同时快速加载以前的页面。在 relayjs 5.0.0 中,它们在 QueryRenderer 上有一个 dataFrom,可以设置为“STORE_THEN_NETWORK”,它将首先尝试中继缓存存储并从网络中获取,就像 rejay 经典一样。除了较新版本的中继使用垃圾收集功能来删除当前未使用的数据。这使得几乎所有页面都从网络中获取数据。

【问题讨论】:

    标签: relayjs relay relaymodern


    【解决方案1】:

    我设法让这个工作。这里的关键是environment.retain(operation.root);,它将在缓存中保留对象。

    然后在QueryRenderer 中使用fetchPolicy="store-and-network"

    在下面查看我的完整中继环境文件。

    import {Environment, Network, RecordSource, Store} from 'relay-runtime';
    
    function fetchQuery(operation, variables) {
        const environment = RelayEnvironment.getInstance();
        environment.retain(operation.root);
    
        return fetch(process.env.GRAPHQL_ENDPOINT, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            credentials: 'include',
            body: JSON.stringify({
                query: operation.text,
                variables
            })
        }).then(response => {
            return response.json();
        });
    }
    
    const RelayEnvironment = (function() {
        let instance;
    
        function createInstance() {
            return new Environment({
                network: Network.create(fetchQuery),
                store: new Store(new RecordSource())
            });
        }
    
        return {
            getInstance: function() {
                if (!instance) {
                    instance = createInstance();
                }
                return instance;
            }
        };
    })();
    
    export default RelayEnvironment;
    

    也从 Relay Slack Channel 获得了这个。还没试过。

    const store = new Store(new RecordSource());
    (store as any).holdGC(); // Disable GC on the store.
    

    【讨论】:

      猜你喜欢
      • 2011-10-15
      • 2016-01-29
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 2011-03-10
      相关资源
      最近更新 更多