Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to reset the state of all modules? #29

Open
pancake-boy opened this issue Jan 29, 2020 · 2 comments
Open

how to reset the state of all modules? #29

pancake-boy opened this issue Jan 29, 2020 · 2 comments
Labels
question Further information is requested

Comments

@pancake-boy
Copy link

for example, i want to clear the state on logout.
how can i do this for all modules within the store?

@bodograumann bodograumann added the question Further information is requested label Feb 11, 2020
@bodograumann
Copy link
Collaborator

Afaik there is no build-in way to reset a typescript object to its initial state.
I usually define a reset mutation and clear the state there manually.

@BonBonSlick
Copy link

BonBonSlick commented Dec 15, 2021

In functions that would be smth like this


export default {
    resetWholeState({commit}): void {
        commit(RESET_WHOLE_STATE);
    },
} as ActionTree<any, any>;

export default {
    [RESET_WHOLE_STATE](state: any): void {
        if (state.hasOwnProperty('_initialState')) {
            Object.assign(state, merge({}, state._initialState, {_initialState: state._initialState}));
        }
    },
} as MutationTree<any>;


export const defaultState = (): IStrAny => merge({}, common(), local) as IStrAny;
export default () => merge({}, defaultState(), {_initialState: defaultState()}) as IStrAny;

should give an idea how to make it with classes, smth like

abstract class ResetModule {
public _initialState: object = {};

@Action
  public  resetWholeState() : void {
    this.RESET_WHOLE_STATE();
  }

@Mutation
protected  RESET_WHOLE_STATE (){
 this = this._initialState;
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants