Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ng modules #2247

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions angular/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { TestBed } from "@angular/core/testing";
import { AppComponent } from "./app.component";
import { FormsModule } from "@angular/forms";
import { ElizaProvider } from "src/connect/client.provider";
import { provideConnect } from "src/connect/connect.module";
import { ELIZA } from "src/connect/tokens";
import { ConnectModule } from "src/connect/connect.module";

describe("AppComponent", () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
providers: [ElizaProvider],
imports: [
AppComponent,
FormsModule,
ConnectModule.forRoot({
providers: [
provideConnect({
baseUrl: "https://demo.connectrpc.com",
}),
],
imports: [AppComponent, FormsModule],
}).compileComponents();
});

Expand Down
18 changes: 5 additions & 13 deletions angular/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { Component, Inject } from "@angular/core";
import { Component, inject } from "@angular/core";
import { CommonModule } from "@angular/common";
import { FormsModule } from "@angular/forms";
import { ObservableClient } from "src/connect/observable-client";
import { ElizaService } from "src/gen/connectrpc/eliza/v1/eliza_pb";
import { ELIZA } from "../connect/tokens";

interface Response {
text: string;
sender: "eliza" | "user";
}

// Note that with Angular v19, standalone components are the default so no
// need for standalone: true here.
@Component({
imports: [CommonModule, FormsModule],
selector: "app-root",
templateUrl: "./app.component.html",
imports: [CommonModule, FormsModule],
styleUrls: ["./app.component.css"],
templateUrl: "./app.component.html",
})
export class AppComponent {
client = inject(ELIZA);
title = "Eliza";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of @Inject, we use the function inject.

project = "Angular";
statement: string = "";
Expand All @@ -28,12 +25,7 @@ export class AppComponent {
sender: "eliza",
},
];
introFinished: boolean = false;

constructor(
@Inject(ELIZA)
private client: ObservableClient<typeof ElizaService>,
) {}
introFinished = false;

onSend(event?: MouseEvent) {
if (event) {
Expand Down
12 changes: 12 additions & 0 deletions angular/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApplicationConfig } from '@angular/core';
import { provideConnect } from "../connect/connect.module";
import { provideProtractorTestingSupport } from "@angular/platform-browser";

export const appConfig: ApplicationConfig = {
providers: [
provideConnect({
baseUrl: "https://demo.connectrpc.com",
}),
provideProtractorTestingSupport(),
],
};
21 changes: 0 additions & 21 deletions angular/src/app/app.module.ts

This file was deleted.

13 changes: 0 additions & 13 deletions angular/src/connect/client.provider.ts

This file was deleted.

84 changes: 57 additions & 27 deletions angular/src/connect/connect.module.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,59 @@
import { ModuleWithProviders, NgModule } from "@angular/core";
import { Interceptor } from "@connectrpc/connect";
import { createConnectTransport } from "@connectrpc/connect-web";
import { INTERCEPTORS, TRANSPORT } from "./tokens";
import { inject, InjectionToken, Provider } from "@angular/core";
import { Interceptor, Transport } from "@connectrpc/connect";
import {
createConnectTransport,
createGrpcWebTransport,
} from "@connectrpc/connect-web";
import { DescService } from "@bufbuild/protobuf";
import { createObservableClient, ObservableClient } from "./observable-client";

@NgModule()
export class ConnectModule {
public static forRoot(
connectOptions: Omit<
Parameters<typeof createConnectTransport>[0],
"interceptors"
>,
): ModuleWithProviders<ConnectModule> {
return {
ngModule: ConnectModule,
providers: [
{
provide: TRANSPORT,
useFactory: (interceptors: Interceptor[]) =>
createConnectTransport({
...connectOptions,
interceptors: interceptors,
}),
deps: [INTERCEPTORS],
},
],
};
}
const TRANSPORT = new InjectionToken<Transport>("connect.transport");

export const INTERCEPTORS = new InjectionToken<Interceptor[]>(
"connect.interceptors",
{
factory: () => [],
},
);

export function createClientToken<T extends DescService>(
service: T,
): InjectionToken<ObservableClient<T>> {
return new InjectionToken(`client for ${service.typeName}`, {
factory() {
return createObservableClient(service, inject(TRANSPORT));
},
});
}

export function provideConnect(
options: Omit<Parameters<typeof createConnectTransport>[0], "interceptors">,
): Provider[] {
return [
{
provide: TRANSPORT,
useFactory: (interceptors: Interceptor[]) =>
createConnectTransport({
...options,
interceptors,
}),
deps: [INTERCEPTORS],
},
];
}

export function provideGrpcWeb(
options: Omit<Parameters<typeof createGrpcWebTransport>[0], "interceptors">,
): Provider[] {
return [
{
provide: TRANSPORT,
useFactory: (interceptors: Interceptor[]) =>
createGrpcWebTransport({
...options,
interceptors,
}),
deps: [INTERCEPTORS],
},
];
}
29 changes: 0 additions & 29 deletions angular/src/connect/grpc-web.module.ts

This file was deleted.

18 changes: 4 additions & 14 deletions angular/src/connect/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import { InjectionToken } from "@angular/core";
import type { Interceptor, Transport } from "@connectrpc/connect";
import { ElizaService } from "src/gen/connectrpc/eliza/v1/eliza_pb";
import { ObservableClient } from "./observable-client";
import { createClientToken } from "./connect.module";

export const TRANSPORT = new InjectionToken<Transport>("connect.transport");
// Create an injection token for the Eliza service client
export const ELIZA = createClientToken(ElizaService);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to connect.module.ts? Feels odd to have it it in its own tokens file now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tokens.ts would be modified by users, connect.module.ts won't. We don't publish this as a library. I don't feel strongly about it. Do whatever it takes 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. In 68b684a, I unexported the transport and interceptor tokens since they're only used in connect.module.ts and added a comment about creating other client tokens in tokens.ts. Seems a little better.

export const INTERCEPTORS = new InjectionToken<Interceptor[]>(
"connect.interceptors",
{
factory: () => [],
},
);

export const ELIZA = new InjectionToken<ObservableClient<typeof ElizaService>>(
ElizaService.name,
);
// Additional client tokens representing Connect services could be added here
17 changes: 6 additions & 11 deletions angular/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { appConfig } from "./app/app.config";

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err),
);
Loading