Skip to content

Commit

Permalink
may improve getMany mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin SCHROTER committed Sep 25, 2024
1 parent 2bc82eb commit 6b5bda2
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Bones.UI/core/composableFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ref, onUnmounted, ref } from "vue";
import { Ref, onUnmounted, ref, computed } from "vue";

import { FilterFactory } from "./filterFactory";
import { INotifyService } from "../abstractions";
Expand Down Expand Up @@ -105,12 +105,15 @@ export class ComposableFactory {
});

const fetching = ref(false);
const entities = ref<TInfos[]>([]) as Ref<TInfos[]>;
const _entities = ref<TInfos[]>([]) as Ref<TInfos[]>;
let _filter: (el: TInfos) => boolean = () => true;

const getMany = async (...args: [...TArgs, ((el: TDetails) => boolean)?]) => {
const entities = computed(() => _entities.value.filter(e => _filter(e)));

const getMany = async (...args: [...TArgs, ((el: TInfos) => boolean)?]) => {
fetching.value = true;

let customFilter: ((el: TDetails) => boolean) | undefined = undefined
let customFilter: ((el: TInfos) => boolean) | undefined = undefined

if (args.length > 1 && typeof args[args.length - 1] === 'function') {
customFilter = args.pop();
Expand All @@ -119,21 +122,22 @@ export class ComposableFactory {
let actualArgs = args as unknown as TArgs;

try {
entities.value = await method(...actualArgs);
if (apply) apply(entities)
_entities.value = await method(...actualArgs);
if (apply) apply(_entities)
}
finally {
fetching.value = false;
}

const filterMethod = customFilter || (actualArgs.length > 0 ? FilterFactory.create(actualArgs[0]) : () => true);
_filter = filterMethod

subscribersIds.push(service.subscribe("all", onCollectionChanged(entities, filterMethod)));
subscribersIds.push(service.subscribe("all", onCollectionChanged(_entities)));
subscribersIds.push(service.subscribe("reset", async () => {
fetching.value = true;
try {
entities.value = await method(...actualArgs);
if (apply) apply(entities)
_entities.value = await method(...actualArgs);
if (apply) apply(_entities)
}
finally {
fetching.value = false;
Expand Down

0 comments on commit 6b5bda2

Please sign in to comment.