listen-proxy
v1.5.3
Published
Npm module to create proxy objects with listeners that will be fired when properties are created, changed or deleted.
Downloads
1
Maintainers
Readme
- onExampleAdd: Callback that will be fired when the example property is added.
- onExampleDelete: Callback that will be fired when the example property is deleted.
- onExampleChange: Callback that will be fired when the example property is changed.
This package was created because I wanted to test some TS features, credit must be given to Jack Herrington, since a video he did about mapped types was the inspiration for this package, I'll leave the video at the bottom of the readme.
npm install listen-proxy
yarn add listen-proxy
import { createProxy } from "listen-proxy";
const dog = {
name: 'Rex',
age: 3,
};
const dogListeners = {
onNameChange: (value, oldValue, target) => {
console.log(`The dog's name changed from ${oldValue} to ${value}`, target);
},
onAgeDelete: (oldValue) => {
console.log(`The dog's age was deleted, his age was ${oldValue}`);
},
onBreedAdd: (value, target) => {
console.log(`The dog's breed was added, his breed is ${value} his name is ${target.name}`);
},
};
const proxyDog = createProxy(dog, dogListeners);
proxyDog.name = 'Rexy'; // The dog's name changed from Rex to Rexy { name: 'Rexy', age: 3 }
delete proxyDog.age; // The dog's age was deleted, his age was 3
proxyDog.breed = 'Labrador'; // The dog's breed was added, his breed is Labrador his name is Rexy
import { Listeners } from "listen-proxy";
If you find any bugs or errors, please let me know by creating an issue here.
And if you want to contribute, please feel free to do so by opening a PR.