Skip to content

Commit 957df88

Browse files
Merge 3ef45d2 into 0e52919
2 parents 0e52919 + 3ef45d2 commit 957df88

14 files changed

Lines changed: 103 additions & 66 deletions

.changeset/lemon-llamas-rule.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lit-labs/context': minor
3+
---
4+
5+
Rename ContextKey to Context

packages/labs/context/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ export {
99
ContextRequestEvent as ContextEvent,
1010
} from './lib/context-request-event.js';
1111

12-
export {ContextKey, ContextType, createContext} from './lib/context-key.js';
12+
export {
13+
Context,
14+
ContextKey,
15+
ContextType,
16+
createContext,
17+
} from './lib/create-context.js';
1318

1419
export {ContextConsumer} from './lib/controllers/context-consumer.js';
1520
export {ContextProvider} from './lib/controllers/context-provider.js';

packages/labs/context/src/lib/context-key.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

packages/labs/context/src/lib/context-request-event.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66

7-
import {ContextType, ContextKey} from './context-key.js';
7+
import {ContextType, Context} from './create-context.js';
88

99
declare global {
1010
interface HTMLElementEventMap {
1111
/**
1212
* A 'context-request' event can be emitted by any element which desires
1313
* a context value to be injected by an external provider.
1414
*/
15-
'context-request': ContextRequestEvent<ContextKey<unknown, unknown>>;
15+
'context-request': ContextRequestEvent<Context<unknown, unknown>>;
1616
}
1717
}
1818

