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

121
Views
Activar un evento cuando se agrega un objeto a un mapa o conjunto

Estoy buscando una forma de activar un evento cuando se agrega un objeto a un mapa o conjunto. Algo así, ¿es posible? En este momento solo estamos usando un temporizador para verificar.

 let mymap = new Map() // or Set() mymap.on('set', obj => { console.log(obj) }

Este es mi primer pase basado en las recomendaciones de @PsychoX, ¿pensamientos?

 const EventEmitter = require('events') const emitter = new EventEmitter() const Wrapper = () => { this._set = new Set() this.get = key => { return this._set.get(key) } this.add = obj => { this._set.add(obj) emitter.emit('add', obj) } this.values = () => { return this._set.values() } } emitter.on('add', obj => { console.log('added', obj) }) let test = new Wrapper() test.add({hello:'world'})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede ampliar el Map predeterminado o usar un proxy.

Mi acercamiento:

 class ObservableMapChangeEvent extends Event { constructor(observable, key, oldValue, newValue) { super('change'); this.observable = observable; this.key = key; this.oldValue = oldValue; this.newValue = newValue; } } class ObservableMap extends Map { constructor(iterable) { super(iterable); this._eventTarget = new EventTarget(); } on(name, listener, options) { this._eventTarget.addEventListener(name, listener, options); } off(name, listener, options) { this._eventTarget.addEventListener(name, listener, options); } delete(key) { this._eventTarget.dispatchEvent(new ObservableMapChangeEvent(this, key, this.get(key), undefined)); super.delete(key); } set(key, value) { this._eventTarget.dispatchEvent(new ObservableMapChangeEvent(this, key, this.get(key), value)); super.set(key, value); } } // Example: const foo = new ObservableMap(); foo.on('change', event => { console.log(`Changes [${event.key}] to '${event.newValue}' (old: '${event.oldValue}')`); }); foo.set('hello', 'world'); // Changes [hello] to 'world' (old: undefined)

Es posible que desee ver:

  • Cómo extender correctamente el mapa ES6
  • ¿Hay algún EventEmitter en el lado del navegador que tenga una lógica similar en nodejs?
about 4 years ago · Juan Pablo Isaza Report
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!