Pyxus/fray

Proposal: Add means of sharing data between states within a hierarchy.

Pyxus opened this issue · 0 comments

Pyxus commented

Problem

Currently, sharing data among states within a hierarchy is cumbersome. If user-created states depend on specific information in order to function, then users are forced to manually provide this data during instantiation; this can result in a lot of boilerplate code.

Approach 1 - Read-only context propagating from root.

A possible approach to this is having a context dictionary which exists on the root. The dictionary would be read-only to prevent states from unpredictably modifying it. The dictionary would be passed to the State._ready_impl() method allowing user states to read from the context when added to the hierarchy.

Approach 2 - Shared mutable store that exists on the root.

Another approach is creating a shared store dictionary which exists on the root. The store would be private with methods available to read and update it.

Thoughts

Personally, I lean toward approach 1. Approach 2 would allow store values to be updated, which also facilitates a form of communication between states but I'm not entirely sure if that's useful or desirable. I can also see it being potentially error prone if states depend on reading from the store but the user isn't careful with how they modify it. Approach 1 avoids those issues by forcing the user to initialize the context with all the data their states require. Ignoring typos, if a state fails to fetch some data there is only one place that needs to be checked and that's when the context is initialized.

Update: It just occurred to me that if users truly wished to share mutable data between states in approach 1 they could easily provide an object all states interact with in the context. I further lean toward approach 1.

Neither approach is particularly difficult to implement. I'm open to hearing any thoughts on this including alternative solutions.