2

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 ?

MhdSyrwan
  • 1,613
  • 3
  • 19
  • 26

1 Answers1

2

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

Gildas Garcia
  • 6,966
  • 3
  • 15
  • 29
  • But If i used `restClient` directly, it woud not update the UI (the currently shown record/list). I think i should dispatch a `CRUD_GET_LIST` action (for the case of list view). How can i dispatch such redux action from the context of a realtime saga ? – MhdSyrwan May 11 '17 at 14:34
  • To be more clear, I'm trying to add an auto-refreshing functionality to the json rest-client by using websockets. – MhdSyrwan May 11 '17 at 14:36
  • 1
    When you call `observer.next` with the new data, the saga will call the appropriate action type (either `CRUD_GET_LIST_SUCCESS` or `CRUD_GET_ONE_SUCCESS`. Btw, I also updated the README for https://github.com/marmelab/aor-realtime to use an existing client – Gildas Garcia May 11 '17 at 14:46