I have a base abstract class that I want public properties to be able to be Observed, including mutations if that property is an object/array/etc. This base class could take on a multitude of interfaces and other properties. I do not want someone who is using this base class to have to worry about events, a pub sub, or any other means of notification; I want that all just baked in.
abstract class BaseClass {
#stores: Map<any, Function[]>
subscribe(prop: NonFunctionPropertyNames<this>, callback: (data: CallbackType)=>void){
// handle subscribe
}
unsubscribe(prop: NonFunctionPropertyNames<this>, callback: (data: CallbackType)=>void){
// handle unsubscribe
}
constructor(){
this.#stores = new Map<any, Function[]>
}
}
I have attempted this using both Proxy and get/set each with their own pros and cons.
Proxy
Get/Set
I can live with the Get/Set approach, but that does mean anyone using this base class must be sure to re-assign a prop after any mutations. Is there some other approach I am missing?