Skip to content

Instantly share code, notes, and snippets.

View mweststrate's full-sized avatar
💭
I may be slow to respond.

Michel Weststrate mweststrate

💭
I may be slow to respond.
View GitHub Profile
@mweststrate
mweststrate / action.js
Last active May 24, 2016 20:28
MobX store with actions
export class ContactStore {
@observable contacts = [];
@observable pendingRequestCount = 0;
@computed get isLoading() {
return this.pendingRequestCount > 0;
}
@action createRandomContact() {
this.pendingRequestCount++;
@mweststrate
mweststrate / intercept.js
Last active May 25, 2016 19:36
Intercept example
const theme = observable({
backgroundColor: "#ffffff"
})
intercept(theme, "backgroundColor", change => {
if (!change.newValue) {
// ignore attempts to unset the background color
return null;
}
if (change.newValue.length === 6) {
@mweststrate
mweststrate / setstate.js
Last active June 15, 2016 19:20
Example where React.setState causes issues
class Select extends React.Component {
constructor(props, context) {
super(props, context)
this.state = {
selection: props.values[0]
};
}
render() {
return (
@mweststrate
mweststrate / document-shim.js
Created July 4, 2016 19:36 — forked from developit/document-shim.js
Minimal stand-in for a full DOM. Mocks out basic DOM 1 node and attribute manipulation.
var document = global.document = createDocument();
export default document;
function createDocument() {
const NODE_TYPES = {
ELEMENT_NODE: 1,
ATTRIBUTE_NODE: 2,
TEXT_NODE: 3,
CDATA_SECTION_NODE: 4,
ENTITY_REFERENCE_NODE: 5,
@mweststrate
mweststrate / observe.js
Last active July 16, 2016 14:49
Object.observe is dead. Long live Mobservable.observe
// JSBin: http://jsbin.com/kekoli/edit?js,console
import {observable, observe} from "mobx";
const person = observable({
firstName: "Maarten",
lastName: "Luther"
});
const disposer = observe(person, (change) => {
console.log(`${change.type} '${change.name}' from '${change.oldValue}' to '${change.object[change.name]}'`);
@action performLogin(username, password, callback) {
this.fetch(`/json/${username}-${password}.json`)
.then(user => {
this.currentUser = user
callback(true)
})
.catch(err => {
callback(false)
})
}
@mweststrate
mweststrate / create-observables.ts
Last active November 18, 2016 20:28
MobX 3: creating observables
// 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:
@mweststrate
mweststrate / tracked-mobx.js
Created March 30, 2017 06:32
tracked-mobx.js
/**
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;
import { types } from 'mobx-state-tree'
export const Todo = types.model(
'Todo',
{
title: types.string,
finished: false,
},
{
toggle() {
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 }