Skip to content

Commit

Permalink
fix: more improvements to types
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jun 22, 2020
1 parent 962456b commit d244488
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 30 deletions.
4 changes: 0 additions & 4 deletions packages/compat/src/createCompatNavigationProp.tsx
Expand Up @@ -16,10 +16,6 @@ type EventName =
| 'didBlur'
| 'refocus';

// const focusSubscriptions = new WeakMap<() => void, () => void>();
// const blurSubscriptions = new WeakMap<() => void, () => void>();
// const refocusSubscriptions = new WeakMap<() => void, () => void>();

export default function createCompatNavigationProp<
NavigationPropType extends NavigationProp<ParamListBase>,
ParamList extends ParamListBase = NavigationPropType extends NavigationProp<
Expand Down
6 changes: 3 additions & 3 deletions packages/compat/src/createCompatNavigatorFactory.tsx
Expand Up @@ -6,7 +6,6 @@ import {
TypedNavigator,
NavigationProp,
RouteProp,
EventMapBase,
NavigationRouteContext,
} from '@react-navigation/native';
import CompatScreen from './CompatScreen';
Expand All @@ -19,7 +18,7 @@ export default function createCompatNavigatorFactory<
ParamListBase,
NavigationState,
{},
EventMapBase,
any,
React.ComponentType<any>
>
>(createNavigator: CreateNavigator) {
Expand Down Expand Up @@ -116,7 +115,8 @@ export default function createCompatNavigatorFactory<
typeof screenNavigationOptions === 'function'
? {
navigation: createCompatNavigationProp<
NavigationPropType
NavigationPropType,
ParamList
>(navigation, route, {}),
navigationOptions: defaultNavigationOptions || {},
screenProps,
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/BaseNavigationContainer.tsx
Expand Up @@ -17,8 +17,11 @@ import useOptionsGetters from './useOptionsGetters';
import useEventEmitter from './useEventEmitter';
import useSyncState from './useSyncState';
import isSerializable from './isSerializable';

import type { NavigationContainerRef, NavigationContainerProps } from './types';
import type {
NavigationContainerEventMap,
NavigationContainerRef,
NavigationContainerProps,
} from './types';

type State = NavigationState | PartialState<NavigationState> | undefined;

Expand Down Expand Up @@ -175,7 +178,7 @@ const BaseNavigationContainer = React.forwardRef(
return state.routes[state.index];
}, [getRootState]);

const emitter = useEventEmitter();
const emitter = useEventEmitter<NavigationContainerEventMap>();

const { addOptionsGetter, getCurrentOptions } = useOptionsGetters({});

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/types.tsx
Expand Up @@ -61,7 +61,7 @@ export type EventArg<
preventDefault(): void;
}
: {}) &
(Data extends undefined ? {} : { readonly data: Data });
(undefined extends Data ? {} : { readonly data: Data });

export type EventListenerCallback<
EventMap extends EventMapBase,
Expand Down Expand Up @@ -107,7 +107,7 @@ export type EventEmitter<EventMap extends EventMapBase> = {
} & (EventMap[EventName]['canPreventDefault'] extends true
? { canPreventDefault: true }
: {}) &
(EventMap[EventName]['data'] extends undefined
(undefined extends EventMap[EventName]['data']
? {}
: { data: EventMap[EventName]['data'] })
): EventArg<
Expand Down Expand Up @@ -275,7 +275,7 @@ export type RouteProp<
ParamList extends ParamListBase,
RouteName extends keyof ParamList
> = Omit<Route<Extract<RouteName, string>>, 'params'> &
(ParamList[RouteName] extends undefined
(undefined extends ParamList[RouteName]
? {}
: {
/**
Expand Down Expand Up @@ -419,7 +419,7 @@ export type NavigationContainerEventMap = {
/**
* The updated state object after the state change.
*/
state: NavigationState;
state: NavigationState | PartialState<NavigationState> | undefined;
};
};
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/useDescriptors.tsx
Expand Up @@ -51,7 +51,7 @@ type Options<
addStateGetter: (key: string, getter: NavigatorStateGetter) => void;
onRouteFocus: (key: string) => void;
router: Router<State, NavigationAction>;
emitter: NavigationEventEmitter;
emitter: NavigationEventEmitter<any>;
};

