-
Notifications
You must be signed in to change notification settings - Fork 1
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
Does it make sense to have an Observable of the entire class? #10
Comments
@CMCDragonkai, that's an interesting idea. It's definitely possible! For this to work, we would need to create a new decorator function intended for a class, let's just call it I wrote up a quick example usage here, let me know if this is what you had in mind. @ObservedClass()
class Person {
constructor(public name: string,
public age: number) {
}
incrementAge() {
this.age += 1;
}
} Usage: const person = new Person('Garret', 26);
const sub = person.subscribe(console.log);
// output: { name: 'Garret', age: 26, [[Prototype]]: Object }
person.incrementAge();
// output: { name: 'Garret', age: 27, [[Prototype]]: Object }
person.name = 'Billy';
// output: { name: 'Billy', age: 27, [[Prototype]]: Object }
sub.unsubscribe(); |
I might also add that an implementation like this would likely be incompatible with @ObservedClass()
class A {
@Observed() propA: string;
readonly propA!$: Observable<string>;
} Since both If I recall correctly, class decorators are applied before property decorators. That being the case, making a change to |
I checked out rxdb and they have something like this:
Meaning that This way, the object's methods won't conflict with the observable interface. What are your thoughts on this? Is it better idea to encapsulate observable object into the |
Maybe if I imagine that if it was possible to create an observable class, then I'd want to be able to use property decorators to decide which properties should be be considered observable.
In this situation, we would therefore expect:
I'm not entirely sure if it makes sense to use Maybe the main idea is that it would more efficient to directly subscribe to the property rather than plucking it from the object. This is because the property may change only a few times, but if you pluck from the object, you're getting the object property every time any other properties change. |
Yes class decorators do get applied before property decorators. But there should be a way to "share" information between the class decorator and the property decorator. This could be done with an external variable in the scope of the decorators. Or private properties keyed by a unique symbol. |
With these decorators it becomes easy to create observable properties within a class.
But can the class itself be observable?
What that means is that if I subscribe to the class itself, I should get the whole entire class, all the properties of the class and any prototype methods. It'd likely be a BehaviourSubject since it would have a value as soon as the class is constructed.
The text was updated successfully, but these errors were encountered: