Skip to content

Commit 10481d7

Browse files
committed
feat: angular 19
1 parent d3a49a4 commit 10481d7

File tree

4 files changed

+96
-82
lines changed

4 files changed

+96
-82
lines changed

projects/ng-http-caching-demo/src/app/app.component.html

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@
1414
</li>
1515
<li>
1616
<label>Mark request as "NO CACHEABLE"</label>
17-
<input
18-
type="checkbox"
19-
[checked]="nocache"
20-
(change)="nocache = !nocache"
21-
/>
17+
<input type="checkbox" [(ngModel)]="nocache" [ngModelOptions]="{standalone: true}" />
2218
</li>
2319
<li>
2420
<select [(ngModel)]="typeOfRequest">
25-
<option *ngFor="let t of typeOfRequests" [ngValue]="t">{{ t }}</option>
21+
@for(t of typeOfRequests(); track t){
22+
<option [ngValue]="t">{{ t }}</option>
23+
}
2624
</select>
2725
<button (click)="getRequest()">EXECUTE REQUEST</button>
2826
<br />
29-
<em *ngIf="timeSpan !== null">
30-
HTTP server respond in {{ timeSpan }} ms
27+
@if(timeSpan() !== null){
28+
<em>
29+
HTTP server respond in {{ timeSpan() }} ms
3130
</em>
32-
<em *ngIf="count"> (for {{ count }} requests)</em>
31+
}
32+
@if(count()){
33+
<em> (for {{ count() }} requests)</em>
34+
}
3335
</li>
3436
<li>
3537
<hr />
@@ -64,11 +66,9 @@
6466
<th>Headers</th>
6567
<th>Status</th>
6668
</thead>
67-
<tbody *ngIf="cachedKeys && cachedKeys.length > 0; else noCachedKeys">
68-
<tr
69-
*ngFor="let cachedKey of cachedKeys; trackBy: trackByCachedKey"
70-
[ngClass]="cachedKey.status === 'queue' ? 'queue' : ''"
71-
>
69+
<tbody>
70+
@for(cachedKey of cachedKeys(); track trackByCachedKey){
71+
<tr [class.queue]="cachedKey.status === 'queue'">
7272
<td style="width: 30%; text-align: left;">
7373
{{ cachedKey.key }}
7474
</td>
@@ -79,15 +79,13 @@
7979
{{ cachedKey.status }}
8080
</td>
8181
</tr>
82+
} @empty {
83+
<tr>
84+
<td style="text-align: center;" colspan="3">No cached keys</td>
85+
</tr>
86+
}
8287
</tbody>
83-
<ng-template #noCachedKeys>
84-
<tbody>
85-
<tr>
86-
<td style="text-align: center;" colspan="3">No cached keys</td>
87-
</tr>
88-
</tbody>
89-
</ng-template>
9088
</table>
9189
<br />
9290
<button (click)="updateCachedKeys()">Update cached keys</button>
93-
</main>
91+
</main>
Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, inject, model, signal } from '@angular/core';
2+
import { HttpClient, HttpHeaders } from '@angular/common/http';
3+
import { lastValueFrom } from 'rxjs';
4+
import { CommonModule } from '@angular/common';
5+
import { FormsModule } from '@angular/forms';
26
import {
37
NgHttpCachingService,
48
NgHttpCachingHeaders,
59
NgHttpCachingConfig,
610
NgHttpCachingHeadersList,
711
withNgHttpCachingContext
812
} from '../../../ng-http-caching/src/public-api';
9-
import { HttpClient, HttpHeaders } from '@angular/common/http';
10-
import { lastValueFrom } from 'rxjs';
11-
import { CommonModule } from '@angular/common';
12-
import { FormsModule } from '@angular/forms';
1313

