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

loadable-data

v1.0.1

Published

Loadable data for redux thunk

Downloads

3

Readme

Loadable Data

Description

This library provides primitives for removing boilerplate code when you use redux-thunk

By default, if you want to create async action you need to create three primitive actions and one composite action:

import { PayloadAction, createSlice } from '@reduxjs/toolkit';

import { Dispatch } from 'redux';
import { get } from './api';

export type Data = any;

export type State = {
  loadingState: 'INIT' | 'LOADING' | 'SUCCESS' | 'FAILED';
  data?: Data;
  error?: Error;
};

export const data = createSlice({
  name: 'data',
  initialState: {
    loadingState: 'INIT',
  } as State,
  reducers: {
    isLoading: (state: State) => {
      state.loadingState = 'LOADING';
      state.data = undefined;
      state.error = undefined;
    },
    hasLoaded: (state: State, action: PayloadAction<Data>) => {
      state.loadingState = 'SUCCESS';
      state.data = action.payload;
      state.error = undefined;
    },
    hasFailed: (state: State, action: PayloadAction<Error>) => {
      state.loadingState = 'FAILED';
      state.data = undefined;
      state.error = action.payload;
    },
  },
});

export const { isLoading, hasLoaded, hasFailed } = data.actions;

export const loadData = () => async (dispatch: Dispatch) => {
  dispatch(isLoading());
  try {
    const data = await get();
    dispatch(hasLoaded(data));
  } catch (e) {
    dispatch(hasFailed(e));
  }
};

export default data.reducer;

With Loadable Data you need to have only one action:

import {
  LoadableData,
  LoadableDataEvent,
  init,
  isLoading,
  success,
  updateLoadableData,
} from 'loadable-data';
import { PayloadAction, createSlice } from '@reduxjs/toolkit';

import { Dispatch } from 'redux';
import { get } from './api';

export type Data = any;

export type State = {
  data: LoadableData<Data>;
};

export const data = createSlice({
  name: 'data',
  initialState: {
    data: init(),
  } as State,
  reducers: {
    processData: (state: State, action: PayloadAction<LoadableDataEvent<Data>>) => {
      state.data = updateLoadableData(state.data, action.payload);
    },
  },
});

export const { processData } = data.actions;

export const loadData = () => async (dispatch: Dispatch) => {
  dispatch(processData(isLoading()));
  try {
    const data = await get();
    dispatch(processData(success(data)));
  } catch (e) {
    dispatch(processData(fail(e)));
  }
};

export default data.reducer;

API

LoadableData

export type Init<T> = {
  state: "init";
  data?: T;
};

export type IsLoading<T> = {
  state: "isLoading";
  data?: T;
};

export type Success<T> = {
  state: "success";
  data: T;
};

export type Failed<T> = {
  state: "failed";
  data?: T;
  failure: Error;
};

export type LoadableData<T> = Init<T> | IsLoading<T> | Success<T> | Failed<T>;

LoadableDataEvent

export type IsLoadingEvent<T> = {
  state: "isLoading";
  storeDate: boolean;
};

export type SuccessEvent<T> = {
  state: "success";
  data: T;
};

export type FailedEvent<T> = {
  state: "failed";
  storeDate: boolean;
  failure: Error;
};

export type LoadableDataEvent<T> =
  | IsLoadingEvent<T>
  | SuccessEvent<T>
  | FailedEvent<T>;

updateLoadableData

export declare function updateLoadableData<T>(
  previousState: LoadableData<T>,
  event: LoadableDataEvent<T>
): LoadableData<T>;

LoadableDataEvent helpers

export declare function isLoading<T>(storeDate?: boolean): IsLoadingEvent<T>;
export declare function success<T>(data: T): SuccessEvent<T>;
export declare function failed<T>(failure: Error, storeDate?: boolean): FailedEvent<T>;

Configuration

By default, updateLoadableData removes old data on isLoading and failed events.

You can change this behaviour by adding flag storeDate for isLoading and failed events:

export const loadData = () => async (dispatch: Dispatch) => {
  dispatch(processData(isLoading(true)));
  try {
    const data = await get();
    dispatch(processData(success(data)));
  } catch (e) {
    dispatch(processData(fail(e, true)));
  }
};