Created
July 11, 2019 20:04
-
-
Save cowboyd/067825793cab8987816153cd63573cac to your computer and use it in GitHub Desktop.
A function to execute a transaction on a microstate substate.
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
let next = over(inventory.item.price, price => price.add(2).add(2).add(2)); |
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 { 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