All Projects → natural-apptitude → ngrx-store-ionic-storage

natural-apptitude / ngrx-store-ionic-storage

Licence: MIT license
Simple syncing between @ngrx/store and Ionic Storage.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to ngrx-store-ionic-storage

ionic2-PreDB
Simple Ionic 2+ with pre-populated database starter project
Stars: ✭ 14 (-78.79%)
Mutual labels:  ionic, ionic2
ionic3
This repository contains complete source code for Ionic 3 tutorial from https://ampersandacademy.com/tutorials/ionic-framework-3. I will try many Ionic 3 specific scripts and will write them as separate tutorial. You can follow this repo to get more tested and working script for the Ionic 3.
Stars: ✭ 21 (-68.18%)
Mutual labels:  ionic, ionic2
Ion Digit Keyboard V2
A digital keyboard plugin to use in Ionic 2 applications.
Stars: ✭ 97 (+46.97%)
Mutual labels:  ionic, ionic2
Ionic2 Tutorial Pouchdb
Tutorial: How To Use PouchDB + SQLite For Local Storage In Ionic 2
Stars: ✭ 56 (-15.15%)
Mutual labels:  ionic, ionic2
Ionic2 Rating
⭐️ Angular star rating bar. Built for Ionic 2+.
Stars: ✭ 177 (+168.18%)
Mutual labels:  ionic, ionic2
Ion Affix
A directive for Ionic framework for creating affix headers.
Stars: ✭ 58 (-12.12%)
Mutual labels:  ionic, ionic2
Ionic2 Reddit Reader
Ionic 2 Sample App
Stars: ✭ 128 (+93.94%)
Mutual labels:  ionic, ionic2
Ionic2 Calendar
A calendar component based on Ionic framework
Stars: ✭ 338 (+412.12%)
Mutual labels:  ionic, ionic2
Ionic2 Pokedex
🎮 Pokédex sample app developed with Ionic 2, Angular 2 and Apache Cordova. Using Pokéapi as source for data.
Stars: ✭ 143 (+116.67%)
Mutual labels:  ionic, ionic2
Ionic3 Multilevelsidemenu
Ionic 3 demo of a two-level side menu.
Stars: ✭ 141 (+113.64%)
Mutual labels:  ionic, ionic2
Awesome Ionic
An "awesome" list of Ionic resources
Stars: ✭ 799 (+1110.61%)
Mutual labels:  ionic, ionic2
Wooionic3
An eCommerce App for WooCommerce stores using Ionic 3.
Stars: ✭ 208 (+215.15%)
Mutual labels:  ionic, ionic2
Ion2 Calendar
📅 A date picker components for ionic2 /ionic3 / ionic4
Stars: ✭ 537 (+713.64%)
Mutual labels:  ionic, ionic2
Ionic2 City Picker
ionic2的省市区三级联动组件
Stars: ✭ 95 (+43.94%)
Mutual labels:  ionic, ionic2
Ionic3 Chat
ionic3 chat example
Stars: ✭ 465 (+604.55%)
Mutual labels:  ionic, ionic2
Chihu2
ionic2-example <吃乎2>混合开发-美食app 🍜 ☕️ 🍦 (This is a support android and apple ionic2 case, a food app)
Stars: ✭ 124 (+87.88%)
Mutual labels:  ionic, ionic2
ionic-parallax
Parallax Module for Ionic Framework 2+
Stars: ✭ 10 (-84.85%)
Mutual labels:  ionic, ionic2
Ionic Boilerplate
✨ An Ionic Starter kit featuring Tests, E2E, Karma, Protractor, Jasmine, Istanbul, Gitlab CI, Automatic IPA and APK, TypeScript 2, TsLint, Codelyzer, Typedoc, Yarn, Rollup, and Webpack 2
Stars: ✭ 309 (+368.18%)
Mutual labels:  ionic, ionic2
Ionic3 Components
A project full of ionic 3 components and samples - to make life easier :)
Stars: ✭ 1,689 (+2459.09%)
Mutual labels:  ionic, ionic2
Growth Ionic
[v2.0 DEPRECATED, please update to Growth 3.0] Growth - App to help you Be Awesome Developer & Awesome Hacker
Stars: ✭ 2,200 (+3233.33%)
Mutual labels:  ionic, ionic2

ngrx-store-ionic-storage

Simple syncing between @ngrx 7 and Ionic Storage.

If you are looking to use this package with an Ionic 2 or 3 app, please use version 4 of this package.

Dependencies

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

Installation

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

Then run:

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

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

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].