All files / services sidebar.service.ts

100% Statements 17/17
75% Branches 6/8
100% Functions 8/8
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 341x           1x 11x   11x 12x       13x 13x 13x 13x         4x 4x 4x 4x         2x      
import { Injectable, signal, computed } from '@angular/core';
import { SidebarAction } from '@drevo-web/shared';
 
@Injectable({
    providedIn: 'root',
})
export class SidebarService {
    private readonly actionsMap = signal<Map<string, SidebarAction>>(new Map());
 
    readonly actions = computed(() =>
        Array.from(this.actionsMap().values()).sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
    );
 
    registerAction(action: SidebarAction): void {
        this.actionsMap.update(map => {
            const newMap = new Map(map);
            newMap.set(action.id, action);
            return newMap;
        });
    }
 
    unregisterAction(id: string): void {
        this.actionsMap.update(map => {
            const newMap = new Map(map);
            newMap.delete(id);
            return newMap;
        });
    }
 
    clear(): void {
        this.actionsMap.set(new Map());
    }
}