Skip to content

Commit

Permalink
navio tunnel added
Browse files Browse the repository at this point in the history
  • Loading branch information
kanzitelli committed Feb 24, 2023
1 parent f7c1e1e commit abc7636
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/tunnel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {TunnelEvent, TunnelEvents, TunnelListener, TunnelParams} from './types';

export class NavioTunnel {
private events: TunnelEvents = {};

on(event: TunnelEvent, listener: TunnelListener) {
if (!(event in this.events)) {
this.events[event] = [];
}
this.events[event]?.push(listener);
return () => this.removeListener(event, listener);
}

removeListener(event: TunnelEvent, listener: TunnelListener) {
if (!(event in this.events)) {
return;
}
const idx = this.events[event]?.indexOf(listener);
if (idx && idx > -1) {
this.events[event]?.splice(idx, 1);
}
if (this.events[event]?.length === 0) {
delete this.events[event];
}
}

echo(event: TunnelEvent, params?: TunnelParams) {
if (!(event in this.events)) {
return;
}
this.events[event]?.forEach((listener: TunnelListener) => listener(params));
}

once(event: TunnelEvent, listener: TunnelListener) {
const remove = this.on(event, (params?: TunnelParams) => {
remove();
listener(params);
});
}
}
20 changes: 19 additions & 1 deletion src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ export type ContentKeys<T extends {content: any}> = Keys<T['content']>;
export type RootSetAs = keyof Pick<Layout, 'stacks' | 'tabs' | 'drawers'>;

// Options
export type BaseOptionsProps =
| {route?: RouteProp<ParamListBase, string>; navigation?: any}
| undefined;
export type BaseOptions<Return = NativeStackNavigationOptions> =
| Return
| ((props?: {route?: RouteProp<ParamListBase, string>; navigation?: any}) => Return);
| ((props?: BaseOptionsProps) => Return);
type ScreenOptions = BaseOptions<NativeStackNavigationOptions>;
export type StackScreenOptions = ScreenOptions;
export type ModalScreenOptions = ScreenOptions;
Expand Down Expand Up @@ -45,6 +48,7 @@ export type TStackData<ScreensName> = ScreensName[] | TStackDataObj<ScreensName>
export type TTabContentData<ScreensName, StacksName> = {
stack: TStackDefinition<ScreensName, StacksName>;
options?: TabScreenOptions;
// updateOptions?: any;
};
export type TTabsContentValue<ScreensName, StacksName> =
| TStackDefinition<ScreensName, StacksName>
Expand Down Expand Up @@ -154,3 +158,17 @@ export type RootProps<RootName extends string> = {
navigationContainerProps?: Omit<ExtractProps<typeof NavigationContainer>, 'children'>;
initialRouteName?: RootName;
};

// Tunnel (Event Emitter)
export type TunnelEvent$Tabs$UpdateOptions$Params<
Name = string,
Options = BottomTabNavigationOptions,
> = {
name: Name;
options: Options;
};

export type TunnelEvent = 'tabs.updateOptions';
export type TunnelParams<T = any> = T;
export type TunnelListener = (params: TunnelParams) => void;
export type TunnelEvents = Partial<Record<TunnelEvent, TunnelListener[]>>;

0 comments on commit abc7636

Please sign in to comment.