2016-03-15 7 views

Antwort

2

Vielleicht ein Proxy verwendet, ist eine Option, obwohl es das ursprüngliche Objekt zu ersetzen, benötigt

const { Subject } = require('rxjs'); 

// Take an object, and return a proxy with an 'observation$' stream 
const toObservableObject = targetObject => { 
    const observation$ = new Subject(); 
    return new Proxy(targetObject, { 
     set: (target, name, value) => { 
      const oldValue = target[name]; 
      const newValue = value; 
      target[name] = value; 
      observation$.next({ name, oldValue, newValue }); 
     }, 

     get: (target, name) => name == 'observation$' ? observation$ : target[name] 
    }); 
} 

const observableObject = toObservableObject({ }); 

observableObject.observation$ 
    .filter(modification => modification.name == 'something') 
    .subscribe(({ name, oldValue, newValue }) => console.log(`${name} changed from ${oldValue} to ${newValue}`)); 

observableObject.something = 1; 
observableObject.something = 2; 

Der Ausgang

something changed from undefined to 1 
something changed from 1 to 2 

Geben Sie für Proxy in der Kompatibilitätstabelle aktuellen Knoten Versionen hat die volle Unterstützung) https://kangax.github.io/compat-table/es6/

und Dokumentation des Proxy bei https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy