0.2.0 • Published 5 years ago
@hogbros/observe-property v0.2.0
observeProperty
Wire in to property changes on an object
Installation
npm install @hogbros/observe-property --save
Basic Usage
import { observeProperty } from "@hogbros/observe-property";
const myObject = {
myObservedProperty: "foo";
};
observeProperty(
myObject,
"myObservedProperty",
(target, oldValue, newValue) => console.log(
`myObservedProperty changed from ${oldValue} to ${value}`
)
);
myObject.myObservedProperty = "bar";
// console: myObservedProperty changed from foo to bar
Experimental Decorators
import { onSet } from "@hogbros/observe-property";
class MyClass {
@onSet((target, oldValue, newValue) =>
console.log(`myObservedProperty changed from ${oldValue} to ${value}`)
)
myObservedProperty?: string;
}
let myObject = new MyClass();
myObject.myObservedProperty = "foo";
// console: myObservedProperty changed from undefined to foo
myObject.myObservedProperty = "bar";
// console: myObservedProperty changed from foo to bar
How it Works
observeProperty will dynamically create a property getter/setter on the specified object. The setter on this property will invoke a callback method to notify of any updates on the property.