Skip to content

Commit 014413a

Browse files
committed
feat: refactor Example10Component and CollaborativeListService to use inject and linkedSignal for improved dependency management
1 parent 7a4e4a1 commit 014413a

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

src/app/examples/advanced/example10/example10.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Component } from '@angular/core';
1+
import { Component, inject } from '@angular/core';
22
import { CollaborativeListService } from '../../../shared/collaborative-list.service';
33
import { MatTabsModule } from '@angular/material/tabs';
44
import { MatCardModule } from '@angular/material/card';
55
import { MatIconModule } from '@angular/material/icon';
66
import { MarkdownComponent } from 'ngx-markdown';
77
import { RouterModule } from '@angular/router';
88
import { MatButtonModule } from '@angular/material/button';
9-
import { JsonPipe, CommonModule } from '@angular/common';
9+
import { JsonPipe } from '@angular/common';
1010
import { FormsModule } from '@angular/forms';
1111

1212
@Component({
@@ -26,10 +26,10 @@ import { FormsModule } from '@angular/forms';
2626
styleUrl: './example10.component.scss'
2727
})
2828
export class Example10Component {
29-
list = this.collabService.list;
30-
newItem = '';
29+
private readonly collabService = inject(CollaborativeListService);
3130

32-
constructor(private collabService: CollaborativeListService) {}
31+
public list = this.collabService.list;
32+
public newItem = '';
3333

3434
add() {
3535
if (this.newItem.trim()) {

src/app/shared/collaborative-list.service.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import { Injectable, signal } from '@angular/core';
2-
import { fromEvent } from 'rxjs';
1+
import { Injectable, linkedSignal, signal } from '@angular/core';
2+
import { toSignal } from '@angular/core/rxjs-interop';
3+
import { filter, fromEvent, map } from 'rxjs';
34

45
const STORAGE_KEY = 'collab-list';
56

67
@Injectable({ providedIn: 'root' })
78
export class CollaborativeListService {
8-
private _list = signal<string[]>(this.loadList());
99

10-
readonly list = this._list.asReadonly();
10+
private listSignal = toSignal(
11+
fromEvent<StorageEvent>(window, 'storage').pipe(
12+
filter(event => event.key === STORAGE_KEY),
13+
map((event) => (event.newValue ? JSON.parse(event.newValue) : []) as string[])
14+
),
15+
{ initialValue: [] as string[] }
16+
)
1117

12-
constructor() {
13-
fromEvent<StorageEvent>(window, 'storage').subscribe(event => {
14-
if (event.key === STORAGE_KEY) {
15-
const updatedList = event.newValue ? JSON.parse(event.newValue) : [];
16-
this._list.set(updatedList);
17-
}
18-
});
19-
}
18+
private _list = linkedSignal({
19+
source: () => this.listSignal(),
20+
computation: (list) => list?.length ? list : this.loadList()
21+
});
22+
23+
readonly list = this._list.asReadonly();
2024

2125
private loadList(): string[] {
2226
const data = localStorage.getItem(STORAGE_KEY);

src/assets/examples/advanced/example10/example10.component.ts.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import { JsonPipe, CommonModule } from '@angular/common';
1515
styleUrl: './advanced-example10.component.scss'
1616
})
1717
export class AdvancedExample10Component {
18-
list = this.collabService.list;
19-
newItem = '';
18+
private readonly collabService = inject(CollaborativeListService);
2019

21-
constructor(private collabService: CollaborativeListService) {}
20+
public list = this.collabService.list;
21+
public newItem = '';
2222

2323
add() {
2424
if (this.newItem.trim()) {

src/assets/examples/advanced/example10/example10.service.ts.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
```typescript
2-
import { Injectable, signal } from '@angular/core';
3-
import { fromEvent } from 'rxjs';
2+
import { Injectable, linkedSignal, signal } from '@angular/core';
3+
import { toSignal } from '@angular/core/rxjs-interop';
4+
import { filter, fromEvent, map } from 'rxjs';
45

56
const STORAGE_KEY = 'collab-list';
67

78
@Injectable({ providedIn: 'root' })
89
export class CollaborativeListService {
9-
private _list = signal<string[]>(this.loadList());
1010

11-
readonly list = this._list.asReadonly();
11+
private listSignal = toSignal(
12+
fromEvent<StorageEvent>(window, 'storage').pipe(
13+
filter(event => event.key === STORAGE_KEY),
14+
map((event) => (event.newValue ? JSON.parse(event.newValue) : []) as string[])
15+
),
16+
{ initialValue: [] as string[] }
17+
)
1218

13-
constructor() {
14-
fromEvent<StorageEvent>(window, 'storage').subscribe(event => {
15-
if (event.key === STORAGE_KEY) {
16-
const updatedList = event.newValue ? JSON.parse(event.newValue) : [];
17-
this._list.set(updatedList);
18-
}
19-
});
20-
}
19+
private _list = linkedSignal({
20+
source: () => this.listSignal(),
21+
computation: (list) => list?.length ? list : this.loadList()
22+
});
23+
24+
readonly list = this._list.asReadonly();
2125

2226
private loadList(): string[] {
2327
const data = localStorage.getItem(STORAGE_KEY);
@@ -48,7 +52,7 @@ export class CollaborativeListService {
4852

4953
clear() {
5054
this._list.set([]);
51-
localStorage.setItem(STORAGE_KEY, JSON.stringify([]));
55+
localStorage.removeItem('collab-list');
5256
}
5357
}
5458
```

0 commit comments

Comments
 (0)