Skip to content

Commit f06b433

Browse files
feat(service): preloading capability
1 parent f775cc3 commit f06b433

File tree

8 files changed

+155
-5
lines changed

8 files changed

+155
-5
lines changed

projects/elements-demo/src/app/features/docs/api/api.component.html

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,17 @@ <h2>LazyElementRootOptions</h2>
293293
>
294294
</td>
295295
</tr>
296+
<tr>
297+
<td><pre>preload: boolean</pre></td>
298+
<td>
299+
<p>
300+
Flag that specifies if the all the modules should be preloaded
301+
</p>
302+
<code color="accent">Optional</code>&nbsp;<code color="accent"
303+
>Default: undefined</code
304+
>
305+
</td>
306+
</tr>
296307
</tbody>
297308
</table>
298309
</mat-card>
@@ -406,6 +417,53 @@ <h2>ElementConfig</h2>
406417
>
407418
</td>
408419
</tr>
420+
<tr>
421+
<td><pre>preload: boolean</pre></td>
422+
<td>
423+
<p>
424+
Flag that specifies if the element is preloaded.
425+
</p>
426+
<code color="accent">Optional</code>&nbsp;<code color="accent"
427+
>Default: undefined</code
428+
>
429+
</td>
430+
</tr>
431+
</tbody>
432+
</table>
433+
</mat-card>
434+
</section>
435+
436+
<section>
437+
<h2>LazyElementsLoaderService</h2>
438+
<code color="accent">Service</code>
439+
<br />
440+
<p>
441+
A service used for loading the Angular element (or any other webcomponent)
442+
</p>
443+
<mat-card>
444+
<table>
445+
<thead>
446+
<th>Method</th>
447+
<th>Description</th>
448+
</thead>
449+
<tbody>
450+
<tr>
451+
<td><pre>preload(tags? : string[]): void</pre></td>
452+
<td>
453+
<p>
454+
Preloads the specified tags which are preconfigured using
455+
<code>forRoot</code> and <code>forFeature</code>. If
456+
<code>tags</code> is <code>undefined|null</code> preload all the
457+
configured tags. <br />
458+
Parameters: <br />
459+
tags <code color="accent">Optional</code>&nbsp;<code
460+
color="accent"
461+
>Default: undefined</code
462+
><br />
463+
returns: <code color="accent">void</code>
464+
</p>
465+
</td>
466+
</tr>
409467
</tbody>
410468
</table>
411469
</mat-card>

projects/elements-demo/src/app/features/examples/advanced/advanced.component.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,32 @@ <h2>
110110
<pre [highlight]="codeExample4coreModule"></pre>
111111
</div>
112112
</div>
113+
114+
<h2>
115+
Preloading
116+
</h2>
117+
<div class="content">
118+
<div class="implementation">
119+
<button mat-flat-button color="accent" (click)="preload()">
120+
Preload
121+
</button>
122+
<button
123+
class="additional-btn"
124+
mat-flat-button
125+
color="accent"
126+
(click)="preloadFab()"
127+
>
128+
Preload Fab
129+
</button>
130+
</div>
131+
<div class="description">
132+
<p>
133+
You can inject <code>LazyElementLoaderService</code> and call the
134+
<code>preload</code> method to preload all the configured modules, or
135+
specify the list of <code>tags</code> you want to preload.
136+
</p>
137+
<pre [highlight]="codeExample5html"></pre>
138+
<pre [highlight]="codeExample5ts"></pre>
139+
</div>
140+
</div>
113141
</div>

projects/elements-demo/src/app/features/examples/advanced/advanced.component.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
p {
2121
margin: 10px 0 0 0;
2222
}
23+
24+
.additional-btn {
25+
margin-top: 10px;
26+
}
2327
}
2428

