Created
September 8, 2019 00:06
-
-
Save lifeart/d6237e0129e603b23d1aa8e8d4b07504 to your computer and use it in GitHub Desktop.
GlimmerX autotracked objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeTracked(obj) { | |
function shouldDeepTrack(value) { | |
return typeof value === 'object' && value !== null; | |
} | |
Object.keys(obj).forEach((key)=>{ | |
const value = obj[key]; | |
tracked(obj, key); | |
if (shouldDeepTrack(value)) { | |
obj[key] = makeTracked(value); | |
} else { | |
obj[key] = value; | |
} | |
const desc = Object.getOwnPropertyDescriptor(obj, key); | |
let set = desc.set; | |
desc.set = function(value) { | |
if (shouldDeepTrack(value)) { | |
return set.call(this, makeTracked(value)); | |
} else { | |
return set.call(this, value); | |
} | |
} | |
Object.defineProperty(obj, key, desc); | |
}); | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment