8000 GitHub - asahi7/watcher: A JS watcher implementation for objects, arrays, literals
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
/ watcher Public

A JS watcher implementation for objects, arrays, literals

Notifications You must be signed in to change notification settings

asahi7/watcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JS Watcher

Overview

A JS watcher implementation for objects, arrays, literals

If you need to catch every change made on your JS variables, this library is for you.

Install

npm i js-watcher in your npm project directory.

The npm package is located here

Run

proxy = require('js-watcher')
a = {a:'1', b:[1,2,3]}
p = proxy.createProxy(a, { valueChangeCallback: () => {console.log('hello')} })
p.a = 2

Will print: hello

proxy = require('js-watcher')
b = {a:{o:[1,2,3]}}
x = proxy.createProxy(b, {}, (prev, next, path) => { console.log(prev + ' -> ' + next + '; path: ' + path)})
x.a.o[0] = -1

Will print: 1 -> -1; path: a.o[0]

If you want to prevent assignment of the same value to the object, you can use comparison in shouldComponentUpdate method.

proxy = require('js-watcher')
b = {a:{o:[1,2,3]}}
x = proxy.createProxy(b, {}, (prev, next, path) => { if(prev === next) return false; console.log('hello') })
x.a.o[0] = 1
x.a.o[0] = -1

Will print (once): hello

More

To test the implementation run: npm test

More examples on usage of this library is available at test/proxyTest.js file.

About

A JS watcher implementation for objects, arrays, literals

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0