Skip to content

Commit c8de395

Browse files
feat(lib): lazy url config
1 parent 86fee70 commit c8de395

File tree

8 files changed

+31
-31
lines changed

8 files changed

+31
-31
lines changed

packages/ngx-fast-icon-demo/src/app/app.config.server.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import { join } from 'node:path';
22
import { readFileSync } from 'node:fs';
33

4-
import { mergeApplicationConfig, ApplicationConfig, Injectable } from '@angular/core';
4+
import { ApplicationConfig, Injectable, mergeApplicationConfig } from '@angular/core';
55
import { provideServerRendering } from '@angular/platform-server';
66

7-
import { map, Observable } from 'rxjs';
7+
import { Observable, of } from 'rxjs';
88

99
import { provideFastSVG, SvgLoadStrategy } from '@push-based/ngx-fast-svg';
1010

1111
import { appConfig } from './app.config';
1212

1313
@Injectable()
14-
export class SvgLoadStrategySsr implements SvgLoadStrategy {
15-
load(url$: Observable<string>): Observable<string> {
16-
return url$.pipe(
17-
map((url) => {
18-
const iconPath = join(process.cwd(), 'packages', 'ngx-fast-icon-demo', 'src', url);
19-
return readFileSync(iconPath, 'utf8')
20-
})
21-
)
14+
export class SvgLoadStrategySsr extends SvgLoadStrategy {
15+
load(url: string): Observable<string> {
16+
const iconPath = join(process.cwd(), 'packages', 'ngx-fast-icon-demo', 'src', url);
17+
const iconSVG = readFileSync(iconPath, 'utf8')
18+
return of(iconSVG);
2219
}
2320
}
2421

packages/ngx-fast-icon-demo/src/app/app.config.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApplicationConfig } from '@angular/core';
1+
import { ApplicationConfig, Injectable } from '@angular/core';
22
import {
33
provideRouter,
44
withComponentInputBinding,
@@ -9,17 +9,19 @@ import { provideHttpClient, withFetch } from '@angular/common/http';
99
import { provideClientHydration } from '@angular/platform-browser';
1010
import { provideAnimations } from '@angular/platform-browser/animations';
1111

12-
import { provideFastSVG } from '@push-based/ngx-fast-svg';
12+
import { provideFastSVG, SvgLoadStrategyImpl } from '@push-based/ngx-fast-svg';
1313
import { provideAngularSvgIcon } from 'angular-svg-icon';
1414
import { provideIonicAngular } from '@ionic/angular/standalone';
1515

1616
import { appRoutes } from './app.routes';
17-
import { map, Observable, timer } from 'rxjs';
17+
import { Observable, of, switchMap, timer } from 'rxjs';
1818

19-
class LoaderStrategy {
20-
load(name: string): Observable<string> {
21-
return timer(1000).pipe(map(() => `assets/svg-icons/${name}.svg`))
22-
};
19+
@Injectable({ providedIn: 'root' })
20+
export class ConfigService extends SvgLoadStrategyImpl {
21+
lazy$ = timer(10_000)
22+
override config(url: string): Observable<string> {
23+
return this.lazy$.pipe(switchMap(() => of(url)))
24+
}
2325
}
2426

2527
export const appConfig: ApplicationConfig = {
@@ -39,7 +41,8 @@ export const appConfig: ApplicationConfig = {
3941
provideAngularSvgIcon(),
4042
provideIonicAngular({}),
4143
provideFastSVG({
42-
url: (name: string) => timer(10000).pipe(map(() => `assets/svg-icons/${name}.svg`)),
44+
url: (name: string) => `assets/svg-icons/${name}.svg`,
45+
svgLoadStrategy: ConfigService
4346
}),
4447
],
4548
};

packages/ngx-fast-lib/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
export * from './lib/token/svg-options.model';
33
export * from './lib/token/svg-options.token';
44
export * from './lib/token/svg-load.strategy.model';
5-
export * from './lib/token/svg-load.strategy';
5+
export { SvgLoadStrategyImpl } from './lib/token/svg-load.strategy';
66
// service
77
export * from './lib/svg-registry.service';
88
// component

packages/ngx-fast-lib/src/lib/fast-svg.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ export class FastSvgComponent implements AfterViewInit, OnDestroy {
112112
height = input<string>('');
113113

114114
#url = toSignal(toObservable(this.name).pipe(switchMap((name) => {
115-
const url = this.registry.url(name);
116-
return typeof url === 'string' ? of(url) : url;
115+
return this.registry.url(name);
117116
})))
118117

119118
// When the browser loaded the svg resource we trigger the caching mechanism

packages/ngx-fast-lib/src/lib/svg-registry.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { BehaviorSubject, map, Observable } from 'rxjs';
44
import { SvgOptionsToken } from './token/svg-options.token';
55
import { suspenseSvg } from './token/default-token-values';
66
import { SvgLoadStrategy } from './token/svg-load.strategy.model';
7-
import { SvgLoadStrategyImpl } from "./token/svg-load.strategy";
7+
import { SvgLoadStrategyImpl } from './token/svg-load.strategy';
88

99
// @TODO compose svg in 1 sprite and fetch by id as before
1010

@@ -69,7 +69,7 @@ export class SvgRegistry {
6969
public defaultSize = this.svgOptions?.defaultSize || '24';
7070
private _defaultViewBox = `0 0 ${this.defaultSize} ${this.defaultSize}`;
7171

72-
public url = this.svgOptions.url;
72+
public url = (name: string) => this.svgLoadStrategy.config(this.svgOptions.url(name));
7373

7474
constructor() {
7575
// configure suspense svg
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { Observable } from 'rxjs';
1+
import { Observable, of } from 'rxjs';
22
import { Injectable } from '@angular/core';
33

44
@Injectable()
55
export abstract class SvgLoadStrategy {
66
abstract load(url: string | Observable<string>): Observable<string>;
7+
config(url: string): Observable<string> {
8+
return of(url)
9+
};
710
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { from, Observable, switchMap } from 'rxjs';
1+
import { from, Observable } from 'rxjs';
22
import { getZoneUnPatchedApi } from '../internal/get-zone-unpatched-api';
3-
import { SvgLoadStrategy } from "./svg-load.strategy.model";
3+
import { SvgLoadStrategy } from './svg-load.strategy.model';
44

55
export class SvgLoadStrategyImpl extends SvgLoadStrategy {
66
fetch = getZoneUnPatchedApi('fetch', window as any);
77

8-
load(url$: Observable<string>): Observable<string> {
9-
return url$.pipe(switchMap((url) =>from(fetch(url).then((res) => (!res.ok ? '' : res.text())))));
8+
load(url: string): Observable<string> {
9+
return from(fetch(url).then((res) => (!res.ok ? '' : res.text())));
1010
}
1111
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { Observable } from 'rxjs';
2-
31
export type SvgOptions = {
4-
url: (name: string) => string | Observable<string>;
2+
url: (name: string) => string;
53
defaultSize?: string;
64
suspenseSvgString?: string;
75
};

0 commit comments

Comments
 (0)