Skip to content

Commit

Permalink
Remove handleevent function (#13)
Browse files Browse the repository at this point in the history
* Add a bunch of tests with other mixins

* Remove public `handleEvent` function in favor of a private one
  • Loading branch information
koddsson authored Apr 8, 2024
1 parent 6e4a897 commit 48b1ddc
Show file tree
Hide file tree
Showing 13 changed files with 1,005 additions and 34 deletions.
25 changes: 15 additions & 10 deletions context-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ export type UnknownContext = Context<unknown>;
/**
* A helper type which can extract a Context value type from a Context type
*/
export type ContextType<T extends UnknownContext> = T extends Context<infer Y>
? Y
: never;
export type ContextType<T extends UnknownContext> =
T extends Context<infer Y> ? Y : never;

/**
* A function which creates a Context value object
*/
export function createContext<T>(
name: string,
initialValue?: T
initialValue?: T,
): Readonly<Context<T>> {
return {
name,
Expand All @@ -39,7 +38,7 @@ export function createContext<T>(
*/
export type ContextCallback<ValueType> = (
value: ValueType,
unsubscribe?: () => void
unsubscribe?: () => void,
) => void;

/**
Expand All @@ -56,18 +55,24 @@ export class ContextEvent<T extends UnknownContext> extends Event {
public constructor(
public readonly context: T,
public readonly callback: ContextCallback<ContextType<T>>,
public readonly subscribe?: boolean
public readonly subscribe?: boolean,
) {
super("context-request", { bubbles: true, composed: true });
}
}

/**
* A 'context-request' event can be emitted by any element which desires
* a context value to be injected by an external provider.
*/
declare global {
interface WindowEventMap {
"context-request": ContextEvent<UnknownContext>;
}
interface ElementEventMap {
"context-request": ContextEvent<UnknownContext>;
}
interface HTMLElementEventMap {
/**
* A 'context-request' event can be emitted by any element which desires
* a context value to be injected by an external provider.
*/
"context-request": ContextEvent<UnknownContext>;
}
}
14 changes: 3 additions & 11 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,20 @@ export function ProviderMixin<T extends Constructor<ProviderElement>>(
this.#dataStore.set(key, value());
}

this.addEventListener("context-request", this);
this.addEventListener("context-request", this.#handleContextRequest);
}

disconnectedCallback(): void {
this.#dataStore = new ObservableMap();
this.removeEventListener("context-request", this);
}

handleEvent(event: Event) {
if (event.type === "context-request") {
this.#handleContextRequest(event as ContextEvent<UnknownContext>);
}
this.removeEventListener("context-request", this.#handleContextRequest);
}

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

// We listen for a bubbled context request event and provide the event with the context requested.
#handleContextRequest(
event: ContextEvent<{ name: string; initialValue?: unknown }>,
) {
#handleContextRequest(event: ContextEvent<UnknownContext>) {
const { name, initialValue } = event.context;
const subscribe = event.subscribe;
if (initialValue) {
Expand Down
Loading

0 comments on commit 48b1ddc

Please sign in to comment.