-
Notifications
You must be signed in to change notification settings - Fork 18
How to handle state X 3
First heads up: I'm proposing that we add a third scope type called "shared" for states.
So a state's scope can be
-
:instance
state exists independently for each instance -
:class
state is a class variable -
:shared
state is shared between all instances and the class (similar to rails cattr)
States that are not explicitly declared will have the scope in which they are used. I.e. if in an instance method, then its a instance state, and if its in a class method its a class state.
The only way to get a shared state is to explicitly declare it.
If you were to explicitly declare an instance state, and implicitly reference a state by the same name in a class method, you would be still talking about two different states.
For instances, you would want them because you want each instance to have its copy of any mutable data (hashes, arrays etc) otherwise all instances would be sharing the same data. The work around would be explicitly initialize the state in the instances initializer. Okay!
For class states, you would also want them because class states can be reset by broadcasting Hyperloop::Boot. Again if the state is being initialized to any mutatable structure you would want a new copy of that structure. Again there is a workaround - set the store up to receive the Hyperloop::Boot explicitly.
Okay so here is a way out with that I like: for instance states the provided initial value is dup
ed. For class (and shared) states the initial value is dup
ed on any reboots. This in a few cases may not be what you want, and if so you can fall back to the initializer / explicit Boot reciever. but it will work most of the time.
class MyStore < HyperStore::Base
def self.a_class_method
state.class_state # <- we are talking about a single state "owned" by the class
state.another_state # ditto here
end
def self.an_instance_method
state.instance_state # every instance gets a new one
state.another_state # this aint the same as the one above
self.class.state.another_state # but this one is!
end
end
The current approach is that we would have a way of declaring a state that is "shared" between in instances and classes implicitly
Thus instead of saying self.class.state.some_shared_state
you would just say state.some_shared_state
.
The way we would declare these would be to define the scope
as class
i.e. scope: :class
This seems way to confusing and implicit.
At least IMHO we should say scope: :shared