redux-saga / saga-query

Data synchronization using a middleware system for front-end apps
64 stars 4 forks source link

new timer middleware #15

Closed neurosnap closed 2 years ago

neurosnap commented 2 years ago

This PR upgrade the cache timer middleware.

import { createApi, requestMonitor, fetcher, timer } from 'saga-query';

const api = createApi();
api.use(requestMonitor());
api.use(api.routes());
api.use(fetcher());

const fetchApp = api.get<{ id: string }>('/app/:id', { saga: timer() });
dispatch(fetchApp({ id: '1' }))
dispatch(fetchApp({ id: '1' }))
dispatch(fetchApp({ id: '2' }))
dispatch(fetchApp({ id: '2' }))

not using timer: 4 requests (fetch id 1) old timer: 1 request (fetch id 1) new timer: 2 requests (fetch id 1 and 2)

The old timer would cache any call to fetchApp regardless of the action payload. This is not ideal because we actually want to request per payload for each endpoint. The new timer will only make the http request when the action payload is different.