Skip to content

Instantly share code, notes, and snippets.

View splincode's full-sized avatar
💬
печатает...

Максим Иванов splincode

💬
печатает...
View GitHub Profile
import { HmrInitAction, HmrBeforeDestroyAction } from '@ngxs/hmr-plugin';
@State({ ... })
export class MyState {
@Action(HmrInitAction)
public hmrInit(ctx: StateContext, { payload }) {
ctx.setState({ ... })
}
@Action(HmrBeforeDestroyAction)
@State<AnimalsStateModel>({
name: 'animals',
defaults: {
zebra: {
food: [],
name: 'zebra'
},
panda: {
food: [],
name: 'panda'
const fiz = { foo: 'bar' };
const baz = fiz;
baz.foo = 'qux'; // mutate field 'foo'
// because the references are equal
console.log(baz.foo); // qux
console.log(fiz.foo); // qux
/*
You have to understand that each object
has a unique identity in JavaScript
console.log(new Object() === new Object());
*/
console.log({} === {}); // false
@Component({
selector: 'my-app',
template: `
<b>default</b>: {{ todos$ | async | json }} <br>
<b>mutable reverse</b>: {{ mutableTodos$ | async | json }} <br>
<b>immutable reverse</b>: {{ immutableTodos$ | async | json }} <br>
`
})
export class AppComponent {
@State<number[]>({
name: 'todos',
defaults: [1, 2, 3]
})
export class AppState {
@Selector() public static mutableTodos(state: number[]): number[] {
return state.reverse();
}
<b>default</b>: {{ todos$ | async | json }} <br>
<b>immutable reverse</b>: {{ immutableTodos$ | async | json }} <br>
<b>mutable reverse</b>: {{ mutableTodos$ | async | json }} <br>
@NgModule({
imports: [
BrowserModule,
NgxsModule.forRoot([ AppState ], { developmentMode: !env.production })
],
...
})
export class AppModule { }
const animals = {
zebra: {
food: ['leaves', 'bark'],
name: 'zebra'
},
panda: {
food: [],
name: 'panda'
}
};
/*
INCORRECT CHANGES (deep mutation)
*/
const animals = {
zebra: {
food: ['leaves', 'bark'],
name: 'zebra'
},
panda: {