/**
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/useEventEmitter.tsx
@@ -1,18 +1,20 @@
import * as React from 'react';
import type { EventEmitter, EventConsumer, EventArg } from './types';

export type NavigationEventEmitter = EventEmitter<Record<string, any>> & {
create: (target: string) => EventConsumer<Record<string, any>>;
export type NavigationEventEmitter<
T extends Record<string, any>
> = EventEmitter<T> & {
create: (target: string) => EventConsumer<T>;
};

type Listeners = ((e: any) => void)[];

/**
* Hook to manage the event system used by the navigator to notify screens of various events.
*/
export default function useEventEmitter(
export default function useEventEmitter<T extends Record<string, any>>(
listen?: (e: any) => void
): NavigationEventEmitter {
): NavigationEventEmitter<T> {
const listenRef = React.useRef(listen);

React.useEffect(() => {
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/useFocusEvents.tsx
Expand Up @@ -2,16 +2,20 @@ import * as React from 'react';
import type { NavigationState } from '@react-navigation/routers';
import NavigationContext from './NavigationContext';
import type { NavigationEventEmitter } from './useEventEmitter';
import type { EventMapCore } from './types';

type Options = {
state: NavigationState;
emitter: NavigationEventEmitter;
type Options<State extends NavigationState> = {
state: State;
emitter: NavigationEventEmitter<EventMapCore<State>>;
};

/**
* Hook to take care of emitting `focus` and `blur` events.
*/
export default function useFocusEvents({ state, emitter }: Options) {
export default function useFocusEvents<State extends NavigationState>({
state,
emitter,
}: Options<State>) {
const navigation = React.useContext(NavigationContext);
const lastFocusedKeyRef = React.useRef<string | undefined>();

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/useNavigationBuilder.tsx
Expand Up @@ -29,6 +29,7 @@ import {
RouteConfig,
PrivateValueStore,
EventMapBase,
EventMapCore,
} from './types';
import useStateGetters from './useStateGetters';
import useOnGetState from './useOnGetState';
Expand Down Expand Up @@ -378,7 +379,7 @@ export default function useNavigationBuilder<
: (initializedStateRef.current as State);
}, [getCurrentState, isStateInitialized]);

const emitter = useEventEmitter((e) => {
const emitter = useEventEmitter<EventMapCore<State>>((e) => {
let routeNames = [];

let route: Route<string> | undefined;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/useNavigationCache.tsx
Expand Up @@ -8,7 +8,7 @@ import {
} from '@react-navigation/routers';
import type { NavigationEventEmitter } from './useEventEmitter';

import type { NavigationHelpers, NavigationProp } from './types';
import type { EventMapBase, NavigationHelpers, NavigationProp } from './types';

type Options<State extends NavigationState> = {
state: State;
Expand All @@ -19,7 +19,7 @@ type Options<State extends NavigationState> = {
cb: (options: Record<string, object>) => Record<string, object>
) => void;
router: Router<State, NavigationAction>;
emitter: NavigationEventEmitter;
emitter: NavigationEventEmitter<EventMapBase>;
};

type NavigationCache<
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/useNavigationHelpers.tsx
Expand Up @@ -20,7 +20,7 @@ type Options<State extends NavigationState, Action extends NavigationAction> = {
visitedNavigators?: Set<string>
) => boolean;
getState: () => State;
emitter: NavigationEventEmitter;
emitter: NavigationEventEmitter<any>;
router: Router<State, Action>;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/routers/src/StackRouter.tsx
Expand Up @@ -51,7 +51,7 @@ export type StackActionHelpers<ParamList extends ParamListBase> = {
* @param [params] Params object for the new route.
*/
replace<RouteName extends keyof ParamList>(
...args: ParamList[RouteName] extends undefined
...args: undefined extends ParamList[RouteName]
? [RouteName] | [RouteName, ParamList[RouteName]]
: [RouteName, ParamList[RouteName]]
): void;
Expand All @@ -63,7 +63,7 @@ export type StackActionHelpers<ParamList extends ParamListBase> = {
* @param [params] Params object for the route.
*/
push<RouteName extends keyof ParamList>(
...args: ParamList[RouteName] extends undefined | any
...args: undefined extends ParamList[RouteName]
? [RouteName] | [RouteName, ParamList[RouteName]]
: [RouteName, ParamList[RouteName]]
): void;
Expand Down
2 changes: 1 addition & 1 deletion packages/routers/src/TabRouter.tsx
Expand Up @@ -42,7 +42,7 @@ export type TabActionHelpers<ParamList extends ParamListBase> = {
* @param [params] Params object for the route.
*/
jumpTo<RouteName extends Extract<keyof ParamList, string>>(
...args: ParamList[RouteName] extends undefined | any
...args: undefined extends ParamList[RouteName]
? [RouteName] | [RouteName, ParamList[RouteName]]
: [RouteName, ParamList[RouteName]]
): void;
Expand Down

0 comments on commit d244488

Please sign in to comment.