Skip to content

RFC: Derived Contexts #266

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open

RFC: Derived Contexts #266

wants to merge 15 commits into from

Conversation

probeiuscorp
Copy link

View Rendered Text

Selectors for Context are an in-demand feature, but with a slightly different interface, can be made much more universally useful (.slice as proposed here is similar).

Following from functional programming techniques, this RFC proposes adding:

  • a ReadonlyContext<T> type which is a supertype of Context<T> without .Provider

and these methods to Context:

  • .map, of type: <T, U>(this: ReadonlyContext<T>, fn: (data: T) => U) => ReadonlyContext<U>
  • .apply, of type: <T, U>(this: ReadonlyContext<T>, fn: ReadonlyContext<(data: T) => U>) => ReadonlyContext<U> (I actually recommend one of its alternatives)

.map allows users to refine and select their Contexts, while .apply allows users to combine multiple Contexts together. This is covered in detail in Motivation.

Every call, without respect to the identity of the given argument (function / context of functions), returns a distinct Context. To be usable within renders, existing user-land techniques like WeakMap can be used to stabilize the identity of the returned Context to the identity of the given argument.


RFC 119 useContextSelector draws from much of the same motivation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants