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

ngrx-6-store-ionic-storage

v1.0.2

Published

Simple storage syncing between @ngrx/store and @ionic/storage.

Downloads

29

Readme

ngrx-store-ionic-storage

Simple syncing between @ngrx 4 and Ionic Storage.

Dependencies

This library depends on the store and effects modules from @ngrx/platform and Ionic 2.

Installation

Make sure you have a scaffolded Ionic 2 app. This library supports Ionic version 2 and above. For more details, see the Ionic documentation.

Then run:

npm install @ngrx/core @ngrx/store @ngrx/effects @ionic/storage ngrx-store-ionic-storage --save

Next, make sure you have installed the cordova-sqlite-storage plugin. This allows Ionic Storage to use the most optimal storage mechanism available, depending on the target device.

ionic cordova plugin add cordova-sqlite-storage --save

Usage

  1. In your app's module, import the StorageSyncEffects and run it through the EffectsModule.
  2. Pass options into the storageSync function to create a meta-reducer, and compose it with your other reducers.

Here is an example with two app-specific reducers, books and collection, and a state object appState.

import { NgModule } from '@angular/core';
import { StoreModule, ActionReducerMaps, ActionReducer, MetaReducer } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';
import { StorageSyncEffects, storageSync } from 'ngrx-store-ionic-storage';
import { BookActions, CollectionActions } from './actions';
import { booksReducer, collectionReducer } from './reducers';
import { appState } from './app-state'

export function onSyncError(err) {
  console.log(err);
}

export const reducers: ActionReducerMap<appState> = {
  books: booksReducer,
  collection: collectionReducer
};

export const storageSyncReducer = storageSync({
  keys: ['collection'],   // Only sync the `collection` state
  ignoreActions: [        // Don't sync when these actions occur
    BookActions.SELECT,
    CollectionActions.FILTER,
  ],
  hydratedStateKey: 'hydrated', // Add this key to the state
  onSyncError: onSyncError      // If a sync fails
});

export function storageMetaReducer(reducer: ActionReducer<any>): ActionReducer<any, any> {
  return storageSyncReducer(reducer);
}

export const metaReducers: MetaReducer<any, any>[] = [storageMetaReducer];

@NgModule({
  imports: [
    StoreModule.forRoot(reducers, { 
      metaReducers,
      initialState: {
        hydrated: false
      }
    })
    EffectsModule.forRoot([ StorageSyncEffects ])
  ]
})
export class MyAppModule {}

Options

  • keys: specify the portion of your state to sync to the underlying storage mechanism.
  • ignoreActions: don't sync whenever any of these actions are fired.
  • hydratedStateKey: if present, the sync reducer will add a flag to your app state when the initial hydration has completed. You can bind an observable to this flag, and so be notified when the app has been hydrated.
  • onSyncError: a callback, called whenever there was an error syncing the state. The callback is passed a single err parameter.

If main reducer is an ActionReducerMap

hydrated.reducer.ts

import { Action } from '@ngrx/store';

export interface HydratedState {}

export function reducer(state: boolean = false, action: Action): State {
    return state;
};

main.reducer.ts

import { ActionReducerMap } from '@ngrx/store';
import * as fromHydrated from './hydrated.reducer';

export interface State {
    hydrated: fromHydrated.State
    //...
}

export const reducer: ActionReducerMap<State> = {
    hydrated: fromHydrated.reducer
    //...
};

How It Works

The sync reducer is a meta-reducer. On startup, it hydrates the initial state from the underlying storage mechanism. Then, whenever an action is dispatched to the store, the sync reducer saves the portion of state to the underlying storage mechanism, after all other reducers have run.

The sync reducer will only store the portion of state provided in the keys option, and will not run for any actions that are specified in the ignoreActions option.

Why?

Much of this library is based on ngrx-store-localstorage. While this is an excellent choice for desktop web browsers, for Ionic apps a better solution exists for local storage: Ionic Storage. This provides a more robust storage mechanism, depending on the device where the app is running. Since Ionic Storage is asynchronous, the functionality needed to be written from the ground-up to support an async get/set operation.

License

MIT