mirror of
https://github.com/Kareadita/Kavita.git
synced 2026-04-02 23:33:41 -04:00
Co-authored-by: KindlyFire <10267586+kindlyfire@users.noreply.github.com> Co-authored-by: Hosted Weblate <hosted@weblate.org> Co-authored-by: Adam Havránek <adamhavra@seznam.cz> Co-authored-by: Aindriú Mac Giolla Eoin <aindriu80@gmail.com> Co-authored-by: Alexey <lewadedun@gmail.com> Co-authored-by: Anon Bitardov <timurvolga23+weblate@gmail.com> Co-authored-by: Ferran <ferrancette@gmail.com> Co-authored-by: Gneb <goozi12345@gmail.com> Co-authored-by: Robin Stolpe <robinstolpe@slashmad.com> Co-authored-by: 안세훈 <on9686@gmail.com> Co-authored-by: Tijl Van den Brugghen <contact@tijlvdb.me>
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import {WritableSignal} from "@angular/core";
|
|
|
|
export function patchArray<T extends { id: number }>(arr: T[], entityMap: Map<number, any>): T[] {
|
|
let changed = false;
|
|
const result = arr.map(item => {
|
|
const updated = entityMap.get(item.id);
|
|
if (updated) {
|
|
changed = true;
|
|
return { ...updated } as T;
|
|
}
|
|
return item;
|
|
});
|
|
return changed ? result : arr;
|
|
}
|
|
|
|
export function patchSignalArray<T extends { id: number }>(signal: WritableSignal<T[]>, entityMap: Map<number, any>): void {
|
|
const patched = patchArray(signal(), entityMap);
|
|
if (patched !== signal()) {
|
|
signal.set([...patched]);
|
|
}
|
|
}
|
|
|
|
export function patchEntity<T>(arr: T[], entity: T, selector: (item: T) => any = (item: any) => item.id): T[] {
|
|
const entityKey = selector(entity);
|
|
const idx = arr.findIndex(item => selector(item) === entityKey);
|
|
if (idx === -1) return arr;
|
|
const result = [...arr];
|
|
result[idx] = { ...entity };
|
|
return result;
|
|
}
|
|
|
|
export function patchEntitySignal<T>(signal: WritableSignal<T[]>, entity: T, selector?: (item: T) => any): void {
|
|
const patched = patchEntity(signal(), entity, selector);
|
|
if (patched !== signal()) {
|
|
signal.set(patched);
|
|
}
|
|
}
|