1414
interface CachedKey {
1515
key: string;
@@ -18,31 +18,32 @@ interface CachedKey {
1818
}
1919

2020
@Component({
21-
// eslint-disable-next-line @angular-eslint/component-selector
22-
selector: 'app-root',
23-
templateUrl: './app.component.html',
24-
styleUrls: ['./app.component.css'],
25-
imports: [CommonModule, FormsModule]
21+
// eslint-disable-next-line @angular-eslint/component-selector
22+
selector: 'app-root',
23+
templateUrl: './app.component.html',
24+
styleUrls: ['./app.component.css'],
25+
imports: [CommonModule, FormsModule]
2626
})
2727
export class AppComponent implements OnInit {
28-
public url = 'https://my-json-server.typicode.com/typicode/demo/db';
29-
public key = 'GET@' + this.url;
30-
public tag = '';
31-
public regex: string = '';
32-
public cachedKeys: CachedKey[] = [];
33-
public timeSpan: number | null = null;
34-
public nocache = false;
35-
public lifetime = null;
36-
public count = 0;
37-
public typeOfRequests = ['PARALLEL', 'SEQUENTIAL', 'NESTED'];
38-
public typeOfRequest = this.typeOfRequests[0];
39-
private config: NgHttpCachingConfig;
28+
private readonly ngHttpCachingService = inject(NgHttpCachingService);
29+
private readonly http = inject(HttpClient);
30+
31+
public readonly url = model('https://my-json-server.typicode.com/typicode/demo/db');
32+
public readonly key = model('GET@' + this.url());
33+
public readonly tag = model('');
34+
public readonly regex = model('');
35+
public readonly cachedKeys = signal<CachedKey[]>([]);
36+
public readonly timeSpan = signal<number | null>(null);
37+
public readonly nocache = signal(false);
38+
public readonly lifetime = model<number | null>(null);
39+
public readonly count = signal(0);
40+
public readonly typeOfRequests = signal(['PARALLEL', 'SEQUENTIAL', 'NESTED']);
41+
public readonly typeOfRequest = model(this.typeOfRequests()[0]);
42+
43+
private readonly config: NgHttpCachingConfig;
4044
private timer!: ReturnType<typeof setTimeout>;
4145

42-
constructor(
43-
private ngHttpCachingService: NgHttpCachingService,
44-
private http: HttpClient
45-
) {
46+
constructor() {
4647
this.config = this.ngHttpCachingService.getConfig();
4748
}
4849

@@ -51,21 +52,29 @@ export class AppComponent implements OnInit {
5152
}
5253

5354
async getRequest(): Promise<void> {
54-
this.timeSpan = null;
55-
this.count = 0;
55+
this.timeSpan.set(null);
56+
this.count.set(0);
5657
const timeStart = new Date();
5758

59+
/**
60+
* @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#headers
61+
*/
5862
let headers = new HttpHeaders();
5963
if (this.tag) {
60-
headers = headers.set(NgHttpCachingHeaders.TAG, this.tag);
64+
headers = headers.set(NgHttpCachingHeaders.TAG, this.tag());
6165
}
62-
if (this.nocache) {
66+
if (this.nocache()) {
6367
headers = headers.set(NgHttpCachingHeaders.DISALLOW_CACHE, '1');
6468
}
65-
if (this.lifetime && Number(this.lifetime) !== this.config.lifetime) {
66-
headers = headers.set(NgHttpCachingHeaders.LIFETIME, this.lifetime);
69+
const lifetime = this.lifetime();
70+
if (lifetime && Number(lifetime) !== this.config.lifetime) {
71+
headers = headers.set(NgHttpCachingHeaders.LIFETIME, lifetime.toString());
6772
}
6873

74+
/**
75+
* You can override NgHttpCachingConfig
76+
* @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#httpcontext
77+
*/
6978
const context = withNgHttpCachingContext({
7079
isExpired: () => {
7180
console.log('context:isExpired');
@@ -81,45 +90,45 @@ export class AppComponent implements OnInit {
8190
}
8291
});
8392

84-
switch (this.typeOfRequest) {
93+
switch (this.typeOfRequest()) {
8594
case 'SEQUENTIAL': {
8695
// test sequential requests
8796
const result1 = await lastValueFrom(
88-
this.http.get(this.url, { headers, context })
97+
this.http.get(this.url(), { headers, context })
8998
);
9099
console.log('Sequential response 1', result1);
91-
this.count++;
100+
this.count.update(value => value + 1);
92101
const result2 = await lastValueFrom(
93-
this.http.get(this.url, { headers, context })
102+
this.http.get(this.url(), { headers, context })
94103
);
95104
console.log('Sequential response 2', result2);
96-
this.count++;
97-
this.timeSpan = new Date().getTime() - timeStart.getTime();
105+
this.count.update(value => value + 1);
106+
this.timeSpan.set(new Date().getTime() - timeStart.getTime());
98107
this.updateCachedKeys();
99108
break;
100109
}
101110
case 'PARALLEL': {
102111
// test parallel requests
103112
const results = await Promise.all([
104-
lastValueFrom(this.http.get(this.url, { headers, context })),
105-
lastValueFrom(this.http.get(this.url, { headers, context })),
113+
lastValueFrom(this.http.get(this.url(), { headers, context })),
114+
lastValueFrom(this.http.get(this.url(), { headers, context })),
106115
]);
107-
this.count++;
108-
this.count++;
109-
this.timeSpan = new Date().getTime() - timeStart.getTime();
116+
this.count.update(value => value + 1);
117+
this.count.update(value => value + 1);
118+
this.timeSpan.set(new Date().getTime() - timeStart.getTime());
110119
this.updateCachedKeys();
111120
console.log('Parallel responses', results);
112121
break;
113122
}
114123
case 'NESTED': {
115124
// test nested requests
116-
this.http.get(this.url, { headers, context }).subscribe((result1) => {
125+
this.http.get(this.url(), { headers, context }).subscribe((result1) => {
117126
console.log('Nested response 1', result1);
118-
this.count++;
119-
this.http.get(this.url, { headers, context }).subscribe((result2) => {
127+
this.count.update(value => value + 1);
128+
this.http.get(this.url(), { headers, context }).subscribe((result2) => {
120129
console.log('Nested response 2', result2);
121-
this.count++;
122-
this.timeSpan = new Date().getTime() - timeStart.getTime();
130+
this.count.update(value => value + 1);
131+
this.timeSpan.set(new Date().getTime() - timeStart.getTime());
123132
this.updateCachedKeys();
124133
});
125134
});
@@ -129,28 +138,32 @@ export class AppComponent implements OnInit {
129138
}
130139

131140
clearCache(): void {
141+
/** @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#cache-service */
132142
this.ngHttpCachingService.clearCache();
133143
this.updateCachedKeys();
134144
}
135145

136146
clearCacheByTag(): void {
137-
this.ngHttpCachingService.clearCacheByTag(this.tag);
147+
/** @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#cache-service */
148+
this.ngHttpCachingService.clearCacheByTag(this.tag());
138149
this.updateCachedKeys();
139150
}
140151

141152
clearCacheByRegex(): void {
142-
this.ngHttpCachingService.clearCacheByRegex(new RegExp(this.regex));
153+
/** @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#cache-service */
154+
this.ngHttpCachingService.clearCacheByRegex(new RegExp(this.regex()));
143155
this.updateCachedKeys();
144156
}
145157

146158
clearCacheByKey(): void {
147-
this.ngHttpCachingService.clearCacheByKey(this.key);
159+
/** @see https://github.com/nigrosimone/ng-http-caching?tab=readme-ov-file#cache-service */
160+
this.ngHttpCachingService.clearCacheByKey(this.key());
148161
this.updateCachedKeys();
149162
}
150163

151164
updateCachedKeys(): void {
152165
clearTimeout(this.timer);
153-
166+
154167
const keys: CachedKey[] = [];
155168

156169
this.ngHttpCachingService.getStore().forEach((value, key) => {
@@ -170,7 +183,7 @@ export class AppComponent implements OnInit {
170183
this.ngHttpCachingService.getQueue().forEach((_, key) => {
171184
keys.push({ key, headers: [], status: 'queue' });
172185
});
173-
this.cachedKeys = keys;
186+
this.cachedKeys.set(keys);
174187

175188
this.timer = setTimeout(() => this.updateCachedKeys(), 100);
176189
}
@@ -179,3 +192,9 @@ export class AppComponent implements OnInit {
179192
return cachedKey.key + '@' + cachedKey.status;
180193
}
181194
}
195+
196+
/**
197+
* NgHttpCaching demo
198+
* Cache for HTTP requests in Angular application.
199+
* see https://www.npmjs.com/package/ng-http-caching
200+
*/
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
import { enableProdMode, isDevMode } from '@angular/core';
1+
import { provideExperimentalZonelessChangeDetection } from "@angular/core"
22
import { bootstrapApplication } from '@angular/platform-browser';
33
import { AppComponent } from './app/app.component';
44
import { withNgHttpCachingLocalStorage, provideNgHttpCaching } from 'projects/ng-http-caching/src/public-api';
55
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
66

7-
if (!isDevMode()) {
8-
enableProdMode();
9-
}
10-
117
bootstrapApplication(AppComponent, {
128
providers: [
139
provideNgHttpCaching({
1410
store: withNgHttpCachingLocalStorage()
1511
}),
16-
provideHttpClient(withInterceptorsFromDi())
12+
provideHttpClient(withInterceptorsFromDi()),
13+
provideExperimentalZonelessChangeDetection(),
1714
]
1815
});

projects/ng-http-caching-demo/src/polyfills.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
/***************************************************************************************************
5656
* Zone JS is required by default for Angular itself.
5757
*/
58-
import 'zone.js'; // Included with Angular CLI.
58+
// import 'zone.js'; // Included with Angular CLI.
5959

6060

6161
/***************************************************************************************************

0 commit comments

Comments
 (0)