This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@State<AnimalsStateModel>({ | |
name: 'animals', | |
defaults: { | |
zebra: { | |
food: [], | |
name: 'zebra' | |
}, | |
panda: { | |
food: [], | |
name: 'panda' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = produce(INITIAL_DATA, draft => { | |
draft.items[NEW_ITEM_ID] = { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 } | |
draft.counter++ | |
draft.keys.push(NEW_ITEM_ID) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = compose( | |
over(lensPath(["keys"]), append(NEW_ITEM_ID)), | |
over(lensPath(["count"]), x => x + 1), | |
set(lensPath(["items", NEW_ITEM_ID]), { | |
id: NEW_ITEM_ID, | |
name: "ITEM-NEW", | |
value: 0 | |
}) | |
)(INITIAL_DATA); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = { | |
...INITIAL_DATA, | |
items: { | |
...INITIAL_DATA.items, | |
[NEW_ITEM_ID]: { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 } | |
}, | |
count: INITIAL_DATA.count +1, | |
keys: [ ...INITIAL_DATA.keys, NEW_ITEM_ID] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@State<AnimalsStateModel>({ | |
name: 'animals', | |
defaults: { | |
zebra: { | |
food: ['leaves', 'bark'], | |
name: 'zebra' | |
}, | |
panda: { | |
food: [], | |
name: 'panda' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@State<AnimalsStateModel>({ | |
name: 'animals', | |
defaults: { | |
zebra: { | |
food: ['leaves', 'bark'], | |
name: 'zebra' | |
}, | |
panda: { | |
food: [], | |
name: 'panda' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { State, Selector, StateContext, Action } from '@ngxs/store'; | |
import { ImmutableSelector, ImmutableContext } from '@ngxs-labs/immer-adapter'; | |
export class Add { | |
static type = 'Add' | |
constructor(public payload: number) {} | |
} | |
@State<number[]>({ | |
name: 'todos', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'my-app', | |
template: ` | |
<b>default</b>: {{ todos$ | async | json }} <br> | |
<b>immutable reverse</b>: {{ todosReverse$ | async | json }} | |
` | |
}) | |
export class AppComponent implements OnInit { | |
@Select(AppState) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deepCopy(obj) { | |
return JSON.parse(JSON.stringify(obj)); // serialize -> deserialize -> unique clone | |
} | |
function copy(obj) { | |
let clone = Array.isArray(obj) ? [] : {}; | |
for (let key in obj) { | |
let value = obj[key]; | |
clone[key] = (typeof value === "object") ? copy(value) : value; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deepCopy(obj) { | |
return JSON.parse(JSON.stringify(obj)); // serialize -> deserialize -> unique clone | |
} | |
const original = { | |
a: new Date(), | |
b: NaN, | |
c: new Function(), | |
d: undefined, | |
e: function(){}, |