ngneat / elf

🧙‍♀️ A Reactive Store with Magical Powers

Home Page:https://ngneat.github.io/elf/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create registerPreStateInit hook

ivalexandr opened this issue · comments

Discussed in #381

Originally posted by ivalexandr November 19, 2022
Hello. Why does the documentaion say that state in Elf is immutable? After all, we can change it without calling update method.
Can we change this behavior and somehow protect from mutations the object obtained from the store?

Add a new registerPreStoreInit hook that takes the provided initial state and returns it.

import { elfHooks, deepFreeze } from '@ngneat/elf';

if (!environment.production) {
  elfHooks.registerPreStateInit((initialState, storeName) => {
    return deepFreeze(initialState);
  });
}

@ivalexandr, you're welcome to create a PR. It should be straightforward.