feat: add notifier service

This commit is contained in:
2025-12-20 18:24:12 -03:00
parent 70ff68930d
commit 7c545e2b1e
2 changed files with 52 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { Notifier } from './notifier';
import { Notification } from '../models/Notification';
describe('Notifier', () => {
let service: Notifier;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(Notifier);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should set notification on notify and clean it after 800ms', fakeAsync(() => {
TestBed.tick();
const NOTIFICATION_MOCK = new Notification('MOCK NOTIFICATION');
service.notify(NOTIFICATION_MOCK);
expect(service.notification$()).toEqual(NOTIFICATION_MOCK);
TestBed.tick();
tick(1000);
expect(service.notification$()).toEqual(null);
}));
});

View File

@@ -0,0 +1,25 @@
import { effect, Injectable, signal } from '@angular/core';
import { Notification } from '../models/Notification';
@Injectable({
providedIn: 'root',
})
export class Notifier {
private readonly notification = signal<Notification | null>(null);
readonly notification$ = this.notification.asReadonly();
constructor() {
effect(() => {
if (this.notification$()) {
setTimeout(() => {
this.cleanup();
}, 800);
}
});
}
notify(notification: Notification) {
this.notification.set(notification);
}
private cleanup() {
this.notification.set(null);
}
}