@@ -28,9 +28,9 @@ export type ContextCallback<ValueType> = (
2828
/**
2929
* Interface definition for a ContextRequest
3030
*/
31-
export interface ContextRequest<Context extends ContextKey<unknown, unknown>> {
32-
readonly context: Context;
33-
readonly callback: ContextCallback<ContextType<Context>>;
31+
export interface ContextRequest<C extends Context<unknown, unknown>> {
32+
readonly context: C;
33+
readonly callback: ContextCallback<ContextType<C>>;
3434
readonly subscribe?: boolean;
3535
}
3636

@@ -47,9 +47,9 @@ export interface ContextRequest<Context extends ContextKey<unknown, unknown>> {
4747
* If no `subscribe` value is present in the event, then the provider can assume that this is a 'one time'
4848
* request for the context and can therefore not track the consumer.
4949
*/
50-
export class ContextRequestEvent<Context extends ContextKey<unknown, unknown>>
50+
export class ContextRequestEvent<C extends Context<unknown, unknown>>
5151
extends Event
52-
implements ContextRequest<Context>
52+
implements ContextRequest<C>
5353
{
5454
/**
5555
*
@@ -58,8 +58,8 @@ export class ContextRequestEvent<Context extends ContextKey<unknown, unknown>>
5858
* @param subscribe an optional argument, if true indicates we want to subscribe to future updates
5959
*/
6060
public constructor(
61-
public readonly context: Context,
62-
public readonly callback: ContextCallback<ContextType<Context>>,
61+
public readonly context: C,
62+
public readonly callback: ContextCallback<ContextType<C>>,
6363
public readonly subscribe?: boolean
6464
) {
6565
super('context-request', {bubbles: true, composed: true});

packages/labs/context/src/lib/context-root.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66

7-
import {ContextKey} from './context-key.js';
7+
import {Context} from './create-context.js';
88
import {ContextRequest, ContextRequestEvent} from './context-request-event.js';
99
import {ContextProviderEvent} from './controllers/context-provider.js';
1010

11-
type UnknownContextKey = ContextKey<unknown, unknown>;
11+
type UnknownContextKey = Context<unknown, unknown>;
1212

1313
/**
1414
* A context request, with associated source element, with all objects as weak references.
@@ -50,7 +50,7 @@ export class ContextRoot {
5050
}
5151

5252
private onContextProvider = (
53-
ev: ContextProviderEvent<ContextKey<unknown, unknown>>
53+
ev: ContextProviderEvent<Context<unknown, unknown>>
5454
) => {
5555
const pendingRequests = this.pendingContextRequests.get(ev.context);
5656
if (!pendingRequests) {
@@ -74,7 +74,7 @@ export class ContextRoot {
7474
};
7575

7676
private onContextRequest = (
77-
ev: ContextRequestEvent<ContextKey<unknown, unknown>>
77+
ev: ContextRequestEvent<Context<unknown, unknown>>
7878
) => {
7979
// events that are not subscribing should not be captured
8080
if (!ev.subscribe) {

packages/labs/context/src/lib/controllers/context-consumer.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import {ContextRequestEvent} from '../context-request-event.js';
8-
import {ContextKey, ContextType} from '../context-key.js';
8+
import {Context, ContextType} from '../create-context.js';
99
import {ReactiveController, ReactiveElement} from 'lit';
1010

1111
/**
@@ -17,21 +17,18 @@ import {ReactiveController, ReactiveElement} from 'lit';
1717
* disconnected.
1818
*/
1919
export class ContextConsumer<
20-
Context extends ContextKey<unknown, unknown>,
20+
C extends Context<unknown, unknown>,
2121
HostElement extends ReactiveElement
2222
> implements ReactiveController
2323
{
2424
private provided = false;
2525

26-
public value?: ContextType<Context> = undefined;
26+
public value?: ContextType<C> = undefined;
2727

2828
constructor(
2929
protected host: HostElement,
30-
private context: Context,
31-
private callback?: (
32-
value: ContextType<Context>,
33-
dispose?: () => void
34-
) => void,
30+
private context: C,
31+
private callback?: (value: ContextType<C>, dispose?: () => void) => void,
3532
private subscribe: boolean = false
3633
) {
3734
this.host.addController(this);

packages/labs/context/src/lib/controllers/context-provider.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import {ContextRequestEvent} from '../context-request-event.js';
8-
import {ContextKey, ContextType} from '../context-key.js';
8+
import {Context, ContextType} from '../create-context.js';
99
import {ValueNotifier} from '../value-notifier.js';
1010
import {ReactiveController, ReactiveElement} from 'lit';
1111

@@ -15,18 +15,18 @@ declare global {
1515
* A 'context-provider' event can be emitted by any element which hosts
1616
* a context provider to indicate it is available for use.
1717
*/
18-
'context-provider': ContextProviderEvent<ContextKey<unknown, unknown>>;
18+
'context-provider': ContextProviderEvent<Context<unknown, unknown>>;
1919
}
2020
}
2121

2222
export class ContextProviderEvent<
23-
Context extends ContextKey<unknown, unknown>
23+
C extends Context<unknown, unknown>
2424
> extends Event {
2525
/**
2626
*
2727
* @param context the context which this provider can provide
2828
*/
29-
public constructor(public readonly context: Context) {
29+
public constructor(public readonly context: C) {
3030
super('context-provider', {bubbles: true, composed: true});
3131
}
3232
}
@@ -39,7 +39,7 @@ export class ContextProviderEvent<
3939
* the host is connected to the DOM and registers the received callbacks
4040
* against its observable Context implementation.
4141
*/
42-
export class ContextProvider<T extends ContextKey<unknown, unknown>>
42+
export class ContextProvider<T extends Context<unknown, unknown>>
4343
extends ValueNotifier<ContextType<T>>
4444
implements ReactiveController
4545
{
@@ -54,7 +54,7 @@ export class ContextProvider<T extends ContextKey<unknown, unknown>>
5454
}
5555

5656
public onContextRequest = (
57-
ev: ContextRequestEvent<ContextKey<unknown, unknown>>
57+
ev: ContextRequestEvent<Context<unknown, unknown>>
5858
): void => {
5959
// Only call the callback if the context matches.
6060
// Also, in case an element is a consumer AND a provider
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
/**
8+
* The Context type defines a type brand to associate a key value with the context value type
9+
*/
10+
export type Context<KeyType, ValueType> = KeyType & {__context__: ValueType};
11+
12+
/**
13+
* @deprecated use Context instead
14+
*/
15+
export type ContextKey<KeyType, ValueType> = Context<KeyType, ValueType>;
16+
17+
/**
18+
* A helper type which can extract a Context value type from a Context type
19+
*/
20+
export type ContextType<Key extends Context<unknown, unknown>> =
21+
Key extends Context<unknown, infer ValueType> ? ValueType : never;
22+
23+
/**
24+
* Creates a typed Context.
25+
*
26+
* Contexts are compared with with strict equality.
27+
*
28+
* If you want two separate `createContext()` calls to referer to the same
29+
* context, then use a key that will by equal under strict equality like a
30+
* string for `Symbol.for()`:
31+
*
32+
* ```ts
33+
* // true
34+
* createContext('my-context') === createContext('my-context')
35+
* // true
36+
* createContext(Symbol.for('my-context')) === createContext(Symbol.for('my-context'))
37+
* ```
38+
*
39+
* If you want a context to be unique so that it's guaranteed to not collide
40+
* with other contexts, use a key that's unique under strict equality, like
41+
* a `Symbol()` or object.:
42+
*
43+
* ```
44+
* // false
45+
* createContext({}) === createContext({})
46+
* // false
47+
* createContext(Symbol('my-context')) === createContext(Symbol('my-context'))
48+
* ```
49+
*
50+
* @param key a context key value
51+
* @template ValueType the type of value that can be provided by this context.
52+
* @returns the context key value with the correct type
53+
*/
54+
export function createContext<ValueType>(key: unknown) {
55+
return key as Context<typeof key, ValueType>;
56+
}

packages/labs/context/src/lib/decorators/consume.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import {ReactiveElement} from '@lit/reactive-element';
88
import {decorateProperty} from '@lit/reactive-element/decorators/base.js';
99
import {ContextConsumer} from '../controllers/context-consumer.js';
10-
import {ContextKey} from '../context-key.js';
10+
import {Context} from '../create-context.js';
1111

1212
/*
1313
* IMPORTANT: For compatibility with tsickle and the Closure JS compiler, all
@@ -45,7 +45,7 @@ export function consume<ValueType>({
4545
context: context,
4646
subscribe,
4747
}: {
48-
context: ContextKey<unknown, ValueType>;
48+
context: Context<unknown, ValueType>;
4949
subscribe?: boolean;
5050
}): <K extends PropertyKey>(
5151
// Partial<> allows for providing the value to an optional field

packages/labs/context/src/lib/decorators/provide.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import {ReactiveElement} from '@lit/reactive-element';
88
import {decorateProperty} from '@lit/reactive-element/decorators/base.js';
9-
import {ContextKey} from '../context-key.js';
9+
import {Context} from '../create-context.js';
1010
import {ContextProvider} from '../controllers/context-provider.js';
1111

1212
/*
@@ -46,7 +46,7 @@ import {ContextProvider} from '../controllers/context-provider.js';
4646
export function provide<ValueType>({
4747
context: context,
4848
}: {
49-
context: ContextKey<unknown, ValueType>;
49+
context: Context<unknown, ValueType>;
5050
}): <K extends PropertyKey>(
5151
protoOrDescriptor: ReactiveElement & Record<K, ValueType>,
5252
name?: K

0 commit comments

Comments
 (0)