npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vn-kooch-realtime

v2.7.7

Published

A saga enabling realtime updates for vn-kooch-react-admin

Downloads

1

Readme

vn-kooch-realtime

A custom saga enabling realtime update inside vn-kooch-react-admin.

Installation

Install with:

npm install --save vn-kooch-realtime

or

yarn add vn-kooch-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 vn-kooch-react-admin (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. If it returns null, the query won't be updated automatically. This allows you to decide which query should be updated in real time.

The observer have the following methods:

  • next(data): Call this method each time new data is received so that the vn-kooch-react-admin 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 'vn-kooch-realtime';

const observeRequest = dataProvider => (type, resource, params) => {
    // Filtering so that only posts are updated in real time
    if (resource !== 'posts') return;

    // Use your apollo client methods here or sockets or whatever else including the following very naive polling mechanism
    return {
        subscribe(observer) {
            const intervalId = setInterval(() => {
                dataProvider(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 dataProvider => realtimeSaga(observeRequest(dataProvider));

This saga factory can be used like this:

import React from "react";
import { Admin, Resource } from "vn-kooch-react-admin";

import createRealtimeSaga from "./createRealtimeSaga";
import dataProvider from "./dataProvider";
import posts from "./posts";

const realTimeSaga = createRealtimeSaga(dataProvider);

render(
    <Admin
        dataProvider={dataProvider}
        customSagas={[realTimeSaga]}
    >
        <Resource name="posts" {...posts} />
    </Admin>,
    document.getElementById("root")
);