Git Product home page Git Product logo

aor-realtime's Introduction

aor-realtime

A custom saga enabling realtime update inside Admin-on-rest.

Installation

Install with:

npm install --save aor-realtime

or

yarn add aor-realtime

Usage

Define an observeRequest function which will be called by the realtime saga whenever a CRUD_GET_LIST or CRUD_GET_ONE fetch is triggered by Admin-on-rest (documentation about those).

This function will be called with the following parameters:

  • fetchType: either CRUD_GET_LIST or CRUD_GET_ONE
  • resource: the resource's name
  • params: the fetch parameters
    • for CRUD_GET_LIST: { pagination: { page: {int} , perPage: {int} }, sort: { field: {string}, order: {string} }, filter: {Object} }
    • for CRUD_GET_ONE: { id: {mixed} }

This function must return an object with a subscribe method which will be called with an observer. The observer have the following methods:

  • next(data): Call this method each time new data is received so that the Admin-on-rest views are updated.
  • complete(): Call this method to indicates this subscription won't receive any new data.
  • error(error): Call this method when an error occurs.

The subscribe method must return a subscription object. The subscription object must have an unsubscribe method which will be called by the realtime saga when the query will not need to be observed anymore. This will happen each time the current route change and will give you the opportunity to clean up related sockets, apollo observable queries, etc. When called and after you cleaned up whatever needed cleaning, you must call the observer.complete method so that the realtime saga is notified about it.

Here is a very naive example using an interval to fetch data every 5 seconds:

// In createRealtimeSaga.js
import realtimeSaga from 'aor-realtime';

const observeRequest = restClient => (type, 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(() => {
                restClient(type, 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;
        },
    };
};

export default restClient => realtimeSaga(observeRequest(restClient));

For a more realistic usage example, please refer to the realtime saga provided by the aor-simple-graphql-client.

aor-realtime's People

Contributors

djhi avatar fzaninotto avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.