Dynamic Children
When traditional supervisors start services, they usually use a static list of children. A dynamic service can’t depend on a list of children, so we’ll need to provide an API instead. We’ll also need to be able to refer to each of the children by name. Remember, supervisors are about lifecycles. Our plan is to build an API to add_character, and another to stop a character. Then we’ll touch up the rest of the functions to refer to GenServers by name.
We’ll need to start with the functions we’ve identified: init, start_link, and child_spec. Not surprisingly, these functions all have lifecycle names. Tweaking them to support both dynamic and static services becomes pretty easy once we get Elixir’s pattern matching involved.
Tweak ...
Get Programmer Passport: OTP now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.