Skip to content

Commit

Permalink
Migrate client app to signals
Browse files Browse the repository at this point in the history
  • Loading branch information
mucsi96 committed Aug 12, 2024
1 parent 231eab4 commit d952af3
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 77 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Migrate client app to signals
4 changes: 2 additions & 2 deletions demo_client/src/app/message/message.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@if($loading | async) {
@if(loading()) {
<div role="progressbar" bt></div>
} @else {
<h1 bt>{{ ($message | async)?.message }}</h1>
<h1 bt>{{ message()?.message }}</h1>
}
31 changes: 0 additions & 31 deletions demo_client/src/app/message/message.component.spec.ts

This file was deleted.

12 changes: 5 additions & 7 deletions demo_client/src/app/message/message.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { AsyncPipe } from '@angular/common';
import { Component } from '@angular/core';
import { Component, Signal } from '@angular/core';
import { Message, MessageService } from './message.service';
import { loading } from '../utils/loading';
import { Observable } from 'rxjs';

@Component({
standalone: true,
Expand All @@ -12,11 +10,11 @@ import { Observable } from 'rxjs';
styleUrls: ['./message.component.css'],
})
export class MessageComponent {
$message: Observable<Message>;
$loading: Observable<boolean>;
message: Signal<Message | undefined>;
loading: Signal<boolean>;

constructor(private readonly messageService: MessageService) {
this.$message = this.messageService.getMessage();
this.$loading = this.$message.pipe(loading());
this.message = this.messageService.getMessage();
this.loading = this.messageService.isMessageLoading();
}
}
26 changes: 0 additions & 26 deletions demo_client/src/app/message/message.service.spec.ts

This file was deleted.

20 changes: 16 additions & 4 deletions demo_client/src/app/message/message.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Injectable, Signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { Observable, shareReplay } from 'rxjs';
import { toLoadingSignal } from '../utils/loading';

export type Message = {
message: string;
};

@Injectable()
export class MessageService {
constructor(private readonly http: HttpClient) {}
$messages: Observable<Message>;

getMessage(): Observable<Message> {
return this.http.get<Message>('/api/message').pipe(shareReplay(1));
constructor(private readonly http: HttpClient) {
this.$messages = this.http
.get<Message>('/api/message')
.pipe(shareReplay(1));
}

getMessage(): Signal<Message | undefined> {
return toSignal(this.$messages);
}

isMessageLoading(): Signal<boolean> {
return toLoadingSignal(this.$messages);
}
}
17 changes: 11 additions & 6 deletions demo_client/src/app/utils/loading.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { catchError, map, of, pipe, startWith } from 'rxjs';
import { Signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { catchError, map, Observable, of, startWith } from 'rxjs';

export function loading() {
return pipe(
map(() => false),
startWith(true),
catchError(() => of(false))
export function toLoadingSignal($data: Observable<unknown>): Signal<boolean> {
return toSignal(
$data.pipe(
map(() => false),
startWith(true),
catchError(() => of(false))
),
{ initialValue: true }
);
}

0 comments on commit d952af3

Please sign in to comment.