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

redux-saga-take-latest-by

v1.0.2

Published

Variation of takeLatest redux-saga effect, allowing to take latest action by specified key.

Downloads

13

Readme

redux-saga-take-latest

Variation of takeLatest redux-saga effect, allowing to take latest action by specified key.

Usage

import takeLatestBy from 'redux-saga-take-latest-by';

yield takeLatestBy(
  FETCH_FROM_COLLECTION,
  fetchFromCollection,
  action => action.collectionId
)

Motivation

Intro

Suppose you want to fetch data from your API. Data is stored in collections, by collection id. This might be your state:

{
  'collection1': {
    loading: false,
    data: [/*some array*/]
  },
  'collection2': {
    loading: false,
    data: [/*some array*/]
  }
}

Problem

You may have want to load the data, filtered by the search term, entered by input. If you use takeEvery effect, you may encounter race condition issue. Take a look:

  • user types Adam
  • we start loading data for the term Adam, but user then types full name: Adam Sandler
  • if, by any chance, previous request ends up being handled longer, we might receive data for Adam after data for Adam Sandler. If we don't save last search term in state, and validate it after data is received, we might overwrite the data for Adam Sandler by the Adam data.

Here's the demo (also see sandbox):

Problem

How can we fix it?

  • debounce the search input. It can help with most cases, but we still might get into trouble if previous requests take longer then timeout passed to debounce
  • update search term in collection state, and validate if it matches the term inside the action that is dispatched with received data. That solution is correct, but also requires maintenance and is prone to bugs.
  • Change takeEvery to takeLatest. If we have only one collection, that's perfectly fine solution. However, if we have two, like in our case, we will end up cancelling the workers for collection1 by workers for collection2. Here's the problem with takeLatest: (sandbox)

Take latest problem

takeLatestBy

This package tries to solve the issue by providing takeLatestBy effect. It works similar to takeLatest, in a sense that it will cancel previous task if it's already running. Difference is, we are grouping task by key that we provide. So when defined like that:

import takeLatestBy from 'redux-saga-take-latest-by';

yield takeLatestBy(
  FETCH_FROM_COLLECTION,
  fetchFromCollection,
  action => action.collectionId
)

It makes sure that we are running only one fetch task per collection id.

Take a look at the demo: (sandbox)

Solution