Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Created July 11, 2019 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboyd/067825793cab8987816153cd63573cac to your computer and use it in GitHub Desktop.
Save cowboyd/067825793cab8987816153cd63573cac to your computer and use it in GitHub Desktop.
A function to execute a transaction on a microstate substate.
let next = over(inventory.item.price, price => price.add(2).add(2).add(2));
import { create, valueOf } from 'microstates';
export default function over(microstate, fn) {
let Type = typeOf(microstate);
let value = valueOf(microstate);
let updated = fn(create(Type, value));
return microstate.set(valueOf(updated));
}
// this will probably become part of the public API of
// microstates at some point.
function typeOf(microstate) {
return microstate.constructor.Type;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment