watch works with arrays

main
Matthew Huntington 11 months ago
parent 7ac2232f39
commit 0c1f801971

@ -1,6 +1,6 @@
const watch = (target, callback)=>{ const watch = (target, callback)=>{
for(const key in target){ for(const key in target){
if(typeof target[key] === 'object'){ if(typeof target[key] === 'object'){
target[key] = watch(target[key], callback) target[key] = watch(target[key], callback)
} }
} }
@ -9,7 +9,11 @@ const watch = (target, callback)=>{
{ {
set: (target, prop, newValue) => { set: (target, prop, newValue) => {
target[prop] = newValue target[prop] = newValue
if(Array.isArray(target) && prop === 'length'){
return true
}
callback() callback()
return true
} }
} }
) )
@ -20,13 +24,16 @@ const state = watch(
name: 'matt', name: 'matt',
friend: { friend: {
name: 'bob' name: 'bob'
} },
stuff: ['bag', 123, true]
}, },
() => { () => {
console.log(state); console.log(state);
} }
) )
//state.name = 'Matthew' console.log(state);
state.name = 'Matthew'
state.friend.name = 'Bilbo' state.friend.name = 'Bilbo'
state.name = 'Matthew' state.stuff[0] = 'fun'
state.stuff.push('weeee')

Loading…
Cancel
Save