Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
TS/JS/Node - Transparent Observation of a Pseudo 3rd Party Class

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

  • Pros
    • Can intercept anything that happens on the proxied class including mutations
  • Cons
    • Does not copy private fields and any methods that reference a private field will error out the program

Get/Set

  • Pros
    • Does not need to clone methods and can therefore play nice with private fields
  • Cons
    • Only works on assignment and does not intercept any mutations that might happen on a property

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!