These are chat archives for jdubray/sam

Apr 2018
Gunar Gessner
Apr 17 2018 01:57
Has anyone documented some possible pitfalls in implementing SAM with MobX? Will start developing an App and I'm trying to avoid dead-ends.
Janne Siera
Apr 17 2018 17:46
Hey everyone, I really really recommend this book called 'Signals and Systems' (pdf available here: Give it a look if you can find some time
Fred Daoud
Apr 17 2018 18:10
@jannesiera :question:
Daniel Neveux
Apr 17 2018 20:06
@gunar Mobx fits well for SAM.
  • wrap your model in Mobx.Observable
  • write mutations in Mobx.Transaction
  • write State function with Mobx.reaction/Mobx.autorun
  • define your ControlState and your view with Mobx.computed
Daniel Neveux
Apr 17 2018 20:23
The main pitfaill is to have your State function not running after a mutation.
With basic implementation with Mobx you have to explicitly say to the state function on what model field mutation it has to react on.
For example if you write the State function with autorun/reaction you will have to access all the model field you want to aware of. If not you State function won't be triggered after a model mutation.
For example
const model = observable({
  firstname: "foo",
  lastname: "bar",
  age: 13,
  wallet: {
    bitcoin: 3,
    ether: 8,
    euro: 30

// mutations

// State function
const stateFunction = autorun(function() {
  // View
    name: `${model.firstname} ${model.lastname}`, // <=  will react to any mutation to firstname or lastname
    age: model.age // <=  will react to any mutation to age
    // <=  won't react to mutation on wallet
  // NAP
Daniel Neveux
Apr 17 2018 20:36
A more handy method is to write the State function as a simple function called after each mutation.
So in the mobx world it means that you will have to wrap the Mobx.transaction in another function that will end by calling the State function
function mutation(acceptor: Function) {
  return function() {
    return transaction(name, acceptor).apply(this, arguments);
    stateFunction(model) // triggers state computation after each step
Daniel Neveux
Apr 17 2018 20:43
and a mutation could be
const updateAge = mutation(function(payload) {
  model.age = payload
Fred Daoud
Apr 17 2018 20:45
Whew, it's alarming to have so much fundamental code tied up to a specific library API.
Daniel Neveux
Apr 17 2018 20:51
Well, the question was on mobx so...
Daniel Neveux
Apr 17 2018 21:01
On a more personal opinion, I did not find a lot of advantage to implement SAM with Mobx except one : use the State Function to derivate the model into a observable react store.