Skip to content

Commit

Permalink
WIP instant rpc bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Nov 27, 2023
1 parent b5859fe commit a3b0cfd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 53 deletions.
15 changes: 12 additions & 3 deletions packages/telescope/src/generators/create-rpc-msg-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export const plugin = (
// if (!builder.options.rpcClients.enabled) {
// return;
// }
const instantRpcBundlerFiles = [];
const instantRpcBundlerFiles: {
[key: string]: BundlerFile[]
} = {};

const mutationContexts = bundler
.contexts
Expand Down Expand Up @@ -103,7 +105,12 @@ export const plugin = (
);

bundlerFile.instantExportedMethods = methodKeys.map((key) => svc.methods[key]);
instantRpcBundlerFiles.push(bundlerFile);

if(!instantRpcBundlerFiles[item.className]){
instantRpcBundlerFiles[item.className] = [];
}

instantRpcBundlerFiles[item.className].push(bundlerFile);
});

asts.push(createRpcClientClass(ctx.generic, proto.Msg))
Expand Down Expand Up @@ -136,6 +143,8 @@ export const plugin = (
}).filter(Boolean);

bundler.addRPCMsgClients(clients);
bundler.addStateManagers("instantRpc", instantRpcBundlerFiles);

Object.keys(instantRpcBundlerFiles).forEach((className)=>{
bundler.addStateManagers(`instantRpc_${className}`, instantRpcBundlerFiles[className]);
})
};
94 changes: 51 additions & 43 deletions packages/telescope/src/generators/create-rpc-ops-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ export const plugin = (builder: TelescopeBuilder) => {
const localname = "service-ops.ts";

// get mapping of packages and rpc query filenames.
const instantOpsMapping = {};
const methodSet = new Set();
const bundlerFiles = builder.stateManagers["instantRpc"];

if (!bundlerFiles || !bundlerFiles.length) {
return;
}

// create proto ref for context
const pkg = "@root";
const ref: ProtoRef = createEmptyProtoRef(pkg, localname);
Expand All @@ -47,6 +39,12 @@ export const plugin = (builder: TelescopeBuilder) => {

const ast = builder.options.rpcClients!.instantOps!.reduce(
(ast, instantOpsConfig) => {
const bundlerFiles =
builder.stateManagers[`instantRpc_${instantOpsConfig.className}`];

if (!bundlerFiles || !bundlerFiles.length) {
return ast;
}
let nameMapping = instantOpsConfig.nameMapping;

nameMapping = swapKeyValue(nameMapping ?? {});
Expand All @@ -58,8 +56,6 @@ export const plugin = (builder: TelescopeBuilder) => {
pkgImports,
nameMapping,
bundlerFiles,
methodSet,
instantOpsMapping
)
);
},
Expand All @@ -86,43 +82,55 @@ function createRpcOpsAst(
pkgImports: ImportUsage[],
nameMapping,
bundlerFiles: BundlerFile[],
methodSet: Set<unknown>,
instantOpsMapping,
) {
const extendInterfaces = [];
const instantMapping: {
[key: string]: {
methodName: string;
importedVarName: string;
comment?: string | undefined;
};
} = {};
const camelRpcMethods = context.options.rpcClients?.camelCase;

bundlerFiles.forEach((bundlerFile) => {
const path = `./${bundlerFile.localname.replace(/\.ts$/, '')}`;
const importedVarName = variableSlug(path)

// // build instantOpsMapping
// bundlerFile.instantExportedMethods?.forEach((method) => {
// const methodName = method.name;

// const useHookName = makeUseHookName(camel(methodName));
// const hookNameWithPkg = `${bundlerFile.package}.${useHookName}`;
// let instantHookName = null;

// if (nameMapping[hookNameWithPkg]) {
// instantHookName = nameMapping[hookNameWithPkg];
// } else {
// if (methodSet.has(useHookName)) {
// instantHookName = makeUsePkgHookName(bundlerFile.package, methodName);
// } else {
// instantHookName = useHookName;
// }
// }

// dotty.put(instantOpsMapping, instantHookName, {
// useHookName,
// importedVarName: variableSlug(path),
// comment: `${bundlerFile.package}.${useHookName}\n${
// method.comment ?? methodName
// }`,
// });

// methodSet.add(instantHookName);
// });
const path = `./${bundlerFile.localname.replace(/\.ts$/, "")}`;
const importedVarName = variableSlug(path);

if (
pkgImports &&
!pkgImports.some((item) => item.importedAs === importedVarName)
) {
pkgImports.push({
type: "typeImport",
name: importedVarName,
import: path,
importedAs: importedVarName,
});
}

extendInterfaces.push({
importedVarName: importedVarName,
interfaceName: className,
});

bundlerFile.instantExportedMethods?.forEach((method) => {
const methodName = camelRpcMethods ? camel(method.name) : method.name;
const nameWithPkg = `${context.ref.proto.package}.${methodName}`;
const methodAlias =
nameMapping && nameMapping[nameWithPkg]
? nameMapping[nameWithPkg]
: methodName;

instantMapping[methodAlias] = {
methodName,
importedVarName,
};
dotty.put(instantMapping, methodAlias, {
methodName,
importedVarName,
});
});
});

return [];
Expand Down
17 changes: 14 additions & 3 deletions packages/telescope/src/generators/create-rpc-query-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export const plugin = (
builder: TelescopeBuilder,
bundler: Bundler
) => {
const instantRpcBundlerFiles = [];
const instantRpcBundlerFiles: {
[key: string]: BundlerFile[]
} = {};
const reactQueryBundlerFiles = [];
const mobxBundlerFiles = [];

Expand Down Expand Up @@ -189,7 +191,12 @@ export const plugin = (
);

bundlerFile.instantExportedMethods = methodKeys.map((key) => proto[svcKey].methods[key]);
instantRpcBundlerFiles.push(bundlerFile);

if(!instantRpcBundlerFiles[item.className]){
instantRpcBundlerFiles[item.className] = [];
}

instantRpcBundlerFiles[item.className].push(bundlerFile);
});

asts.push(createRpcClientClass(ctx.generic, svc));
Expand Down Expand Up @@ -279,7 +286,11 @@ export const plugin = (
}).filter(Boolean);

bundler.addRPCQueryClients(clients);
bundler.addStateManagers("instantRpc", instantRpcBundlerFiles);

Object.keys(instantRpcBundlerFiles).forEach((className)=>{
bundler.addStateManagers(`instantRpc_${className}`, instantRpcBundlerFiles[className]);
})

bundler.addStateManagers("reactQuery", reactQueryBundlerFiles);
bundler.addStateManagers("mobx", mobxBundlerFiles);
};
21 changes: 17 additions & 4 deletions packages/telescope/src/generators/create-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const plugin = (
builder: TelescopeBuilder,
bundler: Bundler
) => {
const instantRpcBundlerFiles = [];
const instantRpcBundlerFiles: {
[key: string]: BundlerFile[]
} = {};

// [x] search for all files that live in package
const baseProtos = builder.store.getProtos().filter(ref => {
Expand Down Expand Up @@ -82,7 +84,12 @@ export const plugin = (
);

bundlerFile.instantExportedMethods = methodKeys.map((key) => proto[svcKey].methods[key]);
instantRpcBundlerFiles.push(bundlerFile);

if(!instantRpcBundlerFiles[item.className]){
instantRpcBundlerFiles[item.className] = [];
}

instantRpcBundlerFiles[item.className].push(bundlerFile);
});

context.body.push(createRpcClientClass(context.generic, proto[svcKey]));
Expand Down Expand Up @@ -129,7 +136,11 @@ export const plugin = (
);

bundlerFile.instantExportedMethods = methodKeys.map((key) => proto['Msg'].methods[key]);
instantRpcBundlerFiles.push(bundlerFile);
if(!instantRpcBundlerFiles[item.className]){
instantRpcBundlerFiles[item.className] = [];
}

instantRpcBundlerFiles[item.className].push(bundlerFile);
});

context.body.push(createRpcClientClass(context.generic, proto.Msg))
Expand Down Expand Up @@ -165,5 +176,7 @@ export const plugin = (
return context;
}).filter(Boolean);

bundler.addStateManagers("instantRpc", instantRpcBundlerFiles);
Object.keys(instantRpcBundlerFiles).forEach((className)=>{
bundler.addStateManagers(`instantRpc_${className}`, instantRpcBundlerFiles[className]);
})
};

0 comments on commit a3b0cfd

Please sign in to comment.