
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
reactive-property
Advanced tools
A small library for getter-setter functions that react to changes.
The pattern for creating reusable data visualizations described in Towards Reusable Charts is great. However, the boilerplate code for getter-setter functions is a bit cumbersome. This library creates chainable getter-setter functions so you don't have to. For more information, see "Introducing reactive-property" on Medium.
Here's a code example from Towards Reusable Charts showing the general pattern.
function chart() {
var width = 720, // default width
height = 80; // default height
function my() {
// generate chart here, using `width` and `height`
}
my.width = function(value) {
if (!arguments.length) return width;
width = value;
return my;
};
my.height = function(value) {
if (!arguments.length) return height;
height = value;
return my;
};
return my;
}
Here's what the above code can look like when using reactive-property:
function chart() {
function my() {
// generate chart here, using `my.width()` and `my.height()`
}
my.width = ReactiveProperty(720);
my.height = ReactiveProperty(80);
return my;
}
Pros:
Cons:
my.width()
instead of simply width
).If you are using NPM, install the library by running this command.
npm install reactive-property
Require it in your code like this.
var ReactiveProperty = require("reactive-property");
You can also require the script in your HTML like this.
<script src="//datavis-tech.github.io/reactive-property/reactive-property-v0.9.0.js"></script>
Or use the minified version.
<script src="//datavis-tech.github.io/reactive-property/reactive-property-v0.9.0.min.js"></script>
Create a property.
var a = ReactiveProperty();
Set its value.
a(5);
Get its value.
a();
Set up method chaining by using a context object.
var my = {
x: ReactiveProperty(5),
y: ReactiveProperty(10)
};
my.x(50).y(100);
You can also set up properties this way.
var my = {};
my.x = ReactiveProperty(5);
my.y = ReactiveProperty(10);
my.x(50).y(100);
Listen for changes. The callback function will be invoked synchronously when the property value is set. The callback will be passed the current value as the first argument and the previous value as the second argument (may be undefined
if the value was not previously set).
a.on(function(value, oldValue){
console.log("The value of 'a' changed from " + oldValue + " to " + value);
});
Cancel your subscription.
function listener(){
console.log("'a' changed!");
}
a.on(listener);
a.off(listener);
a(5); // The listener is NOT called.
For convenenience, the listener function is returned from the call to on
, so the following would also work.
var listener = a.on(function (){
console.log("'a' changed!");
});
a.off(listener);
a(5); // The listener is NOT called.
In case you know you won't be using a property anymore and want to be sure to avoid memory leaks, you can remove all listeners with the destroy()
function like this.
a.destroy();
That covers the entire API. For more detailed example code, have a look at the tests.
A reactive property is similar to KnockoutJS Observables and RxJS Observables.
I hope you enjoy and benefit from this project!
If you appreciate this Open Source work, please consider supporting me on Patreon or tip me with ChangeTip.
You can also hire me as a consultant, please reach out with inquiries at curran@datavis.tech.
FAQs
A small library for getter-setter functions that react to changes.
We found that reactive-property 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.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.