Skip to content

Commit

Permalink
refactor type gen to split out mini lcm and fw lite shared types into…
Browse files Browse the repository at this point in the history
… their own config method.

Don't export apis which are not JSInvokable
  • Loading branch information
hahn-kev committed Jan 13, 2025
1 parent 4b28702 commit 7595e0b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
73 changes: 47 additions & 26 deletions backend/FwLite/FwLiteShared/TypeGen/ReinforcedFwLiteTypingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,29 @@ public static void Configure(ConfigurationBuilder builder)
.AutoOptionalProperties()
.UseVisitor<TypedImportsVisitor>());
DisableEsLintChecks(builder);
builder.Substitute(typeof(WritingSystemId), new RtSimpleTypeName("string"));

builder.Substitute(typeof(Guid), new RtSimpleTypeName("string"));
builder.Substitute(typeof(Uri), new RtSimpleTypeName("string"));
builder.Substitute(typeof(DateTimeOffset), new RtSimpleTypeName("string"));
builder.SubstituteGeneric(typeof(ValueTask<>), (type, resolver) => resolver.ResolveTypeName(typeof(Task<>).MakeGenericType(type.GenericTypeArguments[0]), true));
var dotnetObjectRefInterface = typeof(DotNetObjectReference<>).GetInterfaces().First();
builder.SubstituteGeneric(typeof(DotNetObjectReference<>), (type, resolver) => resolver.ResolveTypeName(dotnetObjectRefInterface));
builder.ExportAsThirdParty([dotnetObjectRefInterface],
exportBuilder => exportBuilder.WithName("DotNet.DotNetObject").Imports([
new() { From = "@microsoft/dotnet-js-interop", Target = "type {DotNet}" }
]));

ConfigureMiniLcmTypes(builder);
ConfigureFwLiteSharedTypes(builder);
}

private static void ConfigureMiniLcmTypes(ConfigurationBuilder builder)
{
builder.Substitute(typeof(WritingSystemId), new RtSimpleTypeName("string"));
//todo generate a multistring type rather than just substituting it everywhere
builder.ExportAsThirdParty<MultiString>().WithName("IMultiString").Imports([new ()
{
From = "$lib/dotnet-types/i-multi-string",
Target = "type {IMultiString}"
}]);
builder.ExportAsThirdParty([dotnetObjectRefInterface], exportBuilder => exportBuilder.WithName("DotNet.DotNetObject").Imports([new ()
{
From = "@microsoft/dotnet-js-interop",
Target = "type {DotNet}"
}]));
builder.ExportAsThirdParty<MultiString>().WithName("IMultiString").Imports([
new() { From = "$lib/dotnet-types/i-multi-string", Target = "type {IMultiString}" }
]);
builder.ExportAsInterface<Sense>().WithPublicNonStaticProperties(exportBuilder =>
{
if (exportBuilder.Member.Name == nameof(Sense.Order))
Expand All @@ -69,37 +74,46 @@ public static void Configure(ConfigurationBuilder builder)
builder.ExportAsInterface<MiniLcmJsInvokable>()
.FlattenHierarchy()
.WithPublicProperties()
.WithPublicMethods(b => b.AlwaysReturnPromise());
.WithPublicMethods(b => b.AlwaysReturnPromise().OnlyJsInvokable());
builder.ExportAsEnum<SortField>().UseString();
builder.ExportAsInterfaces([typeof(QueryOptions), typeof(SortOptions), typeof(ExemplarOptions)],
exportBuilder => exportBuilder.WithPublicNonStaticProperties());
}

private static void ConfigureFwLiteSharedTypes(ConfigurationBuilder builder)
{

builder.ExportAsEnum<DotnetService>().UseString();
builder.ExportAsInterface<AuthService>().WithPublicMethods(b => b.AlwaysReturnPromise());
builder.ExportAsInterface<ImportFwdataService>().WithPublicMethods(b => b.AlwaysReturnPromise());
builder.ExportAsInterface<ServerStatus>().WithPublicProperties();
builder.ExportAsInterface<CombinedProjectsService>().WithPublicMethods(b => b.AlwaysReturnPromise());
builder.ExportAsInterface<ProjectModel>().WithPublicProperties();
builder.ExportAsInterface<ServerProjects>().WithPublicProperties();
builder.ExportAsInterface<LexboxServer>().WithPublicProperties();
builder.ExportAsInterface<CrdtProject>().WithPublicProperties();
builder.ExportAsInterface<ProjectData>().WithPublicProperties();
builder.ExportAsInterface<IProjectIdentifier>().WithPublicProperties();
builder.ExportAsInterface<FwLiteConfig>().WithPublicProperties();
builder.ExportAsEnum<FwLitePlatform>().UseString();
builder.ExportAsEnum<ProjectDataFormat>();
builder.ExportAsInterface<MiniLcmApiProvider>().WithPublicMethods(b => b.AlwaysReturnPromise());
builder.ExportAsInterfaces([
typeof(AuthService),
typeof(ImportFwdataService),
typeof(CombinedProjectsService),
typeof(MiniLcmApiProvider)
], exportBuilder => exportBuilder.WithPublicMethods(b => b.AlwaysReturnPromise().OnlyJsInvokable()));

builder.ExportAsInterfaces([
typeof(ServerStatus),
typeof(ProjectModel),
typeof(ServerProjects),
typeof(LexboxServer),
typeof(CrdtProject),
typeof(ProjectData),
typeof(IProjectIdentifier),
typeof(FwLiteConfig)
], exportBuilder => exportBuilder.WithPublicProperties());
}

private static void AlwaysReturnPromise(this MethodExportBuilder exportBuilder)
private static MethodExportBuilder AlwaysReturnPromise(this MethodExportBuilder exportBuilder)
{
var isUpdatePatchMethod = exportBuilder.Member.GetParameters()
.Any(p => p.ParameterType.IsGenericType &&
p.ParameterType.GetGenericTypeDefinition() == (typeof(UpdateObjectInput<>)));
if (isUpdatePatchMethod)
{
exportBuilder.Ignore();
return;
return exportBuilder;
}

var isTaskMethod = (exportBuilder.Member.ReturnType.IsGenericType &&
Expand All @@ -118,6 +132,13 @@ private static void AlwaysReturnPromise(this MethodExportBuilder exportBuilder)
exportBuilder.Returns(typeof(Task<>).MakeGenericType(exportBuilder.Member.ReturnType));
}
}
return exportBuilder;
}

private static void OnlyJsInvokable(this MethodExportBuilder exportBuilder)
{
if (exportBuilder.Member.GetCustomAttribute<JSInvokableAttribute>() is null)
exportBuilder.Ignore();
}

private static T WithPublicNonStaticProperties<T>(this T tc, Action<PropertyExportBuilder>? configuration = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export interface IAuthService
servers() : Promise<IServerStatus[]>;
signInWebView(server: ILexboxServer) : Promise<void>;
useSystemWebView() : Promise<boolean>;
signInWebApp(server: ILexboxServer, returnUrl: string) : Promise<string>;
logout(server: ILexboxServer) : Promise<void>;
getLoggedInName(server: ILexboxServer) : Promise<string>;
}
/* eslint-enable */
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {DotNet} from '@microsoft/dotnet-js-interop';
export interface IMiniLcmApiProvider
{
getMiniLcmApi() : Promise<DotNet.DotNetObject>;
setMiniLcmApi(miniLcmApi: DotNet.DotNetObject) : Promise<void>;
clearMiniLcmApi() : Promise<void>;
}
/* eslint-enable */

0 comments on commit 7595e0b

Please sign in to comment.