/
index.js
46 lines (33 loc) · 961 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const Watcher = require('./lib/watcher');
const walk = require('./lib/walk');
// initial state
const user = { first: 'Kenny', last: 'Fellows' };
// setup the state with getters and setters
walk( user );
const callback = ( newVal, oldVal ) => {
console.log( 'Old: %s, New: %s', oldVal, newVal );
};
// create a watcher w/ an expression and a callback
new Watcher( () => user.first, callback );
// change the state
user.first = 'Des';
user.first = 'Kevin';
/*****************************************************/
// Pretty example
function DesVue() {
return {
computed: {
fullName() {
return `${ user.first } ${ user.last }`;
}
}
}
}
const component = DesVue();
// Turn each 'computed' property into a Watcher
Object.entries( component.computed ).forEach( ( [ key, val ] ) => {
new Watcher( val, () => console.log( `${ key }: ${ val() }` ) );
});
// 🎉 🎉 🎉
user.first = 'Kenny';
user.last = 'Loggins';