From a5c1a735e1a48a2f04e96b21bb5c2102f211586f Mon Sep 17 00:00:00 2001 From: Matthew Huntington Date: Sat, 8 Feb 2025 12:03:09 -0500 Subject: [PATCH] basic deep watch --- index.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index d2c905f..1121023 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,23 @@ -const watch = (value, callback)=>{ - return new Proxy(value, { set: callback }) +const watch = (target, callback)=>{ + for(const key in target){ + if(typeof target[key] === 'object'){ + target[key] = watch(target[key], callback) + } + } + return new Proxy(target, { set: callback }) } -const state = watch({ name: 'matt' }, (newValue) => { - console.log(newValue); -}) +const state = watch( + { + name: 'matt', + friend: { + name: 'bob' + } + }, + (target, prop, newValue) => { + console.log(newValue); + } +) -state.name = 'Matthew' +//state.name = 'Matthew' +state.friend.name = 'Bilbo'