Skip to content

Commit

Permalink
remove component args (#29239)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 222714cc26e68959754bd72fdb39846c814d60a8
  • Loading branch information
ldanilek authored and Convex, Inc. committed Aug 28, 2024
1 parent 22a7e06 commit f28dd5e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 138 deletions.
28 changes: 2 additions & 26 deletions src/cli/codegen_templates/component_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { Value, jsonToConvex } from "../../values/value.js";
import { z } from "zod";
import { encodeDefinitionPath } from "../lib/components/definition/bundle.js";

export function componentServerJS(isRoot: boolean): string {
let result = `
export function componentServerJS(): string {
const result = `
${header(
"Generated utilities for implementing server-side Convex query and mutation functions.",
)}
Expand All @@ -33,7 +33,6 @@ export function componentServerJS(isRoot: boolean): string {
internalMutationGeneric,
internalQueryGeneric,
componentsGeneric,
createComponentArg,
} from "convex/server";
/**
Expand Down Expand Up @@ -108,11 +107,6 @@ export function componentServerJS(isRoot: boolean): string {
export const components = componentsGeneric();
`;
if (!isRoot) {
result += `
export const componentArg = createComponentArg();
`;
}
return result;
}

Expand Down Expand Up @@ -265,11 +259,6 @@ export function componentServerStubDTS(isRoot: boolean): string {
result += `
export declare const components: AnyComponents;
`;
if (!isRoot) {
result += `
export declare const componentArg: (ctx: GenericCtx, name: string) => any;
`;
}
return result;
}

Expand Down Expand Up @@ -316,19 +305,6 @@ export async function componentServerDTS(

result.push("};");

const definitionType = analysis.definition.definitionType;
if (definitionType.type === "childComponent") {
result.push(`type ComponentArgs = {`);
for (const [name, { value: serializedValidator }] of definitionType.args) {
const validatorType = validatorToType(JSON.parse(serializedValidator));
result.push(`${name}: ${validatorType},`);
}
result.push("};");
result.push(
`export declare const componentArg: <Name extends keyof ComponentArgs>(ctx: GenericCtx, name: Name) => ComponentArgs[Name];`,
);
}

return result.join("\n");
}

Expand Down
2 changes: 1 addition & 1 deletion src/cli/lib/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ async function doInitialComponentServerCodegen(
await writeFormattedFile(
ctx,
tmpDir,
componentServerJS(isRoot),
componentServerJS(),
"typescript",
path.join(codegenDir, "server.js"),
opts,
Expand Down
129 changes: 19 additions & 110 deletions src/server/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import {
Infer,
ObjectType,
PropertyValidators,
convexToJson,
jsonToConvex,
} from "../../values/index.js";
import { PropertyValidators, convexToJson } from "../../values/index.js";
import {
AnyFunctionReference,
FunctionReference,
FunctionType,
} from "../api.js";
import { getFunctionAddress } from "../impl/actions_impl.js";
import { performAsyncSyscall, performSyscall } from "../impl/syscall.js";
import { DefaultFunctionArgs, EmptyObject } from "../registration.js";
import { performAsyncSyscall } from "../impl/syscall.js";
import { DefaultFunctionArgs } from "../registration.js";
import {
AppDefinitionAnalysis,
ComponentDefinitionAnalysis,
Expand Down Expand Up @@ -73,35 +67,19 @@ export interface InitCtx {}
*
* @internal
*/ // eslint-disable-next-line @typescript-eslint/ban-types
export type ComponentDefinition<
Args extends PropertyValidators = EmptyObject,
Exports extends ComponentExports = any,
> = {
export type ComponentDefinition<Exports extends ComponentExports = any> = {
/**
* Install a component with the given definition in this component definition.
*
* Takes a component definition, an optional name, and the args it requires.
* Takes a component definition, and an optional name.
*
* For editor tooling this method expects a {@link ComponentDefinition}
* but at runtime the object that is imported will be a {@link ImportedComponentDefinition}
*/
install<Definition extends ComponentDefinition<any, any>>(
definition: Definition,
options: {
name?: string;
// TODO we have to do the "arguments are optional if empty, otherwise required"
args?: ObjectType<ComponentDefinitionArgs<Definition>>;
},
): InstalledComponent<Definition>;

installWithInit<Definition extends ComponentDefinition<any, any>>(
install<Definition extends ComponentDefinition<any>>(
definition: Definition,
options: {
options?: {
name?: string;
onInit: (
ctx: InitCtx,
args: ObjectType<Args>,
) => ObjectType<ComponentDefinitionArgs<Definition>>;
},
): InstalledComponent<Definition>;

Expand All @@ -112,21 +90,13 @@ export type ComponentDefinition<
*/
mountHttp(pathPrefix: string, component: InstalledComponent<any>): void;

// TODO this will be needed once components are responsible for building interfaces for themselves
/**
* @internal
*/
__args: Args;

/**
* @internal
*/
__exports: Exports;
};

type ComponentDefinitionArgs<T extends ComponentDefinition<any, any>> =
T["__args"];
type ComponentDefinitionExports<T extends ComponentDefinition<any, any>> =
type ComponentDefinitionExports<T extends ComponentDefinition<any>> =
T["__exports"];

/**
Expand All @@ -144,11 +114,10 @@ export type AppDefinition = {
* For editor tooling this method expects a {@link ComponentDefinition}
* but at runtime the object that is imported will be a {@link ImportedComponentDefinition}
*/
install<Definition extends ComponentDefinition<any, any>>(
install<Definition extends ComponentDefinition<any>>(
definition: Definition,
options: {
options?: {
name?: string;
args?: ObjectType<ComponentDefinitionArgs<Definition>>;
},
): InstalledComponent<Definition>;

Expand Down Expand Up @@ -186,7 +155,7 @@ type AppDefinitionData = CommonDefinitionData;
/**
* Used to refer to an already-installed component.
*/
class InstalledComponent<Definition extends ComponentDefinition<any, any>> {
class InstalledComponent<Definition extends ComponentDefinition<any>> {
/**
* @internal
*/
Expand Down Expand Up @@ -236,36 +205,8 @@ function createExports(name: string, pathParts: string[]): any {
function install<Definition extends ComponentDefinition<any>>(
this: CommonDefinitionData,
definition: Definition,
options: {
options?: {
name?: string;
args?: Infer<ComponentDefinitionArgs<Definition>>;
} = {},
): InstalledComponent<Definition> {
// At runtime an imported component will have this shape.
const importedComponentDefinition =
definition as unknown as ImportedComponentDefinition;
if (typeof importedComponentDefinition.componentDefinitionPath !== "string") {
throw new Error(
"Component definition does not have the required componentDefinitionPath property. This code only works in Convex runtime.",
);
}
const name =
options.name ||
importedComponentDefinition.componentDefinitionPath.split("/").pop()!;
this._childComponents.push([
name,
importedComponentDefinition,
options.args ?? {},
]);
return new InstalledComponent(definition, name);
}

function installWithInit<Definition extends ComponentDefinition<any>>(
this: ComponentDefinitionData,
definition: Definition,
options: {
name?: string;
onInit: (ctx: InitCtx, args: any) => any;
},
): InstalledComponent<Definition> {
// At runtime an imported component will have this shape.
Expand All @@ -277,23 +218,12 @@ function installWithInit<Definition extends ComponentDefinition<any>>(
);
}
const name =
options.name ||
options?.name ||
importedComponentDefinition.componentDefinitionPath.split("/").pop()!;
this._childComponents.push([name, importedComponentDefinition, null]);
this._onInitCallbacks[name] = (s) => invokeOnInit(s, options.onInit);
this._childComponents.push([name, importedComponentDefinition, {}]);
return new InstalledComponent(definition, name);
}

function invokeOnInit(
argsStr: string,
onInit: (ctx: InitCtx, args: any) => any,
): string {
const argsJson = JSON.parse(argsStr);
const args = jsonToConvex(argsJson);
const result = onInit({}, args);
return JSON.stringify(convexToJson(result));
}

function mount(this: CommonDefinitionData, exports: any) {
function visit(definition: CommonDefinitionData, path: string[], value: any) {
const valueReference = value[toReferencePath];
Expand Down Expand Up @@ -458,10 +388,7 @@ function exportComponentForAnalysis(
}

// This is what is actually contained in a ComponentDefinition.
type RuntimeComponentDefinition = Omit<
ComponentDefinition<any, any>,
"__args" | "__exports"
> &
type RuntimeComponentDefinition = Omit<ComponentDefinition<any>, "__exports"> &
ComponentDefinitionData & {
export: () => ComponentDefinitionAnalysis;
};
Expand All @@ -474,32 +401,27 @@ type RuntimeAppDefinition = AppDefinition &
* @internal
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function defineComponent<
Args extends PropertyValidators = EmptyObject,
Exports extends ComponentExports = any,
>(
export function defineComponent<Exports extends ComponentExports = any>(
name: string,
options: { args?: Args } = {},
): ComponentDefinition<Args, Exports> {
): ComponentDefinition<Exports> {
const ret: RuntimeComponentDefinition = {
_isRoot: false,
_name: name,
_args: options.args || {},
_args: {},
_childComponents: [],
_httpMounts: {},
_exportTree: {},
_onInitCallbacks: {},

export: exportComponentForAnalysis,
install,
installWithInit,
mount,
mountHttp,

// pretend to conform to ComponentDefinition, which temporarily expects __args
...({} as { __args: any; __exports: any }),
};
return ret as any as ComponentDefinition<Args, Exports>;
return ret as any as ComponentDefinition<Exports>;
}

/**
Expand Down Expand Up @@ -564,19 +486,6 @@ function createChildComponents(
return new Proxy({}, handler);
}

/**
*
* @internal
*/
export function createComponentArg(): (ctx: any, name: string) => any {
return (ctx: any, name: string) => {
const result = performSyscall("1.0/componentArgument", {
name,
});
return (jsonToConvex(result) as any).value;
};
}

/**
* @internal
*/
Expand Down
1 change: 0 additions & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ export {
defineComponent,
componentsGeneric,
currentSystemUdfInComponent,
createComponentArg,
createFunctionHandle,
} from "./components/index.js";
/**
Expand Down

0 comments on commit f28dd5e

Please sign in to comment.