Skip to content

Commit

Permalink
fixed generic names and order
Browse files Browse the repository at this point in the history
  • Loading branch information
nadavov committed Oct 20, 2024
1 parent 2028f2a commit 4c1b9e1
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 39 deletions.
44 changes: 24 additions & 20 deletions packages/app-core/src/define-app-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ export interface FSApi {
path: PathApi;
}

export interface IReactApp<T = unknown, U = unknown> {
export interface IReactApp<MANIFEST_EXTRA_DATA = unknown, ROUTE_EXTRA_DATA = unknown> {
/**
* Should be isomorphic, should return the same result on the server and in a web worker
* returns the app manifest containing a list of routes for the app.
*
* should call onManifestUpdate when the manifest is updated
*/
prepareApp: (options: IPrepareAppOptions) => Promise<{
manifest: IAppManifest<T, U>;
manifest: IAppManifest<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>;
dispose: () => void;
}>;

Expand All @@ -106,13 +106,13 @@ export interface IReactApp<T = unknown, U = unknown> {
* returns the information needed to create a new page
*
*/
getNewPageInfo?: (options: IGetNewPageInfoOptions<T, U>) => {
getNewPageInfo?: (options: IGetNewPageInfoOptions<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>) => {
isValid: boolean;
errorMessage?: string;
warningMessage?: string;
pageModule: string;
newPageSourceCode: string;
newPageRoute?: RouteInfo<T>;
newPageRoute?: RouteInfo<ROUTE_EXTRA_DATA>;
routingPattern?: RoutingPattern;
};

Expand All @@ -121,12 +121,12 @@ export interface IReactApp<T = unknown, U = unknown> {
*
* returns the information needed to move a page
*/
getMovePageInfo?: (options: IMovePageInfoOptions<T, U>) => {
getMovePageInfo?: (options: IMovePageInfoOptions<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>) => {
isValid: boolean;
errorMessage?: string;
warningMessage?: string;
pageModule: string;
newPageRoute?: RouteInfo<T>;
newPageRoute?: RouteInfo<ROUTE_EXTRA_DATA>;
routingPattern?: RoutingPattern;
};
/**
Expand All @@ -151,22 +151,25 @@ export interface IReactApp<T = unknown, U = unknown> {
*/
hasGetStaticRoutes?: (options: ICallServerMethodOptions, forRouteAtFilePath: string) => Promise<boolean>;

App: React.ComponentType<IReactAppProps<T, U>>;
App: React.ComponentType<IReactAppProps<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>>;
/**
* Renders the App into an HTML element
*
* @returns a cleanup function
*/
render: (targetElement: HTMLElement, props: IReactAppProps<T, U>) => Promise<() => void>;
render: (
targetElement: HTMLElement,
props: IReactAppProps<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>,
) => Promise<() => void>;
}
export interface IAppManifest<T = unknown, U = undefined> {
extraData: U;
routes: RouteInfo<T>[];
homeRoute?: RouteInfo<T>;
errorRoutes?: RouteInfo<T>[];
export interface IAppManifest<MANIFEST_EXTRA_DATA = unknown, ROUTE_EXTRA_DATA = undefined> {
extraData: MANIFEST_EXTRA_DATA;
routes: RouteInfo<ROUTE_EXTRA_DATA>[];
homeRoute?: RouteInfo<ROUTE_EXTRA_DATA>;
errorRoutes?: RouteInfo<ROUTE_EXTRA_DATA>[];
}

export interface RouteInfo<T = unknown> {
export interface RouteInfo<ROUTE_EXTRA_DATA = unknown> {
pageModule: string;
pageExportName?: string;
/**
Expand Down Expand Up @@ -194,7 +197,7 @@ export interface RouteInfo<T = unknown> {
/**
* any extra data that should be passed to the App component
*/
extraData: T;
extraData: ROUTE_EXTRA_DATA;
path: Array<StaticRoutePart | DynamicRoutePart>;
/**
* readable (and editable) text representation of the path
Expand All @@ -214,8 +217,8 @@ export interface DynamicRoutePart {
name: string;
}

export interface IReactAppProps<T = unknown, U = unknown> {
manifest: IAppManifest<T, U>;
export interface IReactAppProps<MANIFEST_EXTRA_DATA = unknown, ROUTE_EXTRA_DATA = unknown> {
manifest: IAppManifest<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>;
importModule: DynamicImport;
uri: string;
setUri: (uri: string) => void;
Expand All @@ -242,15 +245,16 @@ export interface ICallServerMethodOptions {
fsApi: FSApi;
importModule: DynamicImport;
}
export interface IGetNewPageInfoOptions<T, U> {
export interface IGetNewPageInfoOptions<MANIFEST_EXTRA_DATA = unknown, ROUTE_EXTRA_DATA = unknown> {
fsApi: FSApi;
requestedURI: string;
manifest: IAppManifest<T, U>;
manifest: IAppManifest<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>;
}

export type RoutingPattern = 'file' | 'folder(route)' | 'folder(index)';

export interface IMovePageInfoOptions<T, U> extends IGetNewPageInfoOptions<T, U> {
export interface IMovePageInfoOptions<MANIFEST_EXTRA_DATA = unknown, ROUTE_EXTRA_DATA = unknown>
extends IGetNewPageInfoOptions<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA> {
movedFilePath: string;
}
export interface EditablePointOfInterest {
Expand Down
6 changes: 4 additions & 2 deletions packages/app-core/src/define-app.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { reactErrorHandledRendering } from '@wixc3/react-board/dist/react-error-handled-render';
import { IReactApp, OmitReactApp } from './define-app-types';

export function defineApp<T, U>(input: OmitReactApp<IReactApp<T, U>>): IReactApp<T, U> {
const res: IReactApp<T, U> = {
export function defineApp<MANIFEST_EXTRA_DATA = unknown, ROUTE_EXTRA_DATA = undefined>(
input: OmitReactApp<IReactApp<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>>,
): IReactApp<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA> {
const res: IReactApp<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA> = {
...input,

async render(target, appProps) {
Expand Down
16 changes: 8 additions & 8 deletions packages/app-core/test-kit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { createMemoryFs, IMemFileSystem } from '@file-services/memory';
import { createRequestResolver } from '@file-services/resolve';
import path from '@file-services/path';
import { IDirectoryContents } from '@file-services/types';
export interface AppDefDriverOptions<T, U> {
app: IReactApp<T, U>;
export interface AppDefDriverOptions<MANIFEST_EXTRA_DATA = unknown, ROUTE_EXTRA_DATAU = unknown> {
app: IReactApp<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATAU>;
initialFiles: IDirectoryContents;
evaluatedNodeModules: Record<string, unknown>;
/**
Expand All @@ -18,16 +18,16 @@ export interface AppDefDriverOptions<T, U> {
projectPath?: string;
}
type DirListenerObj = { cb: (files: string[]) => void; dirPath: string };
export class AppDefDriver<T, U> {
export class AppDefDriver<MANIFEST_EXTRA_DATA = unknown, ROUTE_EXTRA_DATA = unknown> {
private fs: IMemFileSystem;
private moduleSystem: ICommonJsModuleSystem;
private dirListeners: Array<DirListenerObj> = [];
private manifestListeners: Set<(manifest: IAppManifest<T, U>) => void> = new Set();
private manifestListeners: Set<(manifest: IAppManifest<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>) => void> = new Set();
private fileListeners: Record<string, Set<(contents: string | null) => void>> = {};
private exportsListeners: Record<string, Set<(exportNames: string[]) => void>> = {};
private lastManifest: IAppManifest<T, U> | null = null;
private lastManifest: IAppManifest<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA> | null = null;
private disposeApp?: () => void;
constructor(private options: AppDefDriverOptions<T, U>) {
constructor(private options: AppDefDriverOptions<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>) {
this.fs = createMemoryFs(options.initialFiles);
const resolver = createRequestResolver({ fs: this.fs });
this.moduleSystem = createBaseCjsModuleSystem({
Expand Down Expand Up @@ -123,10 +123,10 @@ export class AppDefDriver<T, U> {
movedFilePath,
});
}
addManifestListener(cb: (manifest: IAppManifest<T, U>) => void) {
addManifestListener(cb: (manifest: IAppManifest<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>) => void) {
this.manifestListeners.add(cb);
}
removeManifestListener(cb: (manifest: IAppManifest<T, U>) => void) {
removeManifestListener(cb: (manifest: IAppManifest<MANIFEST_EXTRA_DATA, ROUTE_EXTRA_DATA>) => void) {
this.manifestListeners.delete(cb);
}
private dispatchManifestUpdate() {
Expand Down
8 changes: 4 additions & 4 deletions packages/define-remix-app/src/define-remix-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default function defineRemixApp({ appPath, routingPattern = 'file' }: IDe
requestedURI,
manifest,
layoutMap,
}: IGetNewPageInfoOptions<undefined, RouteModuleInfo> & { layoutMap: Map<string, ParentLayoutWithExtra> }) => {
}: IGetNewPageInfoOptions<RouteModuleInfo, undefined> & { layoutMap: Map<string, ParentLayoutWithExtra> }) => {
const appDir = fsApi.path.join(fsApi.path.dirname(fsApi.appDefFilePath), appPath);
const routeDir = fsApi.path.join(appDir, 'routes');
const varNames = new Set<string>();
Expand Down Expand Up @@ -191,15 +191,15 @@ export default function defineRemixApp({ appPath, routingPattern = 'file' }: IDe
},
};
};
return defineApp<undefined, RouteModuleInfo>({
return defineApp<RouteModuleInfo, undefined>({
App: ({
manifest,
importModule,
setUri,
uri,
onCaughtError,
callServerMethod,
}: IReactAppProps<undefined, RouteModuleInfo>) => {
}: IReactAppProps<RouteModuleInfo, undefined>) => {
const uriRef = useRef(uri);
uriRef.current = uri;
const { Router, navigate } = useMemo(
Expand Down Expand Up @@ -466,7 +466,7 @@ export default function defineRemixApp({ appPath, routingPattern = 'file' }: IDe
},
);
layoutMap = layouts;
const initialManifest: IAppManifest<undefined, RouteModuleInfo> = {
const initialManifest: IAppManifest<RouteModuleInfo, undefined> = {
routes: [],
errorRoutes: [],
extraData: rootModuleInfo,
Expand Down
2 changes: 1 addition & 1 deletion packages/define-remix-app/src/manifest-to-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { createLinksProxy } from './links-proxy';
type RouteObject = Parameters<typeof createRemixStub>[0][0];

export const manifestToRouter = (
manifest: IAppManifest<undefined, RouteModuleInfo>,
manifest: IAppManifest<RouteModuleInfo, undefined>,
requireModule: DynamicImport,
setUri: (uri: string) => void,
onCaughtError: ErrorReporter,
Expand Down
6 changes: 3 additions & 3 deletions packages/define-remix-app/test/define-remix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ const createAppAndDriver = async (
appPath,
routingPattern,
});
const driver = new AppDefDriver<undefined, RouteModuleInfo>({
const driver = new AppDefDriver<RouteModuleInfo, undefined>({
app,
initialFiles,
evaluatedNodeModules: {
Expand All @@ -1459,8 +1459,8 @@ const createAppAndDriver = async (
return { app, driver, manifest };
};
const expectManifest = (
manifest: IAppManifest<undefined, RouteModuleInfo>,
expected: Partial<IAppManifest<undefined, RouteModuleInfo>>,
manifest: IAppManifest<RouteModuleInfo, undefined>,
expected: Partial<IAppManifest<RouteModuleInfo, undefined>>,
) => {
const fullExpected = {
errorRoutes: [],
Expand Down
2 changes: 1 addition & 1 deletion packages/define-remix-app/test/test-cases/roots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export const rootWithBreadCrumbs = transformTsx(
Breadcrumbs:
<div>
{matches.map((match) => (
match?.handle?.name ? <div key={match?.handle?.name}>{match?.handle?.name}!</div> : null
match?.handle?.name ? <div key={match.handle.name}>{match.handle.name}!</div> : null
))}
</div>
</div>
Expand Down

0 comments on commit 4c1b9e1

Please sign in to comment.