@virtualstate/navigation
Version:
Native JavaScript [navigation](https://developer.mozilla.org/en-US/docs/Web/API/Navigation_API) implementation
35 lines (30 loc) • 1.08 kB
text/typescript
import { Event } from "./event";
import { EventDescriptor, EventDescriptorSymbol } from "./descriptor";
export interface SyncEventCallback<TargetEvent = unknown> {
(event: TargetEvent): void;
}
export interface EventCallback<TargetEvent extends Event = Event> {
<E extends TargetEvent>(event: E): Promise<unknown | void> | unknown | void;
}
export function matchEventCallback(
type: string | symbol,
callback?: EventCallback | Function,
options?: unknown
): (descriptor: EventDescriptor) => boolean {
const optionsDescriptor = isOptionsDescriptor(options) ? options : undefined;
return (descriptor) => {
if (optionsDescriptor) {
return optionsDescriptor === descriptor;
}
return (
(!callback || callback === descriptor.callback) &&
type === descriptor.type
);
};
function isOptionsDescriptor(options: unknown): options is EventDescriptor {
function isLike(options: unknown): options is Partial<EventDescriptor> {
return !!options;
}
return isLike(options) && options[EventDescriptorSymbol] === true;
}
}