From a329c32c6ea3f14d698f959e81b1f668d0b772cc Mon Sep 17 00:00:00 2001 From: rduteil Date: Mon, 26 Feb 2024 14:30:21 +0100 Subject: [PATCH] onUnmounted fix in composableFactory --- src/Bones.UI/core/composableFactory.ts | 53 +++++++++++++++++--------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/src/Bones.UI/core/composableFactory.ts b/src/Bones.UI/core/composableFactory.ts index a6aa596..ed0ccb2 100644 --- a/src/Bones.UI/core/composableFactory.ts +++ b/src/Bones.UI/core/composableFactory.ts @@ -6,10 +6,15 @@ import { onCollectionChanged, onEntityChanged } from "../tools"; export class ComposableFactory { public static get(factory: () => { get(id: string): Promise } & INotifyService) { + return () => { + const service = factory(); + let subscribersIds: number[] = []; - const service = factory(); + onUnmounted(() => { + subscribersIds.forEach(id => service.unsubscribe(id)); + subscribersIds = []; + }); - return () => { const getting = ref(false); const entity = ref(null) as Ref; @@ -22,8 +27,7 @@ export class ComposableFactory { getting.value = false; } - const subscriberId = service.subscribe("all", onEntityChanged(entity)) - onUnmounted(() => service.unsubscribe(subscriberId)); + subscribersIds.push(service.subscribe("all", onEntityChanged(entity))); return entity; } @@ -37,10 +41,15 @@ export class ComposableFactory { } public static getMany(factory: () => { getMany(filter?: TFilter): Promise } & INotifyService) { + return () => { + const service = factory(); + let subscribersIds: number[] = []; - const service = factory(); + onUnmounted(() => { + subscribersIds.forEach(id => service.unsubscribe(id)); + subscribersIds = []; + }); - return () => { const fetching = ref(false); const entities = ref([]) as Ref; @@ -55,8 +64,7 @@ export class ComposableFactory { const filterMethod = customFilter || (filter ? FilterFactory.create(filter) : (el: TInfos) => true); - const subscriberId = service.subscribe("all", onCollectionChanged(entities, filterMethod)) - onUnmounted(() => service.unsubscribe(subscriberId)); + subscribersIds.push(service.subscribe("all", onCollectionChanged(entities, filterMethod))); return entities; } @@ -71,10 +79,15 @@ export class ComposableFactory { public static create(factory: () => { create(payload: TCreateDTO): Promise } & INotifyService) { + return () => { + const service = factory(); + let subscribersIds: number[] = []; - const service = factory(); + onUnmounted(() => { + subscribersIds.forEach(id => service.unsubscribe(id)); + subscribersIds = []; + }); - return () => { const creating = ref(false); const created = ref(null) as Ref; @@ -87,8 +100,7 @@ export class ComposableFactory { creating.value = false; } - const subscriberId = service.subscribe("all", onEntityChanged(created)) - onUnmounted(() => service.unsubscribe(subscriberId)); + subscribersIds.push(service.subscribe("all", onEntityChanged(created))); return created as Ref; } @@ -102,9 +114,15 @@ export class ComposableFactory { } public static update(factory: () => { update(id: string, payload: TUpdateDTO): Promise } & INotifyService) { - const service = factory(); - return () => { + const service = factory(); + let subscribersIds: number[] = []; + + onUnmounted(() => { + subscribersIds.forEach(id => service.unsubscribe(id)); + subscribersIds = []; + }); + const updating = ref(false); const updated = ref(null) as Ref; @@ -117,8 +135,7 @@ export class ComposableFactory { updating.value = false; } - const subscriberId = service.subscribe("all", onEntityChanged(updated)) - onUnmounted(() => service.unsubscribe(subscriberId)); + subscribersIds.push(service.subscribe("all", onEntityChanged(updated))); return updated.value as Ref; } @@ -132,9 +149,9 @@ export class ComposableFactory { } public static remove(factory: () => { remove(id: string): Promise }) { - const service = factory(); - return () => { + const service = factory(); + const removing = ref(false); const remove = async (id: string) => {