-
-
Notifications
You must be signed in to change notification settings - Fork 311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for read-only observables #220
Comments
Are you suggesting something like a @readonly
@observable
int value; |
@pavanpodila I don't think so. If no code at all is generated for a setter then how could
|
Ok got it! This will be a custom setter then, whose name you are specifying in the annotation |
Yes, that's what I was suggesting. There may be a better way to declare a readonly observable that can be mutated within the store, that approach was the one I thought was simplest though. |
Can't you do this right now by creating an observable on a private field? class _TestStore with Store {
@observable(setter: '_setValue')
int _value = 0;
int get value => _value;
@action
void incrementValue() => _value++;
} |
@shyndman that seems to work for me, you don't need the setter param though (it doesn't exist) class _TestStore with Store {
@observable
int _value = 0;
int get value => _value;
@action
void incrementValue() => _value++;
} |
It would be cool if class _TestStore with Store {
@readonly
@observable
int _value = 0;
@action
void incrementValue() => _value++;
} Or even better class _TestStore with Store {
@observable(readonly: true)
int _value = 0;
@action
void incrementValue() => _value++;
} |
Adding a @readonly annotation would be great, meaning a public getter and a private setter are generated, so the value is observable and only mutable from inside the store. |
If this feature is really desirable, I suggest we upvote the issue. That way we can prioritize on what we pick up next. |
@eltonomicon what your thoughts about the ObservableList, I see that this approach is good for simple values, link string or int, but for a complex class, that expose methods that allow changing of itself, how do you think that we could still user ObservableList but only with inside store modifications. |
@AlbertoMonteiro you can that complex class be a Store itself. I do this in a bunch of places today. |
Sorry @MisterJimson but I think that you didn't understand what I said. |
@pavanpodila this should be closed. |
Why is it limited to private fields? I don't see a scenario, when this could be used, unless you put store and widget in the same file
To achieve desired functionality, one has to declare private observable and public computed getter like that:
Would be so much better to have option to do it merely with
---------UPD--------- |
I'd like to define stores where observables are only mutated by actions and cannot be mutated externally of the store. This currently is not possible because the code generator defines get/set properties for observables. It would be particularly useful to define read-only observable ObservableList properties since you usually never call the setter once it's initialized.
I know one option is to make private observables then provide read-only computed properties for the observables, but a) that seems unnecessarily verbose, and b) it's not really computed, they're just simple accessors.
For comparison, here's a simple example of how it currently works.
Here's one suggestion of an alternate approach.
I think the ObservableList case as just a read-only property with the default @observable annotation should be sufficient, in that case just generate the getter but not the setter.
The text was updated successfully, but these errors were encountered: