Skip to content

Commit 48b1ddc

Browse files
authored
Remove handleevent function (#13)
* Add a bunch of tests with other mixins * Remove public `handleEvent` function in favor of a private one
1 parent 6e4a897 commit 48b1ddc

13 files changed

+1005
-34
lines changed

context-protocol.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ export type UnknownContext = Context<unknown>;
1616
/**
1717
* A helper type which can extract a Context value type from a Context type
1818
*/
19-
export type ContextType<T extends UnknownContext> = T extends Context<infer Y>
20-
? Y
21-
: never;
19+
export type ContextType<T extends UnknownContext> =
20+
T extends Context<infer Y> ? Y : never;
2221

2322
/**
2423
* A function which creates a Context value object
2524
*/
2625
export function createContext<T>(
2726
name: string,
28-
initialValue?: T
27+
initialValue?: T,
2928
): Readonly<Context<T>> {
3029
return {
3130
name,
@@ -39,7 +38,7 @@ export function createContext<T>(
3938
*/
4039
export type ContextCallback<ValueType> = (
4140
value: ValueType,
42-
unsubscribe?: () => void
41+
unsubscribe?: () => void,
4342
) => void;
4443

4544
/**
@@ -56,18 +55,24 @@ export class ContextEvent<T extends UnknownContext> extends Event {
5655
public constructor(
5756
public readonly context: T,
5857
public readonly callback: ContextCallback<ContextType<T>>,
59-
public readonly subscribe?: boolean
58+
public readonly subscribe?: boolean,
6059
) {
6160
super("context-request", { bubbles: true, composed: true });
6261
}
6362
}
6463

64+
/**
65+
* A 'context-request' event can be emitted by any element which desires
66+
* a context value to be injected by an external provider.
67+
*/
6568
declare global {
69+
interface WindowEventMap {
70+
"context-request": ContextEvent<UnknownContext>;
71+
}
72+
interface ElementEventMap {
73+
"context-request": ContextEvent<UnknownContext>;
74+
}
6675
interface HTMLElementEventMap {
67-
/**
68-
* A 'context-request' event can be emitted by any element which desires
69-
* a context value to be injected by an external provider.
70-
*/
7176
"context-request": ContextEvent<UnknownContext>;
7277
}
7378
}

index.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,20 @@ export function ProviderMixin<T extends Constructor<ProviderElement>>(
4949
this.#dataStore.set(key, value());
5050
}
5151

52-
this.addEventListener("context-request", this);
52+
this.addEventListener("context-request", this.#handleContextRequest);
5353
}
5454

5555
disconnectedCallback(): void {
5656
this.#dataStore = new ObservableMap();
57-
this.removeEventListener("context-request", this);
58-
}
59-
60-
handleEvent(event: Event) {
61-
if (event.type === "context-request") {
62-
this.#handleContextRequest(event as ContextEvent<UnknownContext>);
63-
}
57+
this.removeEventListener("context-request", this.#handleContextRequest);
6458
}
6559

6660
updateContext(name: string, value: unknown) {
6761
this.#dataStore.set(name, value);
6862
}
6963

7064
// We listen for a bubbled context request event and provide the event with the context requested.
71-
#handleContextRequest(
72-
event: ContextEvent<{ name: string; initialValue?: unknown }>,
73-
) {
65+
#handleContextRequest(event: ContextEvent<UnknownContext>) {
7466
const { name, initialValue } = event.context;
7567
const subscribe = event.subscribe;
7668
if (initialValue) {

0 commit comments

Comments
 (0)