Function
isConst public
Defined in packages/@ember/-internals/metal/lib/cache.ts:78
import { isConst } from '@glimmer/tracking/primitives/cache'; |
Can be used to check if a memoized function is constant. If no tracked state
was used while running a memoized function, it will never rerun, because nothing
can invalidate its result. isConst
can be used to determine if a memoized
function is constant or not, in order to optimize code surrounding that
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import { tracked } from '@glimmer/tracking'; import { createCache, getValue, isConst } from '@glimmer/tracking/primitives/cache'; class State { value; } let state = new State(); let computeCount = 0; let counter = createCache(() => { // consume the state state.value; return computeCount++; }); let constCounter = createCache(() => { return computeCount++; }); getValue(counter); getValue(constCounter); isConst(counter); // false isConst(constCounter); // true |
If called on a cache that hasn't been accessed yet, it will throw an error. This is because there's no way to know if the function will be constant or not yet, and so this helps prevent missing an optimization opportunity on accident.