Created
January 19, 2018 05:18
-
-
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.
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
'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