Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

reactive-property

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reactive-property

A small library for getter-setter functions that react to changes.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

ReactiveProperty

A simple library that generates getter-setters.

This abstracts the getter-setter pattern described in Towards Reusable Charts - Mike Bostock (2012);

Here are the tests that demonstrate the functionality of this library:

var ReactiveProperty = require("reactive-property");
var assert = require("assert");

describe("Getter-setters", function() {

  it("Should construct a property.", function () {
    var a = ReactiveProperty();
    assert.equal(typeof(a), "function");
    assert.equal(typeof(a()), "undefined");
  });

  it("Should construct a property with a default value and get the value.", function () {
    var a = ReactiveProperty(5);
    assert.equal(a(), 5);
  });

  it("Should set and the value.", function () {
    var a = ReactiveProperty();
    a(10);
    assert.equal(a(), 10);
  });

  it("Should set and the value, overriding the default value.", function () {
    var a = ReactiveProperty(5);
    a(10);
    assert.equal(a(), 10);
  });

});

In addition to setting and getting values, you can also listen for changes using the on function.

describe("Reacting to changes", function (){

  it("Should react to changes.", function (done) {
    var a = ReactiveProperty();
    a.on(function (){
      assert.equal(a(), 10);
      done();
    }); 
    a(10);
  });

  it("Should react to the default value.", function (done) {
    var a = ReactiveProperty(5);
    a.on(function (){
      assert.equal(a(), 5);
      done();
    }); 
  });

  it("Should not react to no value.", function () {
    var a = ReactiveProperty();
    var numInvocations = 0;
    a.on(function (){ numInvocations++; }); 
    assert.equal(numInvocations, 0);
  });

  it("Should react synchronously.", function () {
    var a = ReactiveProperty();
    var numInvocations = 0;
    a.on(function (){ numInvocations++; }); 
    assert.equal(numInvocations, 0);
    a(10);
    assert.equal(numInvocations, 1);
    a(15);
    assert.equal(numInvocations, 2);
  });

  it("Should stop reacting to changes.", function () {
    var a = ReactiveProperty();
    var numInvocations = 0;
    var listener = a.on(function (){ numInvocations++; }); 
    a(10);
    assert.equal(numInvocations, 1);
    a.off(listener);
    a(15);
    assert.equal(numInvocations, 1);
  });

  it("Should pass the value to the listener as an argument.", function (done) {
    var a = ReactiveProperty();
    a.on(function (value){
      assert.equal(value, 10);
      done();
    }); 
    a(10);
  });
});

I hope you enjoy this library! Feel free to open GitHub issues if you have any questions, comments or suggestions.

Keywords

FAQs

Package last updated on 21 Feb 2016

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc