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

reapex-module-dataloader

v1.2.2

Published

reapex dataloader module

Downloads

3

Readme

Reapex dataloader module

register the module

import { App } from 'reapex'
import dataloader from 'reapex-module-dataloader'

const app = new App()
// 1. register the module
export const { useDataLoader, useLazyDataLoader } = app.use(dataloader)

Use react hooks

const LoaderWithHook = () => {
  const loaderStatus = useDataLoader({
    name: 'api2',
    apiCall: mockApi,
  })

  if (loaderStatus.loading) {
    return <div>loading...</div>
  }
  if (loaderStatus.error) {
    return <div>Error!!!</div>
  }
  return <div>{loaderStatus.data ? loaderStatus.data : 'No Data!'}</div>
}

Lazy load

const LoaderWithHook = () => {
  const [loaderStatus, load] = useLazyDataLoader({
    name: 'api2',
    apiCall: mockApi,
  })

  return <button onClick={() => load()}>click to load</button>
}

API

DataLoaderProps: The parameter of useDataloader hook function

| Property | Description | Type | Default | Required | | --- | --- | --- | --- | --- | | name | The key of the data stored in redux state, has to be unique if dataKey is not provided | string | - | Yes | | apiCall | A function that returns promise | (params?: TPramas) => Promise<any> | - | Yes | | interval | Fetch data in an interval if given a none 0(ms) number | boolean | 0 | No | | params | The parameters that passed to apiCall function | TParams = any | undefined | No | | dataKey | Function that compute a dynamic key based on params | (name: string, params?: TParams) => string | () => 'default' | No | | ttl | How much time the cache will valid before making a new data fetching, default 0, no cache. The apiCall function will be called every time | number | 0 | No | | shouldInterval | A function the returns true/false to determine whether the interval function call should continue or not | (data?: TData) => boolean | () => true | No | | onSuccess | A function will get called when apiCall run successfully | (data?: TData) => any | - | No | | onFailure | A function will get called when apiCall throw an exception | (error?: Error) => any | - | No | | dataPersister | An object that configures how to persist the data | DataPersister | - | No | | lazyLoad | if dataPersister is configured, it will first use the data from persister then call apiCall to refresh the data | boolean | - | No |

useDataLoader() hook

useDataLoader: <TData = any, TParams = any>(props: DataLoaderProps) => [LoaderStatus<TData>, LoadActionCreator]

props: DataLoaderProps

The props: DataLoaderProps are defined in the table above.

LoaderStatus

| Property | Description | Type | | --- | --- | --- | | data | The data that reuturned by apiCall | TData | | loading | true when data is loading, otherwise false | boolean | | error | An Error object if apiCall threw exception | Error | | lastUpdateTime | The timestamp of last time when receiving the data from apiCall | number | undefined| | lastErrorTime | The timestamp of last time whenapiCallthrew an exception |number | undefined |

LoadActionCreator

load: (params?: TParams) => any

A function to call with params which will trigger the apiCall

DataPersister

export interface DataPersister {
  getItem: (key: string, meta?: Meta) => any
  setItem: (key: string, value: any, meta?: Meta) => any
  removeItem: (key: string, meta?: Meta) => any
}

For example, localStorage data persister:

export const LocalStorageDataPersister = (): DataPersister => {
  const getItem = (key: string) => {
    const data = localStorage.getItem(key)
    return data && JSON.parse(data)
  }

  const setItem = (key: string, value: any) => {
    localStorage.setItem(key, JSON.stringify(value))
  }

  const removeItem = (key: string) => {
    localStorage.removeItem(key)
  }

  return {
    getItem,
    setItem,
    removeItem,
  }
}