Skip to content

Commit

Permalink
feat: add error handling for adding messages
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuamorony committed Oct 10, 2023
1 parent e040260 commit 2e28ad8
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/app/shared/data-access/message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AuthService } from './auth.service';

interface MessageState {
messages: Message[];
error: string | null;
}

@Injectable({
Expand All @@ -29,15 +30,18 @@ export class MessageService {
})
);
add$ = new Subject<Message['content']>();
error$ = new Subject<string>();
logout$ = this.authUser$.pipe(filter((user) => !user));

// state
private state = signal<MessageState>({
messages: [],
error: null,
});

// selectors
messages = computed(() => this.state().messages);
error = computed(() => this.state().error);

constructor() {
// reducers
Expand All @@ -53,13 +57,24 @@ export class MessageService {
takeUntilDestroyed(),
exhaustMap((message) => this.addMessage(message))
)
.subscribe();
.subscribe({
error: (err) => {
console.log(err);
this.error$.next('Failed to send message');
},
});

this.logout$
.pipe(takeUntilDestroyed())
.subscribe(() =>
this.state.update((state) => ({ ...state, messages: [] }))
);

this.error$
.pipe(takeUntilDestroyed())
.subscribe((error) =>
this.state.update((state) => ({ ...state, error }))
);
}

private getMessages() {
Expand Down

0 comments on commit 2e28ad8

Please sign in to comment.