Skip to content

Commit

Permalink
Clean up(?) ListenerType usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tcobbs-bentley committed Jul 9, 2024
1 parent c8afb4a commit 40f62b1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
16 changes: 8 additions & 8 deletions src/mobile-ui-react/MobileUi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { getAllViewports, getEmphasizeElements, Messenger, MobileCore, UIError }

import "./MobileUi.scss";

// NOTE: All of the below is what AppUI says we need to do to avoid using the deprecated
// UiSyncEventArgs type from AppUI. We can't do this in one file and reference from others,
// because that would require US to export UiSyncEventArgs, and we definitely don't want to do that.
// So every file where we need to use this type, we must include all three of the below lines.
type ListenerType<TEvent extends BeEvent<Listener>> = TEvent extends BeEvent<infer TListener> ? TListener : never;
type UiSyncEventListener = ListenerType<typeof SyncUiEventDispatcher.onSyncUiEvent>;
type UiSyncEventArgs = Parameters<UiSyncEventListener>[0];
// @todo: Import this from @itwin/core-bentley in iTwin 4.8.
/**
* Convenience type to extract the listener type from a specific BeEvent. This will go away when the
* same type is exported from @itwin/core-bentley in iTwin 4.8.
* @internal
*/
export type ListenerType<TEvent extends BeEvent<Listener>> = TEvent extends BeEvent<infer TListener> ? TListener : never;

/** Type used for MobileUi.onClose BeEvent. */
export declare type CloseListener = () => void;
Expand Down Expand Up @@ -342,7 +342,7 @@ export const useScrolling = (scrollable: HTMLElement | undefined | null) => {
* @param eventIds - The optional event ids to handle. Note: if these are missing, ALL SyncUi events
* will be handled.
*/
export function useSyncUiEvent(handler: (args: UiSyncEventArgs) => void, ...eventIds: string[]) {
export function useSyncUiEvent(handler: ListenerType<typeof SyncUiEventDispatcher.onSyncUiEvent>, ...eventIds: string[]) {
React.useEffect(() => {
return SyncUiEventDispatcher.onSyncUiEvent.addListener((args) => {
if (eventIds.length === 0 || SyncUiEventDispatcher.hasEventOfInterest(args.eventIds, eventIds)) {
Expand Down
12 changes: 2 additions & 10 deletions src/mobile-ui-react/PanViewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@ import { Point3d, SmoothTransformBetweenFrusta, Transform } from "@itwin/core-ge
import { Easing, Frustum, Tweens } from "@itwin/core-common";
import { SessionStateActionId, SyncUiEventDispatcher, UiFramework } from "@itwin/appui-react";
import { Animator, IModelApp, ScreenViewport, ViewAnimationOptions } from "@itwin/core-frontend";
import { BeEvent, Listener } from "@itwin/core-bentley";

// NOTE: All of the below is what AppUI says we need to do to avoid using the deprecated
// UiSyncEventArgs type from AppUI. We can't do this in one file and reference from others,
// because that would require US to export UiSyncEventArgs, and we definitely don't want to do that.
// So every file where we need to use this type, we must include all three of the below lines.
type ListenerType<TEvent extends BeEvent<Listener>> = TEvent extends BeEvent<infer TListener> ? TListener : never;
type UiSyncEventListener = ListenerType<typeof SyncUiEventDispatcher.onSyncUiEvent>;
type UiSyncEventArgs = Parameters<UiSyncEventListener>[0];
import { ListenerType } from "./MobileUi";

/**
* Custom animator to animate the 8 corners of a view frustum change.
Expand Down Expand Up @@ -80,7 +72,7 @@ export class PanTracker {
return this._vpParentDivId;
}

private _onSyncUi = (args: UiSyncEventArgs) => {
private _onSyncUi = (args: Parameters<ListenerType<typeof SyncUiEventDispatcher.onSyncUiEvent>>[0]) => {
if (args.eventIds.has(SessionStateActionId.SetIModelConnection) && this._vpParentDivId) {
let panTracker = PanTracker.getWithKey(this._vpParentDivId);
const nextX = panTracker.nextX;
Expand Down

0 comments on commit 40f62b1

Please sign in to comment.