From 0c1f80197130cade40baaefe20a90d963c6a9156 Mon Sep 17 00:00:00 2001 From: Matthew Huntington Date: Sat, 8 Feb 2025 17:30:08 -0500 Subject: [PATCH] watch works with arrays --- index.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index b18ccdf..7892f3a 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ const watch = (target, callback)=>{ for(const key in target){ - if(typeof target[key] === 'object'){ + if(typeof target[key] === 'object'){ target[key] = watch(target[key], callback) } } @@ -9,7 +9,11 @@ const watch = (target, callback)=>{ { set: (target, prop, newValue) => { target[prop] = newValue + if(Array.isArray(target) && prop === 'length'){ + return true + } callback() + return true } } ) @@ -20,13 +24,16 @@ const state = watch( name: 'matt', friend: { name: 'bob' - } + }, + stuff: ['bag', 123, true] }, () => { console.log(state); } ) -//state.name = 'Matthew' +console.log(state); +state.name = 'Matthew' state.friend.name = 'Bilbo' -state.name = 'Matthew' +state.stuff[0] = 'fun' +state.stuff.push('weeee')