Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda