Skip to content

Instantly share code, notes, and snippets.

@lifeart
Created September 8, 2019 00:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lifeart/d6237e0129e603b23d1aa8e8d4b07504 to your computer and use it in GitHub Desktop.
Save lifeart/d6237e0129e603b23d1aa8e8d4b07504 to your computer and use it in GitHub Desktop.
GlimmerX autotracked objects
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