Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
get(key) {
const cachedDerivable = cache.get(key);
// If the cache has a hit for the current key, we know it is already connected (through another proxy).
if (cachedDerivable) {
return cachedDerivable.getState();
}
// A cache miss means no other proxy is currently connected.
const newDerivable = _internal.independentTracking(() => derivableFactory(key));
// We don't want final-value-optimalization, because that defeats the purpose of the cache. A final value
// is not registered as an observed value, which means we cannot track the usage of our newly created derivable.
// Therefore introduce a non-final atom (`atom(0)`) in the derivation:
const derivable = isSettableDerivable(newDerivable)
? lens({ get: () => newDerivable.get(), set: v => newDerivable.set(v) }, atom(0))
: atom(0).derive(() => newDerivable.get());
if (delayedEviction) {
derivable.autoCache();
}
// Get the state of our derivable early so it connects when needed.
const state = derivable.getState();
if (derivable.connected) {
derivable[CACHED_PROXY] = this;
cache.set(key, derivable);
derivable.connected$.react(() => cache.delete(key), { skipFirst: true, once: true });
}
return state;
},
set(newValue, key) {
const derivable = cache.get(key) || derivableFactory(key);
if (!isSettableDerivable(derivable)) {
throw _internal.augmentStack(new Error('Cached derivable is not settable'), derivable);
}
derivable.set(newValue);
},
};