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

lore-hook-polling

v0.13.0

Published

A Lore hook that enables polling of action

Downloads

6

Readme

lore-hook-polling

Purpose

A hook that implements polling behavior, providing the ability to invoke an action repeatedly at a given interval.

Usage

First register the hook so that it gets loaded:

lore.summon({
  hooks: {
    // ...
    polling: require('lore-hook-polling'),
    // ...
  }
});

This hook duplicates the lore.actions object but wraps each action with a function that allows it to be polled (it will call that action repeatedly at a specified interval). To illustrate usage, let's say you have a Feed component that fetches a list of tweets from the server. That component might look like this:

module.exports = lore.connect(function(getState, props){
  return {
    tweets: getState('tweet.find')
  };
})(
React.createClass({
  propTypes: {
    tweets: PropTypes.object.isRequired
  },

  render: function() {
    // .. render tweets
  }
})

Normally this component will fetch the list of tweets from the server once, and display what it gets back. This hook allows you to repeatedly fetch the list of tweets on a given interval. To use it, add a componentDidMount method to Feed that looks like this:

...
  componentDidMount: function() {
    this.poll = lore.polling.tweet.find();
    this.poll.start();
  },
...

The lore.polling.tweet.find call mirrors the action structure. Invoking one of the "actions" returns a polling object. Calling poll.start() will start the process of invoking that action on specified interval (defaults to 5 seconds).

If you want to stop polling, you can call poll.stop(). For example, let's say the data you're fetching is only relevant (and visible) to this component. In that case, there's no point to continually polling for changes when the user isn't viewing this page. To address that use case, simply add a componentWillUnmount method that stops polling.

...
  componentWillUnmount: function() {
    this.poll.stop();
  },
...

If your component needs data with query or pagination information, you can leverage the query attribute of the collection data to duplicate the action:

module.exports = lore.connect(function(getState, props){
  return {
    tweets: getState('tweet.find')
  };
})(
React.createClass({
  propTypes: {
    tweets: PropTypes.object.isRequired
  },

  componentDidMount: function() {
    var tweets = this.props.tweets;
    var query = tweets.query;
    this.poll = lore.polling.tweet.find(query.where, query.pagination);
    this.poll.start();
  },

  componentWillUnmount: function() {
    this.poll.stop();
  },

  render: function() {
    // .. render tweets
  }
})

Note

The hook generates a unique key internally for each action call that's a combination of the action name (e.g. tweet.find) and the arguments passed to that action (for example the where and pagination objects). It uses this key to make sure only one polling object exists per unique action call.

This means even if you call that same action repeatedly, like this:

lore.polling.tweet.find().start()
lore.polling.tweet.find().start()
lore.polling.tweet.find().start()

Only one polling action will be created. Subsequent calls return the same polling object, and subsequent calls to start() are ignored because the polling object is already polling for data.

Config

There are two config options exposed for this hook

module.exports = {

   /**
    * The frequency at which the action should be invoked (in milliseconds)
    */

   // interval: 5000,

   /**
    * Determines whether the first request should be delayed when polling starts.
    * If 'true', poll.start() will wait for the specified interval before invoking
    * the action. It 'false', the action will be invoked immediately.
    */

   // delayOnStart: true

};

Model Overrides

Additionally, these config values can be specified on per-model basis by adding a polling section to the model's config:

// src/models/tweet.js
module.exports = {
  polling: {
    interval: 20000,
    delayOnStart: false
  }
};