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

Allow cache.set(x, undefined) to delete the cached item #26

Merged
merged 9 commits into from
Nov 22, 2020
Merged

Allow cache.set(x, undefined) to delete the cached item #26

merged 9 commits into from
Nov 22, 2020

Conversation

cheap-glitch
Copy link
Collaborator

@cheap-glitch cheap-glitch commented Sep 23, 2020

This is a very basic fix for the issue #25 discovered in refined-github/refined-github#3589.

Fixes #25

index.ts Show resolved Hide resolved
Co-authored-by: Federico <me@fregante.com>
index.ts Outdated Show resolved Hide resolved
@fregante fregante added the enhancement New feature or request label Sep 25, 2020
@fregante
Copy link
Owner

Thanks for the PR! This is a breaking change and also something that I had initially specifically avoided doing, so I'll probably wait just in case there's a reason not to do it.

@cheap-glitch
Copy link
Collaborator Author

Thanks for the PR! This is a breaking change and also something that I had initially specifically avoided doing, so I'll probably wait just in case there's a reason not to do it.

Maybe because it means cache.set('myKey') actually unsets the key instead of setting it, which can arguably be confusing?

@fregante
Copy link
Owner

I think the reasoning was that undefined could not be saved, because undefined is meant as "missing", even though .set is very well capable of storing undefined because it wraps it in an object first.

But also, yes, both .set('a') and .set('a', undefined) deleting 'a' are kinda weird.

I guess the former more than the latter. The first could throw an error though, at least to avoid using .set('a') by mistake instead of .get('a')

@fregante
Copy link
Owner

Can you add a arguments.length check so that it throws a TypeError when you call .set('a')?

@cheap-glitch
Copy link
Collaborator Author

Can you add a arguments.length check so that it throws a TypeError when you call .set('a')?

Done. Also added a test to cover the new case.

index.ts Outdated Show resolved Hide resolved
cheap-glitch and others added 2 commits November 22, 2020 21:01
Co-authored-by: Federico <me@fregante.com>
@fregante fregante merged commit c7a1e23 into fregante:master Nov 22, 2020
@cheap-glitch cheap-glitch deleted the fix-set-undefined branch November 22, 2020 20:59
@fregante
Copy link
Owner

Thanks!

I'm releasing this as a minor after all. The behavior it changes isn't that breaking. Passing undefined was a no-op and made no sense in the first place.

@@ -68,18 +68,21 @@ async function get<TValue extends Value>(key: string): Promise<TValue | undefine
}

async function set<TValue extends Value>(key: string, value: TValue, maxAge: TimeDescriptor = {days: 30}): Promise<TValue> {
Copy link
Owner

Choose a reason for hiding this comment

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

I think the only issue here is that undefined isn't accepted by the types

Suggested change
async function set<TValue extends Value>(key: string, value: TValue, maxAge: TimeDescriptor = {days: 30}): Promise<TValue> {
async function set<TValue extends Value>(key: string, value: TValue | undefined, maxAge: TimeDescriptor = {days: 30}): Promise<TValue> {

I released a prerelease 4.3.0-0 if you want to try it in RGH in the original issue that discussed this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow cache.set(x, undefined) to delete the cached item like cache.function does
2 participants