2529
.description {

projects/elements-demo/src/app/features/examples/advanced/advanced.component.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { LazyElementsLoaderService } from './../../../../../../elements/src/lib/lazy-elements/lazy-elements-loader.service';
23

34
@Component({
45
selector: 'demo-advanced',
@@ -21,17 +22,27 @@ export class AdvancedComponent implements OnInit {
2122
codeExample3html = CODE_EXAMPLE_3_HTML;
2223
codeExample4html = CODE_EXAMPLE_4_HTML;
2324
codeExample4coreModule = CODE_EXAMPLE_4_CORE_MODULE;
25+
codeExample5html = CODE_EXAMPLE_5_HTML;
26+
codeExample5ts = CODE_EXAMPLE_5_TS;
2427

2528
// example state
2629
counter = 0;
2730

28-
constructor() {}
31+
constructor(private lazyElementLoaderService: LazyElementsLoaderService) {}
2932

3033
ngOnInit() {}
3134

3235
increment() {
3336
this.counter++;
3437
}
38+
39+
preload() {
40+
this.lazyElementLoaderService.preload();
41+
}
42+
43+
preloadFab() {
44+
this.lazyElementLoaderService.preload(['mwc-fab']);
45+
}
3546
}
3647

3748
const CODE_EXAMPLE_1_MODULE = `// pre-configured LazyElementsModule
@@ -41,7 +52,8 @@ const options: LazyElementModuleOptions = {
4152
tag: 'ion-button',
4253
url: 'https://unpkg.com/@ionic/core@4.6.2/dist/ionic/ionic.js',
4354
loadingComponent: SpinnerComponent,
44-
errorComponent: ErrorComponent
55+
errorComponent: ErrorComponent,
56+
preload: true
4557
}
4658
]
4759
};
@@ -132,3 +144,19 @@ const options: LazyElementModuleRootOptions = {
132144
})
133145
export class CoreModule { }
134146
`;
147+
148+
const CODE_EXAMPLE_5_HTML = `<button (click)="preload()">Preload</button>`;
149+
150+
const CODE_EXAMPLE_5_TS = `
151+
class PageComponent {
152+
constructor(private lazyElementLoaderService: LazyElementLoaderService) {}
153+
154+
preload() {
155+
this.lazyElementLoaderService.preload();
156+
}
157+
158+
preloadFab() {
159+
this.lazyElementLoaderService.preload(['mwc-fab']);
160+
}
161+
}
162+
`;

projects/elements-demo/src/app/features/examples/advanced/advanced.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const options: LazyElementModuleOptions = {
1919
tag: 'ion-button',
2020
url: 'https://unpkg.com/@ionic/core@4.6.2/dist/ionic/ionic.js',
2121
loadingComponent: SpinnerComponent,
22-
errorComponent: ErrorComponent
22+
errorComponent: ErrorComponent,
23+
preload: true
2324
},
2425
{
2526
tag: 'mwc-switch',

projects/elements/src/lib/lazy-elements/lazy-elements-loader.service.spec.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ describe('LazyElementsLoaderService preconfigured with LazyElementsModule', () =
138138
{ tag: 'some-element', url: 'http://elements.com/some-url' },
139139
{
140140
tag: 'some-other-element',
141-
url: 'http://elements.com/some-other-url'
141+
url: 'http://elements.com/some-other-url',
142+
preload: true
142143
},
143144
{
144145
tag: 'some-module-element',
@@ -167,7 +168,8 @@ describe('LazyElementsLoaderService preconfigured with LazyElementsModule', () =
167168
});
168169
expect(service.configs[1]).toEqual({
169170
tag: 'some-other-element',
170-
url: 'http://elements.com/some-other-url'
171+
url: 'http://elements.com/some-other-url',
172+
preload: true
171173
});
172174
expect(service.configs[2]).toEqual({
173175
tag: 'some-module-element',
@@ -185,4 +187,14 @@ describe('LazyElementsLoaderService preconfigured with LazyElementsModule', () =
185187
);
186188
expect(appendChildSpy.calls.argsFor(0)[0].type).toBe('module');
187189
});
190+
191+
it('should preload all the configurations', () => {
192+
service.preload();
193+
expect(appendChildSpy).toHaveBeenCalledTimes(2);
194+
});
195+
196+
it('should preload only specified tags', () => {
197+
service.preload(['some-element']);
198+
expect(appendChildSpy).toHaveBeenCalledTimes(1);
199+
});
188200
});

projects/elements/src/lib/lazy-elements/lazy-elements-loader.service.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface ElementConfig {
1111
isModule?: boolean;
1212
loadingComponent?: Type<any>;
1313
errorComponent?: Type<any>;
14+
preload?: boolean;
1415
}
1516

1617
@Injectable({
@@ -39,6 +40,13 @@ export class LazyElementsLoaderService {
3940
);
4041
} else {
4142
this.configs.push(newConfig);
43+
const shouldPreload =
44+
newConfig.preload !== undefined
45+
? newConfig.preload
46+
: this.options.preload;
47+
if (shouldPreload) {
48+
this.loadElement(newConfig.url, newConfig.tag, newConfig.isModule);
49+
}
4250
}
4351
});
4452
}
@@ -47,6 +55,16 @@ export class LazyElementsLoaderService {
4755
return this.configs.find(config => config.tag === tag);
4856
}
4957

58+
preload(tags?: string[]) {
59+
let configs = this.configs;
60+
if (tags) {
61+
configs = this.configs.filter(config => tags.includes(config.tag));
62+
}
63+
configs.forEach(config =>
64+
this.loadElement(config.url, config.tag, config.isModule)
65+
);
66+
}
67+
5068
loadElement(url: string, tag: string, isModule?: boolean): Promise<void> {
5169
const config = this.getElementConfig(tag);
5270

projects/elements/src/lib/lazy-elements/lazy-elements.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,5 @@ export interface LazyElementRootOptions {
9595
loadingComponent?: Type<any>;
9696
errorComponent?: Type<any>;
9797
isModule?: boolean;
98+
preload?: boolean;
9899
}

0 commit comments

Comments
 (0)