From 82e9edc2db39666d3c8614826346773aa6d572db Mon Sep 17 00:00:00 2001 From: qschroter Date: Tue, 2 Apr 2024 23:05:19 +0200 Subject: [PATCH] update composable factory to expose ref --- src/Bones.UI/core/composableFactory.ts | 75 ++++++++++++++++++++------ src/Bones.UI/tools/notifyTools.ts | 4 +- 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/src/Bones.UI/core/composableFactory.ts b/src/Bones.UI/core/composableFactory.ts index 4c7f16d..4dfa9f1 100644 --- a/src/Bones.UI/core/composableFactory.ts +++ b/src/Bones.UI/core/composableFactory.ts @@ -7,7 +7,7 @@ import { onCollectionChanged, onEntityChanged } from "../tools"; type CFunc = Record Promise> export class ComposableFactory { - public static get(service: { get(id: string): Promise } & INotifyService, applyFactory?: () => (entity: TDetails) => void) { + public static get(service: { get(id: string): Promise } & INotifyService, applyFactory?: () => (entity: Ref) => void) { return () => { const apply = applyFactory ? applyFactory() : () => { }; let subscribersIds: number[] = []; @@ -24,7 +24,7 @@ export class ComposableFactory { getting.value = true; try { entity.value = await service.get(id); - if (apply) apply(entity.value); + if (apply) apply(entity as Ref); } finally { getting.value = false; @@ -43,7 +43,7 @@ export class ComposableFactory { } } - public static getMany(service: { getMany(filter?: TFilter): Promise } & INotifyService, applyFactory?: () => (entity: TInfos) => void) { + public static getMany(service: { getMany(filter?: TFilter): Promise } & INotifyService, applyFactory?: () => (entities: Ref) => void) { return () => { const apply = applyFactory ? applyFactory() : () => { }; let subscribersIds: number[] = []; @@ -60,11 +60,7 @@ export class ComposableFactory { fetching.value = true; try { entities.value = await service.getMany(filter); - if (apply) { - for (const entity of entities.value) { - apply(entity); - } - } + if (apply) apply(entities) } finally { fetching.value = false; @@ -117,7 +113,6 @@ export class ComposableFactory { return () => { let subscribersIds: number[] = []; - const synceds = ref([]) as Ref; onUnmounted(() => { subscribersIds.forEach(id => service.unsubscribe(id)); @@ -125,7 +120,7 @@ export class ComposableFactory { }); const sync = (entities: TInfos[], filter?: TFilter, customFilter?: (el: TDetails) => boolean) => { - synceds.value = entities; + const synceds = ref(entities) as Ref; const filterMethod = customFilter || (filter ? FilterFactory.create(filter) : (el: TInfos) => true); @@ -135,7 +130,30 @@ export class ComposableFactory { } return { - synceds, + sync + } + } + } + + public static syncRef(service: INotifyService) { + return () => { + let subscribersIds: number[] = []; + + + onUnmounted(() => { + subscribersIds.forEach(id => service.unsubscribe(id)); + subscribersIds = []; + }); + + const sync = (entities: Ref, filter?: TFilter, customFilter?: (el: TDetails) => boolean) => { + const filterMethod = customFilter || (filter ? FilterFactory.create(filter) : (el: TInfos) => true); + + subscribersIds.push(service.subscribe("all", onCollectionChanged(entities, filterMethod))); + + return entities; + } + + return { sync } } @@ -150,10 +168,31 @@ export class ComposableFactory { subscribersIds = []; }); - const track = (entity: Ref, onChanged?: ((entity: TDetails) => void), immediate: boolean = true) => { - subscribersIds.push(service.subscribe("all", onEntityChanged(entity, onChanged))); + const track = (initialValue: TDetails, setter?: ((entity: TDetails) => void)) => { + const entity = ref(initialValue) as Ref; + + subscribersIds.push(service.subscribe("all", onEntityChanged(entity, (ref) => setter ? setter(ref.value) : null))); + + return entity; + } + + return { + track + } + } + } - if (onChanged && immediate) onChanged(entity.value); + public static trackRef(service: INotifyService) { + return () => { + let subscribersIds: number[] = []; + + onUnmounted(() => { + subscribersIds.forEach(id => service.unsubscribe(id)); + subscribersIds = []; + }); + + const track = (entity: Ref, onChanged?: ((entity: Ref) => void)) => { + subscribersIds.push(service.subscribe("all", onEntityChanged(entity, onChanged))); return entity; } @@ -164,8 +203,9 @@ export class ComposableFactory { } } - public static create(service: { create(payload: TCreateDTO): Promise } & INotifyService) { + public static create(service: { create(payload: TCreateDTO): Promise } & INotifyService, applyFactory?: () => (entity: Ref) => void) { return () => { + const apply = applyFactory ? applyFactory() : () => { }; let subscribersIds: number[] = []; onUnmounted(() => { @@ -180,6 +220,7 @@ export class ComposableFactory { creating.value = true; try { created.value = await service.create(payload); + if (apply) apply(created as Ref); } finally { creating.value = false; @@ -198,8 +239,9 @@ export class ComposableFactory { } } - public static update(service: { update(id: string, payload: TUpdateDTO): Promise } & INotifyService) { + public static update(service: { update(id: string, payload: TUpdateDTO): Promise } & INotifyService, applyFactory?: () => (entity: Ref) => void) { return () => { + const apply = applyFactory ? applyFactory() : () => { }; let subscribersIds: number[] = []; onUnmounted(() => { @@ -214,6 +256,7 @@ export class ComposableFactory { updating.value = true; try { updated.value = await service.update(id, payload); + if (apply) apply(updated as Ref); } finally { updating.value = false; diff --git a/src/Bones.UI/tools/notifyTools.ts b/src/Bones.UI/tools/notifyTools.ts index fad6c01..1752f12 100644 --- a/src/Bones.UI/tools/notifyTools.ts +++ b/src/Bones.UI/tools/notifyTools.ts @@ -25,7 +25,7 @@ export function onCollectionChanged( export function onEntityChanged( entity: Ref, - onChanged?: (entity: TDetails) => void, + onChanged?: (entity: Ref) => void, identifier: (e1: TDetails) => any = e1 => (e1 as any).id): AllCallback { const result: AllCallback = (ev: NotifyEvent, payload: any) => { @@ -35,7 +35,7 @@ export function onEntityChanged( if (ev === "add" || ev === "update") { if (id === identifier(payload)) { entity.value = payload; - if (onChanged) onChanged(payload); + if (onChanged) onChanged(entity as Ref); } }