Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

"cache entry level" lifecycle #215

Closed
phryneas opened this issue Apr 18, 2021 · 2 comments
Closed

"cache entry level" lifecycle #215

phryneas opened this issue Apr 18, 2021 · 2 comments
Labels
discussion Requires further discussion enhancement New feature or request
Milestone

Comments

@phryneas
Copy link
Collaborator

phryneas commented Apr 18, 2021

We could introduce a cacheEntryContext next to the existing requestContext (renamed from just context and add two new lifecycle callbacks: cacheEntryAdded and cacheEntryCleared.
Those two lifecycles would allow supporting stuff like graphql subscriptions or websocket streams.

    getPosts: build.query({
      query: (arg) => ({ url: `posts` }), // fetches data that will then be updated by a subscription
      cacheEntryAdded(arg, { dispatch, cacheEntryContext }) {
        cacheEntryContext.stopListening = ws.listen(() => {
          // ... 
          dispatch(
            api.util.updateQueryResult('cacheEntryAdded', arg, (draft) => {
              draft.foo = ...
            })
          )
        })
      },
      cacheEntryCleared({ id }, { dispatch, cacheEntryContext }) {
        cacheEntryContext.stopListening()
      },
    }),

OR we go useEffect here and have cacheEntryAdded just return a cleanup callback. Then we could skip all the cacheEntryContext nonsense and omit cacheEntryCleared

    getPosts: build.query({
      query: (arg) => ({ url: `posts` }), // fetches data that will then be updated by a subscription
      cacheEntryAdded(arg, { dispatch }) {
        const stopListening = ws.listen(() => {
          // ... 
          dispatch(
            api.util.updateQueryResult('cacheEntryAdded', arg, (draft) => {
              draft.foo = ...
            })
          )
        })
        return () => {
          stopListening()
        }
      }
    }),
@msutkowski msutkowski added discussion Requires further discussion enhancement New feature or request labels Apr 18, 2021
@markerikson
Copy link
Collaborator

Lenz just put up a commit with hypothetical designs for three different variations on this concept: "context"-based, "callback"-based, and "promise"-based:

https://github.com/rtk-incubator/rtk-query/pull/223/files#diff-c3ac4236290f8a1c76845f190db173acaed90fec487886e4b0e90ecbf21f13b4

Thoughts?

@phryneas
Copy link
Collaborator Author

This is on the integration branch right now, closing it here.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
discussion Requires further discussion enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants