This file contains 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
const byId = produce((draft, action) => { | |
switch (action.type) { | |
case RECEIVE_PRODUCTS: | |
action.products.forEach(product => { | |
draft[product.id] = product | |
}) | |
break | |
} | |
}) |
This file contains 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
// ImmutableJS | |
const newMap = map.updateIn(['inMap', 'inList'], list => list.push(4)) | |
// Immer | |
draft.inMap.inList.push(4) |
This file contains 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
const byId = (state, action) => | |
produce(state, draft => { | |
switch (action.type) { | |
case RECEIVE_PRODUCTS: | |
action.products.forEach(product => { | |
draft[product.id] = product | |
}) | |
break | |
} | |
}) |
This file contains 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
// Shortened, based on: https://github.com/reactjs/redux/blob/master/examples/shopping-cart/src/reducers/products.js | |
const byId = (state, action) => { | |
switch (action.type) { | |
case RECEIVE_PRODUCTS: | |
return { | |
...state, | |
...action.products.reduce((obj, product) => { | |
obj[product.id] = product | |
return obj | |
}, {}) |
This file contains 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 produce from "immer" | |
const todos = [ /* 2 todo objects in here */ ] | |
const nextTodos = produce(todos, draft => { | |
draft.push({ text: "learn immer", done: true }) | |
draft[1].done = true | |
}) | |
// old state is unmodified |
This file contains 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 produce from "immer" | |
const nextState = produce(currentState, draft => { | |
// empty function | |
}) | |
console.log(nextState === currentState) // true |
This file contains 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 { recordPatches } from "mobx-state-tree" | |
const runningActions = new Map() | |
export function atomicAsyncPatch(call, next) { | |
switch (call.type) { | |
case "action": | |
return atomic(call, next) | |
case "process_spawn": { | |
const recorder = recordPatches(call.tree) |
This file contains 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 { getSnapshot } from 'mobx-state-tree' | |
const task1 = Todo.create({ title: "Try MST!" }) | |
task1.toggle() | |
console.dir(getSnapshot(task1)) | |
// prints { title: "Try MST!" , finished: true } |
This file contains 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 { types } from 'mobx-state-tree' | |
export const Todo = types.model( | |
'Todo', | |
{ | |
title: types.string, | |
finished: false, | |
}, | |
{ | |
toggle() { |
This file contains 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
/** | |
Shitty implementation of tracked compatible local component state library powered by MobX | |
Demo: https://jsfiddle.net/mweststrate/Ltmhe3rh/ | |
@example | |
class Example extends Component { | |
// initialize state.count with a value: | |
@tracked count = 1000; |