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
// Theme stores the state of the current theme, and allows components to subscribe to future changes | |
class Theme { | |
constructor(color) { | |
this.color = color | |
this.subscriptions = [] | |
} | |
setColor(color) { | |
this.color = color | |
this.subscriptions.forEach(f => f()) |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://unpkg.com/mobx@2.6.0/lib/mobx.umd.js"></script> | |
<script> | |
var MobxDemo = Object.create(HTMLElement.prototype); | |
MobxDemo.attachedCallback = function() { | |
var state = mobx.observable({ | |
counter : parseInt(this.getAttribute("counter")) | |
}) |
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
// Pseudo code proposal to give more fine grained control and cleaning up the way in which observables are constructed in MobX | |
// Strategies | |
Strategy.Reference = "Reference" | |
Strategy.Deep = "Deep" | |
Strategy.Shallow = "Shallow" | |
Strategy.Structure = "AsStructure" | |
// The essen of MobX: trackable boxed values: |
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; |
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
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 { 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 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 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
// 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 | |
}, {}) |