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

add create_context helper #56

Merged
merged 35 commits into from
Sep 22, 2024
Merged

add create_context helper #56

merged 35 commits into from
Sep 22, 2024

Conversation

ryanatkn
Copy link
Owner

@ryanatkn ryanatkn commented Sep 21, 2024

update: refined the API in #57

Adds a create_context helper for better ergonomics with getContext/setContext.

See github.com/sveltejs/svelte/issues/8941 (ugh this backlink-spammed the Svelte issue, sigh, it didn't linkify github.com/sveltejs/svelte omitting the https:// so I then added issues/8941 and it did linkify, creating a spammy backlink - wrapping with backticks seems to be the best option here, <> doesn't help)

I originally tried a class implementation but it has some type limitations that the function create_context does not have because it leverages overloaded function signatures.

Examples: (notice the subtle type differences)

const required_with_fallback = create_context({fallback: () => new SomeThing()});
// {
//     get: () => SomeThing;
//     set: (value?: SomeThing) => SomeThing;
//}
const optional_and_may_get_undefined = create_context<SomeThing>({optional: true});
// {
//     get: () => SomeThing | undefined;
//     set: (value: SomeThing) => SomeThing;
// }
const required_and_throws_on_undefined = create_context<SomeThing>();
// {
//     get: (error_message?: string) => SomeThing;
//     set: (value: SomeThing) => SomeThing;
// }

I originally included an optional label to error messages, but it's not relevant if a fallback is provided other than being added to the context key symbol, which may not do anything useful.

Fixed the error_message type here - 56462f9 - since it's only valid in the fallbackless/nonoptional case.

Followup:

  • maybe remove error_message?

This PR also cleans up some stuff that was moved to Moss - some of these related things will probably be published as components later.

@ryanatkn ryanatkn merged commit 1402038 into main Sep 22, 2024
1 check passed
@ryanatkn ryanatkn deleted the context-helpers branch September 22, 2024 15:16
@ryanatkn ryanatkn changed the title add context helpers add create_context helper Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant