RFC: Derived Contexts
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 ofContext<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.