Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
property-watch-decorator
Advanced tools
Readme
@OnChange
decorator that can be easily used to listen to changes of class properties.npm install property-watch-decorator
class PersonComponent {
// Parameter value is inferred as any
// Parameter change is optional, and inferred as SimpleChange<any>
@OnChange(function(value, change) {
console.log(`name is changed from ${change.previousValue} to ${value}`);
})
name: string;
}
class PersonComponent {
// Parameter value is inferred as string
// Parameter change is optional, and inferred as SimpleChange<string>
@OnChange<string>(function(value, change) {
console.log(`name is changed from ${change.previousValue} to ${value}`);
})
name: string;
}
this
if you want to access other member of the class (just for better IDE integration)class PersonComponent {
@OnChange<string>(function(this: PersonComponent, value, change) {
console.log(`name is changed from ${change.previousValue} to ${value}`);
console.log(`At the moment, age is ${this.age}`)
})
name: string;
age: number;
}
this
as in example 3)class PersonComponent {
@OnChange<string>('onNameChange')
name: string;
age: number;
onNameChange(value, change) {
console.log(`name is changed from ${change.previousValue} to ${value}`);
console.log(`At the moment, age is ${this.age}`);
}
}
Arrow function should be avoided as this would make the function lose context. In this case, this
would NOT refer to class instance but undefined
For example: it is WRONG to use this way
class MyComponent {
@OnChange(value => {
console.log(`property1 is changed to ${value}`);
console.log(this.property1) // "this" would refer to undefined, cannot access "property1" of undefined
})
property1: any;
}
Change arrow function to es5 function:
class MyComponent {
@OnChange(function(value) {
console.log(`property1 is changed to ${value}`);
console.log(this.property1) // "this" would refer to component instance
})
property1: any;
}
Callback function CANNOT be referred to class method, this would also cause this
to be undefined
For example:
class MyComponent {
@OnChange(this.someFunction) // "this" would refer to undefined, cannot access "someFunction" of undefined
property1: any;
someFunction(value) {
console.log(`property1 is changed to ${value}`);
console.log(this.property1)
}
}
class MyComponent {
@OnChange('someFunction')
property1;
someFunction(value) {
console.log(`property1 is changed to ${value}`);
}
}
class MyComponent {
@OnChange(someFunction)
property1;
}
function someFunction(value) {
console.log(`property1 is changed to ${value}`);
}
FAQs
A decorator for watching property change
The npm package property-watch-decorator receives a total of 4,188 weekly downloads. As such, property-watch-decorator popularity was classified as popular.
We found that property-watch-decorator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.