easy-watch
Super simple dirty checker inspired by @尤小右's vue. BTW, vue is really awesome^^!
You may think of this as some kind of black magic, but Object.defineProperty
is part of standard. And it's faster to go in this way than AngularJS
's $digest
cycle.
Installation
npm install --save easy-watch
Usage
Import
commonjs
var Watch = EasyWatch;
ES2015(ES6)
;
script
Watch Object
var userInfo = name: 'leftstick' age: 31; var watcher = userInfo; watcher; ;
Watch Array
var cities = 'Beijing' 'Shanghai'; var watcher = cities; watcher; ;
Unsubscribe
var obj = name: 'Beijing';var watcher = obj; var unsubscribe = watcher; ; ;
Due to limitation of current JavaScript implementation, it's hard to detect if new property added to the object, or property removed from object. So we need extra method to handle those scenario
add property to Object via $set
var userInfo = {}; var watcher = userInfo; watcher; ;
remove property from Object via $remove
var userInfo = name: 'Shanghai'; var watcher = userInfo; watcher; ;
Run Unit-tests
I wrote some unit-tests to verify the functionality. In case you want to see how they passed, all test cases placed in test
folder, and execute following command would perform the test.
npm test