ng-turkey / ngxs-reset-plugin

Reset plugin for NGXS: Effortlessly clears, resets, or overwrites NGXS states respecting the state tree.
https://stackblitz.com/edit/ngxs-pizza
MIT License
108 stars 8 forks source link
angular clear defaults ngxs overwrite plugin reset state-management

NGXS Reset Plugin

This plugin is designed to clear, reset, and overwrite NGXS states on dispatch of predefined actions. Please check this playground for a working example.

Please use the table below to make sure you are using the correct version of this library.

Angular
version
ngxs-reset-plugin
version
18+ 4
14+ 3
13 2
12 2
< 12 1

If you wonder why you would need a plugin like this, when there is already a reset method in NGXS, you may find the reasons below. Otherwise, skip to the installation or usage section.

Reasons to Use This Plugin

Calling the reset method of NGXS Store like store.reset(myNewStateObject) can:

Drawbacks/missing features of the reset method of NGXS Store are:

Reset plugin improves all these because it:

How the concept of meta reducers related to all this is:

Installation

Run the following code in your terminal:

yarn add ngxs-reset-plugin

or if you are using npm:

npm install ngxs-reset-plugin

Usage

Setup Before Initial Use

Import withNgxsResetPlugin into your app config like:

import { withNgxsResetPlugin } from 'ngxs-reset-plugin';

export const appConfig: ApplicationConfig = {
  providers: [
    providerStore([]),
    withNgxsResetPlugin()
  ]
}

How to Clear States

To clear all states (on logout for example):

this.store.dispatch(
  new StateClear()
);

To clear all states but one: *

this.store.dispatch(
  new StateClear(SomeState)
);

To clear all states but some: *

this.store.dispatch(
  new StateClear(SomeState, SomeOtherState)
);

* Keeping states while deleting others is useful especially combined with @ngxs/storage-plugin

How to Reset States to Defaults

To reset a single state to its defaults on certain events (such as route change):

this.store.dispatch(
  new StateReset(SomeState)
);

To reset multiple states to their defaults (may prove useful in distributed scenarios):

this.store.dispatch(
  new StateReset(SomeState, SomeOtherState)
);

How to Reset All States to Defaults

To reset all states to their defaults:

this.store.dispatch(
  new StateResetAll()
);

To reset all states to their defaults but keep one:

this.store.dispatch(
  new StateResetAll(SomeState)
);

To reset all states to their defaults but keep some:

this.store.dispatch(
  new StateResetAll(SomeState, SomeOtherState)
);

Important Note: Since it does not know anything about the actual states, StateResetAll resets the states based on InitState action dispatched by NGXS at init. If you are persisting states with @ngxs/storage-plugin and want them to get reset as well, you need to reset those persisted states with StateReset instead. Otherwise, next time the app starts, the storage plugin will put them in the store before InitState and StateResetAll will reset to initial states from storage and not to the original defaults of those states.

How to Overwrite States

To replace the value of a single state with a new one: *

// type OverwriteTuple = [StateClass, any];

this.store.dispatch(
  new StateOverwrite([SomeState, newStateValue])
);

To replace the value of multiple states with new ones: *

// type OverwriteTuple = [StateClass, any];

this.store.dispatch(
  new StateOverwrite([SomeState, newStateValue], [SomeOtherState, newOtherStateValue])
);

* Overwriting states with improper values can break your application. Proceed with caution.

Roadmap