Skip to content

Commit

Permalink
always export methods as promises when converting from c# to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Dec 18, 2024
1 parent bf4b63a commit 0a6ec60
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
65 changes: 36 additions & 29 deletions backend/FwLite/FwLiteShared/TypeGen/ReinforcedFwLiteTypingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,19 @@ public static void Configure(ConfigurationBuilder builder)
],
exportBuilder => exportBuilder.WithPublicProperties());
builder.ExportAsEnum<WritingSystemType>().UseString();
builder.ExportAsInterface<MiniLcmJsInvokable>().FlattenHierarchy().WithPublicProperties().WithPublicMethods(
exportBuilder =>
{
var isUpdatePatchMethod = exportBuilder.Member.GetParameters()
.Any(p => p.ParameterType.IsGenericType && p.ParameterType.GetGenericTypeDefinition() == (typeof(UpdateObjectInput<>)));
if (isUpdatePatchMethod)
{
exportBuilder.Ignore();
return;
}
var isTaskMethod = (exportBuilder.Member.ReturnType.IsGenericType &&
(exportBuilder.Member.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)
|| exportBuilder.Member.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>)))
|| exportBuilder.Member.ReturnType == typeof(Task)
|| exportBuilder.Member.ReturnType == typeof(ValueTask);
if (!isTaskMethod)
{
if (exportBuilder.Member.ReturnType == typeof(void))
{
exportBuilder.Returns(typeof(Task));
} else
{
exportBuilder.Returns(typeof(Task<>).MakeGenericType(exportBuilder.Member.ReturnType));
}
}
});
builder.ExportAsInterface<MiniLcmJsInvokable>()
.FlattenHierarchy()
.WithPublicProperties()
.WithPublicMethods(b => b.AlwaysReturnPromise());
builder.ExportAsEnum<SortField>().UseString();
builder.ExportAsInterfaces([typeof(QueryOptions), typeof(SortOptions), typeof(ExemplarOptions)],
exportBuilder => exportBuilder.WithProperties(BindingFlags.Public | BindingFlags.Instance));

builder.ExportAsEnum<DotnetService>().UseString();
builder.ExportAsInterface<AuthService>().WithPublicMethods();
builder.ExportAsInterface<ImportFwdataService>().WithPublicMethods();
builder.ExportAsInterface<AuthService>().WithPublicMethods(b => b.AlwaysReturnPromise());
builder.ExportAsInterface<ImportFwdataService>().WithPublicMethods(b => b.AlwaysReturnPromise());
builder.ExportAsInterface<ServerStatus>().WithPublicProperties();
builder.ExportAsInterface<CombinedProjectsService>().WithPublicMethods();
builder.ExportAsInterface<CombinedProjectsService>().WithPublicMethods(b => b.AlwaysReturnPromise());
builder.ExportAsInterface<ProjectModel>().WithPublicProperties();
builder.ExportAsInterface<ServerProjects>().WithPublicProperties();
builder.ExportAsInterface<LexboxServer>().WithPublicProperties();
Expand All @@ -96,6 +74,35 @@ public static void Configure(ConfigurationBuilder builder)
builder.ExportAsEnum<ProjectDataFormat>();
}

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

var isTaskMethod = (exportBuilder.Member.ReturnType.IsGenericType &&
(exportBuilder.Member.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)
|| exportBuilder.Member.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>)))
|| exportBuilder.Member.ReturnType == typeof(Task)
|| exportBuilder.Member.ReturnType == typeof(ValueTask);
if (!isTaskMethod)
{
if (exportBuilder.Member.ReturnType == typeof(void))
{
exportBuilder.Returns(typeof(Task));
}
else
{
exportBuilder.Returns(typeof(Task<>).MakeGenericType(exportBuilder.Member.ReturnType));
}
}
}

private static void DisableEsLintChecks(ConfigurationBuilder builder)
{
typeof(ExportContext).GetProperty(nameof(ExportContext.FileOperations))?.SetValue(builder.Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import type {ILexboxServer} from '../Auth/ILexboxServer';

export interface ICombinedProjectsService
{
supportsFwData() : Promise<boolean>;
remoteProjects() : Promise<IServerProjects[]>;
localProjects() : IProjectModel[];
localProjects() : Promise<IProjectModel[]>;
downloadProject(lexboxProjectId: string, projectName: string, server: ILexboxServer) : Promise<void>;
createProject(name: string) : Promise<void>;
}
Expand Down

0 comments on commit 0a6ec60

Please sign in to comment.