These are chat archives for jdubray/sam

18th
Aug 2017
Brad Jones
@bradjonesca
Aug 18 2017 15:48
Re: Cycle.js, here is an update from an interview with one of the more active Cycle developers from a year or two ago https://survivejs.com/blog/motorcycle-interview/
Janne Siera
@jannesiera
Aug 18 2017 15:54
Could anyone give me a short introduction on what a saga is and it's importance in context to redux? I'm trying to see how SAM measures up to Redux. Sam.js.org talks about this comparison but both Saga's in Redux and NAP in SAM are still a bit confusing to me.
Jean-Jacques Dubray
@jdubray
Aug 18 2017 15:57
Saga is just a function generator used to manage API calls (in general), it maintains some state and it sits at the level of NAP().
Redux alone does not manage any API call because of TimeTravel (as I understand it)
Personally I prefer executing queries in actions and create/update/delete in the Model
Daniel Neveux
@dagatsoin
Aug 18 2017 16:08

Redux can manage API calls but in several successive actions (first action => startFetching, second action => commitData)
SAM do the same (one action by step).
Mobx state tree also, Vuex also...

But all are very verbose because you have to declare actions for each loading steps.
Generators iterate between those steps in one action. There is a good explanation with mobx state tree: https://github.com/mobxjs/mobx-state-tree/blob/master/docs/async-actions.md
SAGA can be used to compose actions with less code, as JJ said it is a kind of SAMLoop for async actions.
With the SAM pattern you could write a generic action {name: FETCH, args: url} and the NAP() could react to the different states (invoke, progress, done, error, etc...) to dispatch the next action : COMMIT_DATA, DISPLAY_ERROR, STOP_SPINNER, etc...

Jean-Jacques Dubray
@jdubray
Aug 18 2017 16:11

SAM do the same (one action by step).

this is not correct, you can run a full orchestration within an action and propose the results to the model. Similarly for create/update/delete in the model

there is no need for Sagas or ancillary state machines (fetching...) in SAM
the only problem I have with MobX-State-Tree is that actions directly manipulate the model
Daniel Neveux
@dagatsoin
Aug 18 2017 16:14
Yes, I reformulate, you can to the same (step by step), or in just in one step.

the only problem I have with MobX-State-Tree is that actions directly manipulate the model

Me too, it is why I create my own framework using the SAM pattern and just Mobx.

Jean-Jacques Dubray
@jdubray
Aug 18 2017 16:32
:+1:
@jannesiera When people constrain themselves to "pure" functional programming, they tend to have difficulty dealing with asynch calls, you can't have it both ways. There is of course a class of problems which can be solved with pure functions (and Lambda Calculus), but I am not sure distributed systems are part of it.
Janne Siera
@jannesiera
Aug 18 2017 19:23
You guys mostly seem to talk about What they do, but not on How they do it. Which is fine, it's just that I don't know Redux well enough that I understand all of this. I'll have to do some secondary reading to put these comments in context.
Jean-Jacques Dubray
@jdubray
Aug 18 2017 19:31
Redux is (at a high/basic level) a simple process:
  • an action is emitted (that's what I call an event since an action is just a simple data structure)
  • this action triggers the reducer (a pure function of the state and the action)
  • the output of the function is used to update the "store" which triggers React rendering
event->update state->render
Elm is following the same model
Jean-Jacques Dubray
@jdubray
Aug 18 2017 19:39
This message was deleted
SAM fits between event and render:
         |-<- NAP() --<
         V            |
event -> A  ->  M ->  S ->render
Janne Siera
@jannesiera
Aug 18 2017 19:44
Mhm
Janne Siera
@jannesiera
Aug 18 2017 19:49
How does performing the 'whole' API call in an action break Redux' TimeTravel but not SAM's?
Jean-Jacques Dubray
@jdubray
Aug 18 2017 19:54
so there is no "action" where you can make an API call, then the reducer is a pure function, so no API call there.
You can use "thunks" and "sagas" is you want to make an API call.
in essence Redux has two definitions for actions: a data structure or a thunk
then you have sagas which are a fuzzy concept
Sagas are like a little state machine running on the side of your reducer
SAM offers a homogeneous set of semantics, there is no "use this when..."
Redux and Elm were designed to use a pure function to update the state, that's core mutation mechanism
Jean-Jacques Dubray
@jdubray
Aug 18 2017 20:17
Functions are not the basic building block of programming
https://medium.com/@copyconstruct/small-functions-considered-harmful-91035d316c29