easy-observer

0.4.0 • Public • Published

Easy Observer

Observe a simple property change and trigger listener.

Build Status

Install

npm install easy-observer

Usage

Watch property which is simple value;

import { observeValue } from 'easy-observer';

let a = { name: 'Hello' };

observeValue(a, 'name', (previous, current) => console.log(`${previous} -> ${current}`));

a.name = 'World'; // => Hello -> World;

Watch function;

import { observeFn, observable } from 'easy-observer';

const a = observable({ name: 'first', age: 2 });
const b = observable({ name: 'second' });
const c = { name: 1 };
let count = 0;

observeFn(() => {
  a.name + b.name + c.name;
  count++;
}); // count = 1; Auto run once;

a.name = 'x'; // count = 2; Trigger rerun the function when observed property changed

b.name = 'xx'; // count = 3

a.age = 3; // count = 3; Not trigger when change the observed property which is not used in the function

c.name = 2; // count = 3; Not trigger when change the non-observed property

Run Test

npm install 

npm test

Readme

Keywords

Package Sidebar

Install

npm i easy-observer

Weekly Downloads

2

Version

0.4.0

License

ISC

Last publish

Collaborators

  • qlee