GithubHelp home page GithubHelp logo

pull-persist.js's Introduction

PullPersistor

PullPersistor is a utility class designed to persist the state of a pullstate store to either localStorage or sessionStorage. It helps in maintaining state across page reloads or browser sessions.

Installation

You can install PullPersistor from npm:

npm install pull-persist

Usage

Importing the Class

First, import the PullPersistor class into your project:

import PullPersistor from 'pull-persist';

Initializing the Persistor

Create an instance of PullPersistor by passing your pullstate store, a unique key for storage, and optionally, the type of storage ('LOCAL' or 'SESSION'). The default storage type is 'LOCAL'.

import { Store } from 'pullstate';
import PullPersistor from 'pull-persist';

// Define your store type
type CounterState = {
  count: number;
};

// Create a pullstate store
const counterStore = new Store<CounterState>({
  count: 0,
});

// Initialize PullPersistor with the store, a key, and storage type
const persistor = new PullPersistor<CounterState>(counterStore, 'counterState', 'LOCAL');

// Initialize the persistor to load state from storage and subscribe to changes
persistor.initialize();

Handling Restored State

You can provide an optional callback to initialize that will be called with the restored state (if any) when the persistor is initialized:

persistor.initialize((restoredState) => {
  if (restoredState) {
    console.log('State restored:', restoredState);
  } else {
    console.log('No state found in storage');
  }
});

API

PullPersistor<T>

Constructor

constructor(
  store: Store<T extends object ? T : object>,
  key: string,
  storageType: 'LOCAL' | 'SESSION' = 'LOCAL',
  storePrefix: string = 'pullstate'
)
  • store: The pullstate store to persist.
  • key: A unique key to identify the storage entry.
  • storageType: The type of storage to use ('LOCAL' for localStorage or 'SESSION' for sessionStorage). Defaults to 'LOCAL'.
  • storePrefix: A prefix for the storage key. Defaults to 'pullstate'.

Methods

initialize(onRestore?: (state: T | null) => any): void

Initializes the persistor, loading the state from storage if available and subscribing to store changes.

  • onRestore: An optional callback that receives the restored state (or null if no state was found).

Example

import { Store } from 'pullstate';
import PullPersistor from 'pull-persist';

type CounterState = {
  count: number;
};

const counterStore = new Store<CounterState>({ count: 0 });

const persistor = new PullPersistor<CounterState>(counterStore, 'counterState', 'LOCAL');

persistor.initialize((restoredState) => {
  if (restoredState) {
    console.log('State restored:', restoredState);
  } else {
    console.log('No state found in storage');
  }
});

In this example, PullPersistor is used to persist the state of a simple counter across page reloads using localStorage. The restored state is logged to the console when the persistor is initialized.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

pull-persist.js's People

Contributors

esecurtis avatar

Stargazers

Ekikere-Abasi Michael avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.