Skip to content

Commit ffec935

Browse files
committed
use appInitializer to load commands on startup; use fetch instead of HttpClient for smaller bundle size
1 parent 1c9f0ef commit ffec935

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

src/app/app.config.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { ApplicationConfig, InjectionToken, provideBrowserGlobalErrorListeners } from '@angular/core';
1+
import { ApplicationConfig, InjectionToken, provideBrowserGlobalErrorListeners, provideAppInitializer, inject } from '@angular/core';
22
import { provideRouter } from '@angular/router';
3-
import { provideHttpClient, withFetch } from '@angular/common/http';
43

54
import { routes } from './app.routes';
65
import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
6+
import { CommandService } from './services/command.service';
77
import packageJson from '../../package.json';
88

99
export const APP_VERSION = new InjectionToken<string>('APP_VERSION');
@@ -13,7 +13,10 @@ export const appConfig: ApplicationConfig = {
1313
provideBrowserGlobalErrorListeners(),
1414
provideRouter(routes),
1515
provideClientHydration(withEventReplay()),
16-
provideHttpClient(withFetch()),
17-
{ provide: APP_VERSION, useValue: packageJson.version }
16+
{ provide: APP_VERSION, useValue: packageJson.version },
17+
provideAppInitializer(() => {
18+
const commandService = inject(CommandService);
19+
return commandService.loadCommands();
20+
})
1821
]
1922
};

src/app/components/command-builder/command-builder.component.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Component, signal, computed, effect, inject, OnInit, viewChild } from '@angular/core';
1+
import { Component, signal, computed, effect, inject, viewChild } from '@angular/core';
22
import { ActivatedRoute } from '@angular/router';
33
import { Title } from '@angular/platform-browser';
4+
import { toSignal } from '@angular/core/rxjs-interop';
45
import { CommandService } from '../../services/command.service';
56
import { Command } from '../../models/command.model';
67
import { CommandHistoryComponent } from '../command-history/command-history.component';
@@ -127,12 +128,13 @@ interface OptionState {
127128
}
128129
`
129130
})
130-
export class CommandBuilderComponent implements OnInit {
131+
export class CommandBuilderComponent {
131132
private route = inject(ActivatedRoute);
132133
private commandService = inject(CommandService);
133134
private titleService = inject(Title);
134135
private historyComponent = viewChild(CommandHistoryComponent);
135136

137+
private routeParams = toSignal(this.route.paramMap);
136138
command = signal<Command | undefined>(undefined);
137139
isLoading = signal(true);
138140
private flagStates = signal<Map<string, FlagState>>(new Map());
@@ -172,33 +174,28 @@ export class CommandBuilderComponent implements OnInit {
172174
});
173175

174176
constructor() {
175-
// Auto-save effect
176177
effect(() => {
177-
this.generatedCommand(); // Track changes
178+
this.generatedCommand();
178179
});
179-
}
180180

181-
ngOnInit(): void {
182-
this.route.paramMap.subscribe(async params => {
183-
const id = params.get('id');
181+
effect(() => {
182+
const params = this.routeParams();
183+
const id = params?.get('id');
184+
184185
if (id) {
185186
this.isLoading.set(true);
186187
this.command.set(undefined);
187188

188-
// Wait for commands to be loaded if they haven't been yet
189-
if (this.commandService.commands().length === 0) {
190-
await this.commandService.loadCommands();
191-
}
192-
193189
const cmd = this.commandService.getCommand(id);
194190
this.command.set(cmd);
191+
195192
if (cmd) {
196193
this.initializeStates(cmd);
197194
this.titleService.setTitle(`Command Builder - ${cmd.name}`);
198195
}
196+
197+
this.isLoading.set(false);
199198
}
200-
201-
this.isLoading.set(false);
202199
});
203200
}
204201

src/app/services/command.service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Injectable, signal, computed, inject, PLATFORM_ID } from '@angular/core';
2-
import { HttpClient } from '@angular/common/http';
32
import { isPlatformBrowser } from '@angular/common';
43
import { Command, CommandData } from '../models/command.model';
54

@@ -12,21 +11,23 @@ export interface CommandHistoryEntry {
1211
providedIn: 'root'
1312
})
1413
export class CommandService {
15-
private http = inject(HttpClient);
1614
private platformId = inject(PLATFORM_ID);
1715

1816
private commandsData = signal<Command[]>([]);
1917

2018
commands = computed(() => this.commandsData());
2119

2220
async loadCommands(): Promise<void> {
23-
// Only load commands in the browser to avoid SSR issues
2421
if (!isPlatformBrowser(this.platformId)) {
2522
return;
2623
}
2724

2825
try {
29-
const data = await this.http.get<CommandData>('/commands.json').toPromise();
26+
const response = await fetch('/commands.json');
27+
if (!response.ok) {
28+
throw new Error(`HTTP error! status: ${response.status}`);
29+
}
30+
const data: CommandData = await response.json();
3031
if (data) {
3132
this.commandsData.set(data.commands);
3233
}

0 commit comments

Comments
 (0)