JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q42548F
  • License MIT

A library for managing data flows and changing state.

Package Exports

  • reactive-function

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (reactive-function) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

reactive-function

A library for managing data flows and changing state.

Usage

If you are using NPM, install this package with:

npm install reactive-function

Require it in your code like this:

var ReactiveFunction = require("reactive-function");

This library is designed to work with reactive-property, you'll need that too.

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

Suppose you have two reactive properties to represent someone's first and last name.

var firstName = ReactiveProperty("Jane");
var lastName = ReactiveProperty("Smith");

Suppose you'd like to have another property that represents the full name of the person.

var fullName = ReactiveProperty();

You could set the full name value like this.

fullName(firstName() + " " + lastName());

However, this sets the value of fullName only once, and it does not get updated when firstName or lastName change.

Here's how you can define a reactive function that updates the full name whenever the first name or last name changes.

ReactiveFunction({
  inputs: [firstName, lastName],
  output: lastName,
  callback: function (first, last){
    return first + " " last;
  }
});

This defines a "reactive function" that will be invoked when its inputs (firstName and lastName) are both defined and whenever either one changes. The function will be invoked on the next tick of the JavaScript event loop after it is defined and after any dependencies change.

To force a synchronous evaluation of all reactive functions whose dependencies have updated, you can call

ReactiveFunction.digest();

Now you can access the computed value of the reactive function by invoking it as a getter.

console.log(fullName()); // Prints "Jane Smith"

For more detailed example code, have a look at the tests.

Related work: