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

@paysera/redux-state-restore

v1.0.3

Published

Provides tools to persist current redux state to local storage and reinitialize it based on requirements

Downloads

1

Readme

@paysera/redux-state-restore Travis CI Coverage Status

Provides tools to persist redux state to local storage and restore it based on specific entity in future user sessions for redux applications. Library does this by leveraging localforage behind a web-worker to persist and load state, while higher order reducer and provided react higher order component allows to control the flow.

Installation

Require the package npm i @paysera/redux-state-restore

Configuration

To differentiate between the branches we have to pass the storage configuration. To make the configuration passing easy we leverage factory functions which ensures that correct reducer ties to specific storage.

This configuration is consisted of object, namely:

export default storeConfig = {
    storeName: 'my-redux-branch-storage',
    errors: true,
    normalizeToStorage: ({ addresses, phones }) => ({ addresses, phones }),
    normalizeFromStorage: (state, loadedState) => ({ ...state, ...loadedState }),
};
  • storeName required parameter, initiates local storage under this name and uses it tie actions to specific storage.
  • errors optional, when turned on will dispatch and action which you can listen for if error occurs when loading state from storage.
  • normalizeToStorage optional, use when you need to change state before it's saved into storage. For example, want to avoid storing huge images or save only part of the state. Gets state as single argument.
  • normalizeFromStorage optional, use when you need to modify or patch up part of the state on the current state. Get's redux state as first and state from storage as it's second argument. Always return complete state to ensure state integrity.

Reducer:

import { combineReducers } from 'redux';
import { createStorageReducer } from '@paysera/redux-state-restore';
import storageConfig from '../storageConfig';

const withStorageReducer = createStorageReducer(storageConfig);

const reducers = {
    foo: withStorageReducer(fooReducer),
    baz: bazReducer,
};

export default combineReducers(reducers);

Same goes for our library specific actions and provided HOC:

Actions:

import { createRemoveAction } from '@paysera/redux-state-restore';
import { createLoadAction } from '@paysera/redux-state-restore';
import { createSaveAction } from '@paysera/redux-state-restore';
import { createReportErrorAction } from '@paysera/redux-state-restore';
import storageConfig from '../storageConfig';

const save = createSaveAction(storageConfig);
const load = createLoadAction(storageConfig);
const remove = createRemoveAction(storageConfig);

// namely, for usage with handleActions function provided by redux-actions library
const reportError = createReportErrorAction(storageConfig);

export {
    save,
    load,
    remove,
    reportError,
};

HOC:

import { withStorageItemIdentifier } from '@paysera/redux-state-restore';
import MyModalComponent from '../MyModalComponent';
import storageConfig from '../storageConfig';

export default withStorageItemIdentifier(storageConfig)(MyModalComponent);

resolveItemIdentifier

import { createItemIdentifierResolver } from '@paysera/redux-state-restore';
import storageConfig from '../storageConfig';

const resolveItemIdentifier = createItemIdentifierResolver(storageConfig);

Usage:

Saving:

Dispatch saveAction to save state to storage. Provide identifier as it's payload. Value can be falsy, in this case library will assign one of a kind identifier which is handled automatically by it if you leverage provided HOC.

Remove:

Dispatch removeAction to remove state from storage. Provide identifier as it's payload. Removes not persisted entity when provided with falsy value.

Loading:

Dispatch loadAction to load state from storage. Provide identifier as it's payload. Value should not be falsy.

Higher Order Component:

While it's possible to leverage all actions provided without provided HOC, it should be a lot easier to do so with it. Especially if you have to differentiate between persisted entities and ones which were never submitted by user.

Wrap your component with it. And provide identifier prop which can be string|number or falsy value. Like id which can be null if it wasn't persisted yet.

import { withStorageItemIdentifier } from '@paysera/redux-state-restore';
import MyModalComponent from '../MyModalComponent';
import storageConfig from '../storageConfig';

const StorageRestoreDialogue = withStorageItemIdentifier(storageConfig)(MyModalComponent);

function ExampleParent(props) {
    // ...code
    const { entity: { id } } = props;

    return (
        <StorageRestoreDialogue
            identifier={id}
            {...props}
        />
    );
}

The component will only render if something of relevance according to provided identifier was found in storage. It will pass down identifier prop for the item wrapped by it. It's best to dispatch loadAction and removeAction actions here.

resolveItemIdentifier:

Sometimes Higher Order Component may not be sufficient, for example when not using React or for separate item exits in storage check. When provided with entities identifier, resolver will resolve relating storage item identifier by provided entity's id. Argument can be falsy or string|number. Returns a Promise which resolves to string if related item exists or null if there is none.

Unit testing:

Because library uses a web worker, it does not work in node environment as is. You'll have to mock the entire module.