4
4
*/
5
5
6
6
/**
7
- * A Context object defines an optional initial value for a Context, as well as a name identifier for debugging purposes.
7
+ * A context key.
8
+ *
9
+ * A context key can be any type of object, including strings and symbols. The
10
+ * Context type brands the key type with the `__context__` property that
11
+ * carries the type of the value the context references.
8
12
*/
9
- export type Context < T > = {
10
- name : string ;
11
- initialValue ?: T ;
13
+ export type Context < KeyType , ValueType > = KeyType & {
14
+ __context__ : ValueType ;
12
15
} ;
13
16
14
17
/**
15
- * An unknown context typeU
18
+ * An unknown context type
16
19
*/
17
- export type UnknownContext = Context < unknown > ;
20
+ export type UnknownContext = Context < unknown , unknown > ;
18
21
19
22
/**
20
23
* A helper type which can extract a Context value type from a Context type
21
24
*/
22
- export type ContextType < T extends UnknownContext > = T extends Context < infer Y >
23
- ? Y
25
+ export type ContextType < T extends UnknownContext > = T extends Context < infer _ , infer V >
26
+ ? V
24
27
: never ;
25
28
26
29
/**
27
30
* A function which creates a Context value object
28
31
*/
29
- export function createContext < T > (
30
- name : string ,
31
- initialValue ?: T
32
- ) : Readonly < Context < T > > {
33
- return {
34
- name,
35
- initialValue,
36
- } ;
32
+ export function createContext < ValueType > (
33
+ key : unknown ,
34
+ ) : Readonly < Context < typeof key , ValueType > > {
35
+ return key as Context < typeof key , ValueType > ;
37
36
}
38
37
39
38
/**
@@ -42,7 +41,7 @@ export function createContext<T>(
42
41
*/
43
42
export type ContextCallback < ValueType > = (
44
43
value : ValueType ,
45
- dispose ?: ( ) => void
44
+ unsubscribe ?: ( ) => void
46
45
) => void ;
47
46
48
47
/**
@@ -51,15 +50,15 @@ export type ContextCallback<ValueType> = (
51
50
* A provider should inspect the `context` property of the event to determine if it has a value that can
52
51
* satisfy the request, calling the `callback` with the requested value if so.
53
52
*
54
- * If the requested context event contains a truthy `multiple ` value, then a provider can call the callback
55
- * multiple times if the value is changed, if this is the case the provider should pass a `dispose `
56
- * method to the callback which requesters can invoke to indicate they no longer wish to receive these updates.
53
+ * If the requested context event contains a truthy `subscribe ` value, then a provider can call the callback
54
+ * multiple times if the value is changed, if this is the case the provider should pass an `unsubscribe `
55
+ * function to the callback which requesters can invoke to indicate they no longer wish to receive these updates.
57
56
*/
58
- export class ContextEvent < T extends UnknownContext > extends Event {
57
+ export class ContextRequestEvent < T extends UnknownContext > extends Event {
59
58
public constructor (
60
59
public readonly context : T ,
61
60
public readonly callback : ContextCallback < ContextType < T > > ,
62
- public readonly multiple ?: boolean
61
+ public readonly subscribe ?: boolean
63
62
) {
64
63
super ( 'context-request' , { bubbles : true , composed : true } ) ;
65
64
}
@@ -71,7 +70,6 @@ declare global {
71
70
* A 'context-request' event can be emitted by any element which desires
72
71
* a context value to be injected by an external provider.
73
72
*/
74
- 'context-request' : ContextEvent < UnknownContext > ;
73
+ 'context-request' : ContextRequestEvent < Context < unknown , unknown > > ;
75
74
}
76
75
}
77
-
0 commit comments