Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve types to reflect key-making functions may return undefined #627

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,30 @@ export type OptimisticWrapperFunction<
// then it matters that .dirty takes TKeyArgs instead of TArgs.
dirty: (...args: TKeyArgs) => void;
// A version of .dirty that accepts a key returned by .getKey.
dirtyKey: (key: TCacheKey) => void;
dirtyKey: (key: TCacheKey | undefined) => void;

// Examine the current value without recomputing it.
peek: (...args: TKeyArgs) => TResult | undefined;
// A version of .peek that accepts a key returned by .getKey.
peekKey: (key: TCacheKey) => TResult | undefined;
peekKey: (key: TCacheKey | undefined) => TResult | undefined;

// Completely remove the entry from the cache, dirtying any parent entries.
forget: (...args: TKeyArgs) => boolean;
// A version of .forget that accepts a key returned by .getKey.
forgetKey: (key: TCacheKey) => boolean;
forgetKey: (key: TCacheKey | undefined) => boolean;

// In order to use the -Key version of the above functions, you need a key
// rather than the arguments used to compute the key. These two functions take
// TArgs or TKeyArgs and return the corresponding TCacheKey. If no keyArgs
// function has been configured, TArgs will be the same as TKeyArgs, and thus
// getKey and makeCacheKey will be synonymous.
getKey: (...args: TArgs) => TCacheKey;
getKey: (...args: TArgs) => TCacheKey | undefined;

// This property is equivalent to the makeCacheKey function provided in the
// OptimisticWrapOptions, or (if no options.makeCacheKey function is provided)
// a default implementation of makeCacheKey.
makeCacheKey: (...args: TKeyArgs) => TCacheKey;
// a default implementation of makeCacheKey. This function is also exposed as
// optimistic.options.makeCacheKey, somewhat redundantly.
makeCacheKey: (...args: TKeyArgs) => TCacheKey | undefined;
Comment on lines 87 to +91
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this optimistic.makeCacheKey function could/should be removed in v1.0, since it's redundant with optimistic.options.makeCacheKey, but I didn't want any breaking changes in this PR.

};

export { CommonCache }
Expand All @@ -110,7 +111,7 @@ export type OptimisticWrapOptions<
// The makeCacheKey function takes the same arguments that were passed to
// the wrapper function and returns a single value that can be used as a key
// in a Map to identify the cached result.
makeCacheKey?: (...args: NoInfer<TKeyArgs>) => TCacheKey;
makeCacheKey?: (...args: NoInfer<TKeyArgs>) => TCacheKey | undefined;
// If provided, the subscribe function should either return an unsubscribe
// function or return nothing.
subscribe?: (...args: TArgs) => void | (() => any);
Expand Down Expand Up @@ -200,8 +201,8 @@ export function wrap<
cache,
});

function dirtyKey(key: TCacheKey) {
const entry = cache.get(key);
function dirtyKey(key: TCacheKey | undefined) {
const entry = key && cache.get(key);
if (entry) {
entry.setDirty();
}
Expand All @@ -211,8 +212,8 @@ export function wrap<
dirtyKey(makeCacheKey.apply(null, arguments as any));
};

function peekKey(key: TCacheKey) {
const entry = cache.get(key);
function peekKey(key: TCacheKey | undefined) {
const entry = key && cache.get(key);
if (entry) {
return entry.peek();
}
Expand All @@ -222,8 +223,8 @@ export function wrap<
return peekKey(makeCacheKey.apply(null, arguments as any));
};

function forgetKey(key: TCacheKey) {
return cache.delete(key);
function forgetKey(key: TCacheKey | undefined) {
return key ? cache.delete(key) : false;
}
optimistic.forgetKey = forgetKey;
optimistic.forget = function forget() {
Expand All @@ -233,7 +234,7 @@ export function wrap<
optimistic.makeCacheKey = makeCacheKey;
optimistic.getKey = keyArgs ? function getKey() {
return makeCacheKey.apply(null, keyArgs.apply(null, arguments as any));
} : makeCacheKey as (...args: any[]) => TCacheKey;
} : makeCacheKey as (...args: any[]) => TCacheKey | undefined;

return Object.freeze(optimistic);
}
Loading