Search code examples
javascriptreactjsreduxredux-sagaadmin-on-rest

admin-on-rest how to implement a custom saga for auto-refresh


In the example provided in the aor-realtime readme

import realtimeSaga from 'aor-realtime';

const observeRequest = (fetchType, resource, params) => {
    // Use your apollo client methods here or sockets or whatever else including the following very naive polling mechanism
    return {
        subscribe(observer) {
            const intervalId = setInterval(() => {
                fetchData(fetchType, resource, params)
                    .then(results => observer.next(results)) // New data received, notify the observer
                    .catch(error => observer.error(error)); // Ouch, an error occured, notify the observer
            }, 5000);

            const subscription = {
                unsubscribe() {
                    // Clean up after ourselves
                    clearInterval(intervalId);
                    // Notify the saga that we cleaned up everything
                    observer.complete();
                }
            };

            return subscription;
        },
    };
};

const customSaga = realtimeSaga(observeRequest);

fetchData function is mentioned, but it's not accessible from that scope, is it just a symbolic/abstract call ?

If I really wanted to refresh the data based on some realtime trigger how could i dispatch the data fetching command from this scope ?


Solution

  • You're right, it is a symbolic/abstract call. It's up to you to implement observeRequest, by initializing it with your restClient for example and calling the client methods accordingly using the fetchType, resource and params parameters.

    We currently only use this saga with the aor-simple-graphql-client client