wires-reactive
TypeScript icon, indicating that this package has built-in type declarations

1.1.32 • Public • Published

Build Status IHateReact

Wires Reactive

A unique library that watches string template changes and provides a very helpful toolset for watching javascript objects. It combines 3 libraries:

  • async-watch (to watch variables asynchronously)
  • extract-vars (to extract watchable variables)
  • wires-angular-expressions
import {Watch} from "wires-reactive";
let context = { scope: { user: { name: "Bob" }}}
Watch.template(context, "Hello {{user.name}}!", (str) => {
   // Hello Bob!
});

TRY IT LIVE

How it works

wires-reactive uses async-watch, which adds a very unique and efficient feature to the browser - triggers changes when the browser is actually ready to paint. If you are not familiar with it yet - go ahead, and check it out, this micro solution is unique.

We extract variables from an expression using a very efficient library extract-vars It costs nothing for the browser - non RegEx approach and a tiny size. Besides you can precompile your templates beforehand! Interested how it works> Check a very simple and comprehensive test suite!

We evaluate expressions using angular-expression. Which, unfortunately, is the only options for now. (It understands locals, others - don't)

Features

  • Template expression watch
  • Expression watch (will trigger changes when affected variable is changed)
  • Automatically extracts variables from expressions (intelligent and smart enough to extract only variables that can be possible watched)
  • Written in typescript

Install

npm install wires-reactive

If you want to have it in browser, you have to use your own build, or use include universal files.

inpatient to try it out? clone this repository and just run gulp test-build and open .browser-test-helper/test.html in you favourite browser!

Watch.template

Watches a template, precompiles if necessary. You can pass locals to the context as well.

let context = {
    scope: {
        user: {
            name: "Bob"
        }
    }
}
Watch.template(context, "Hello {{user.name}}! We love you {{user.name}}", (str) => {
   // Hello Bob! We love you Bob
});

Any asynchronous change will trigger a callback. For example

setTimeout(()=> {
  context.scope.user.name = "Richard";
},1)

Please not, that any synchronous call within the current trick won't trigger anything. This behaviour is intentional.

procompileTemplate

In order to speed things up, you can precompile a template

precompileString("Hello {{user.name}}! We love you {{user.name}}")
// Precompiled string:
// [ 'Hello ', [ 'user.name', [ 'user.name' ] ], ', we love you ', [ 'user.name', [ 'user.name' ] ] ]

Watch.expression

Watches an expression, context is the same, should consist of scope and locals (optional)

Watch.expression(context, "{ active : user.name === 'Bob' }", function(result) {
    // {active : true}
});

precompileExpression

Precompile your expressions to make it blazing fast!

precompileExpression("{ active : user.name === 'Bob'"})

I you like this idea, please, don't forget to star the repository. If you think that JSX is the worst what happened to web development, contact me personally via window.atob("c2t5cGU6bmNoYW5nZWQ="), i'll buy you a beer.

Also, we have started a gitter channel (link above)

Readme

Keywords

none

Package Sidebar

Install

npm i wires-reactive

Weekly Downloads

2

Version

1.1.32

License

ISC

Last publish

Collaborators

  • nchanged