protected override void ClientAppenderImpl(Type element, RtInterface result, TypeResolver resolver) { SignalRGenerationOptions options = SignalRGenerationOptions.All[element]; string typeName = element.IsInterface && element.Name.StartsWith('I') ? element.Name.Substring(1) : element.Name; Context.AddNewLine(); string callbacks = $"{typeName}_Callbacks"; RtInterface eventInter = new RtInterface { Name = new RtSimpleTypeName(callbacks), Members = { }, Export = false }; eventInter.Members.AddRange(result.Members.OfType <RtFunction>().Select(m => new RtField() { AccessModifier = AccessModifier.Public, Identifier = m.Identifier, Type = GetCallbackType(m) })); Context.Location.CurrentNamespace.CompilationUnits.Add(eventInter); Context.AddNewLine(); Context.AddRawToNamespace($"export type {typeName}_CallbackNames = keyof {callbacks};").AddNewLine(); Context.AddRawToNamespace($"export type {typeName}_Callback<TKey extends {typeName}_CallbackNames> = {callbacks}[TKey];").AddNewLine(); }
private void BuildClientClass(string typeName, SignalRGenerationOptions options, Type element, RtInterface result, TypeResolver resolver) { RtClass clientImpl = new RtClass { Name = new RtSimpleTypeName(typeName), Export = true, Decorators = { }, Members = { new RtConstructor { Arguments = { new RtArgument { Type = new RtSimpleTypeName(options.HubConnectionProviderType), Identifier = new RtIdentifier("hubConnectionProvider") } }, Body = new RtRaw($"this.hubConnection = hubConnectionProvider.getHubConnection(\"{options.HubPath}\");"), LineAfter = " " } } }; clientImpl.Members.AddRange(GetImplementationMembers(result, options.HubPath)); Context.Location.CurrentNamespace.CompilationUnits.Add(clientImpl); Context.AddNewLine(); }
/// <summary> /// Add code generation for SignalR Hubs and clients (Strongly typed). /// Requires module pattern, and auto async. Will enable them automatically. /// </summary> /// <typeparam name="THub"></typeparam> /// <typeparam name="TClient"></typeparam> /// <param name="builder"></param> /// <param name="options"></param> /// <param name="namespaceFilter"></param> public static void GenerateSignalRTypeScriptHub <THub, TClient>( this ConfigurationBuilder builder, SignalRGenerationOptions options) { Type serverType = typeof(THub); Type frontendType = typeof(TClient); //Cache options so they can be accessed in the generators. SignalRGenerationOptions.All[typeof(THub)] = options; SignalRGenerationOptions.All[typeof(TClient)] = options; builder.Global(c => { c.UseModules().AutoAsync(true); }); builder.AddImport("{ HubConnection }", "@microsoft/signalr"); builder.AddImport($"{{ {options.HubConnectionProviderType} }}", options.HubConnectionProviderModule); builder.Substitute(typeof(Uri), new RtSimpleTypeName("string")); HashSet <Type> relatedTypes = new HashSet <Type>(); relatedTypes.UnionWith(TraverseTypes(serverType, options.NamespaceTypeFilter)); relatedTypes.UnionWith(TraverseTypes(frontendType, options.NamespaceTypeFilter)); relatedTypes.Remove(serverType); relatedTypes.Remove(frontendType); IEnumerable <Type> relatedClassTypes = relatedTypes.Where(t => t.IsClass); IEnumerable <Type> relatedEnumTypes = relatedTypes.Where(t => t.IsEnum); IEnumerable <Type> otherTypes = relatedTypes.Where(t => !(t.IsClass || t.IsEnum)); Func <Type, int> orderFunc = relatedTypes.SelectMany(t => EnumerateHierarchy(t, e => e.BaseType).Reverse()).ToList().IndexOf; builder.ExportAsEnums(relatedEnumTypes, c => c.Order(orderFunc(c.Type))); builder.ExportAsInterfaces(relatedClassTypes, c => c.Substitute(typeof(DateTime), new RtSimpleTypeName("string")).WithPublicProperties().WithPublicFields().WithPublicMethods().Order(orderFunc(c.Type))); builder.ExportAsInterfaces(otherTypes, c => c.Substitute(typeof(DateTime), new RtSimpleTypeName("string")).WithPublicProperties().WithPublicFields().WithPublicMethods().Order(orderFunc(c.Type))); builder.ExportAsInterface <THub>().Substitute(typeof(DateTime), new RtSimpleTypeName("Date")).WithPublicProperties().WithPublicFields().WithPublicMethods().WithCodeGenerator <ServerClientAppender>(); //OK to use dates in this direction builder.ExportAsInterface <TClient>().Substitute(typeof(DateTime), new RtSimpleTypeName("string")).WithPublicProperties().WithPublicFields().WithPublicMethods().WithCodeGenerator <FrontEndClientAppender>(); }