Hacker News new | ask | show | jobs
by iudqnolq 1884 days ago
Don't do this unless you've got some experience with alternatives. It's definitely not the standard in the Elixir community.

Also keep in mind OTP (the Erlang stdlib) is sort of one company's kitchen sink helper lib. It's got some gems, but do read other people's opinions about which are worth using. I wish it's docs included more about when not no use it.

Edit:

Definitely look up "contexts". They're the Phoenix word for module that handle storing/retrieving a piece of data.

If you're writing an app with low perf requirements consider using the db directly for this with no in memory persistence. I did this with a low usage internal dashboard and it's was dead simple.

Edit2: Thanks to reply, previously said contexts were an Elixir concept

1 comments

Contexts are Phoenix specific. They are not the standard of the elixir community.

Gen_statem is erlang standard, for state machines

https://hexdocs.pm/gen_state_machine/GenStateMachine.html is a a fine wrapper for that

If u have finite states I would go for a state machine

Contexts [1] are not a standard nor Phoenix specific. All my Elixir projects that deal with data have contexts.

Context is the practice of creating a public API for your (database) models, opposite to having your controllers access directly your DB and database objects. Gives you better testability, better isolation, better code architecture.

So instead of (in pseudocode):

     def change_password(user_id, new_password):
       user = User.db.get(id=user_id, is_active=True)
       user.set_password(hash(new_password))
       user.save()
       send_password_changed_email(user)
       
       render(password_changed.html, user)
you'd do:

    def change_password(user_id, new_password):
      # The Accounts module hides all the complexity and implementation details
      user = Accounts.get_user_by_id(user_id)
      Accounts.change_user_password(user, new_password)
      
      render(password_changed.html, user)
That's really just it. It's a best practice which is prominent in the Ecto and Phoenix docs, but not necessarily applicable to those libraries only, or to Elixir only in fact.

1: https://hexdocs.pm/phoenix/contexts.html

Thanks for the correction in contexts. I still wouldn't recommend state machines to a newcomer.
i only mentioned it cause of the use of Redux, which in couple areas very similar to a state machine.

state,events, transitions as the parent said, are very well buildable with state machines, since thats basically what they explicitly are.

and since we are talking about games, it would be more logical to use than, contexts, loading/saving state to a db is just secondary nature of a game, its more about the state transitions and events of player input and game logic.