Skip to content

Instantly share code, notes, and snippets.

@chrispsn
Created January 19, 2018 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrispsn/efca7dca0108f5178b8c49c89086ffb3 to your computer and use it in GitHub Desktop.
Save chrispsn/efca7dca0108f5178b8c49c89086ffb3 to your computer and use it in GitHub Desktop.
Mesh may cache results of sheet computations in a private property of the object literal.
'use strict';
const ROOT = {
get _cache() {
// These two lines let the proxy get access to the sheet
const calc = this;
delete calc._cache;
return (calc._cache = new Proxy({}, {
get(c, k) {
// TODO how does speed compare to try/catch?
// What we'll actually use (below is verbose for debug):
// return (k in c) ? c[k] : (c[k] = calc[k])
if (k in c) {
console.log("Cached")
return c[k];
} else {
console.log("Calculated and stored");
return (c[k] = calc[k])
}
}
}));
},
get some_val() {
const sheet = this._cache;
return sheet.hardcode;
},
hardcode: 123,
}
console.log(ROOT.some_val);
console.log(ROOT.some_val);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment