Skip to content

Commit

Permalink
put listener check in a utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Kim authored and brainkim committed Aug 8, 2022
1 parent 37796ee commit d455680
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/crank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1617,10 +1617,7 @@ export class Context<TProps = any, TResult = any> implements EventTarget {
): void {
const impl = this[$ContextImpl];
let listeners: Array<EventListenerRecord>;
if (
(typeof listener !== "function" && typeof listener !== "object") ||
listener === null
) {
if (!isListenerOrListenerObject(listener)) {
return;
} else {
const listeners1 = listenersMap.get(impl);
Expand Down Expand Up @@ -1680,11 +1677,7 @@ export class Context<TProps = any, TResult = any> implements EventTarget {
): void {
const impl = this[$ContextImpl];
const listeners = listenersMap.get(impl);
if (
listeners == null ||
(typeof listener !== "function" && typeof listener !== "object") ||
listener === null
) {
if (listeners == null || !isListenerOrListenerObject(listener)) {
return;
}

Expand Down Expand Up @@ -2329,6 +2322,17 @@ type MappedEventListenerOrEventListenerObject<T extends string> =
| MappedEventListener<T>
| {handleEvent: MappedEventListener<T>};

function isListenerOrListenerObject(
value: unknown,
): value is MappedEventListenerOrEventListenerObject<string> {
return (
typeof value === "function" ||
(value !== null &&
typeof value === "object" &&
typeof (value as any).handleEvent === "function")
);
}

interface EventListenerRecord {
type: string;
callback: MappedEventListener<any>;
Expand Down

0 comments on commit d455680

Please sign in to comment.