Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda