Skip to content

Commit

Permalink
update composable factory to expose ref
Browse files Browse the repository at this point in the history
  • Loading branch information
SchroterQuentin committed Apr 2, 2024
1 parent d04d596 commit 82e9edc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
75 changes: 59 additions & 16 deletions src/Bones.UI/core/composableFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { onCollectionChanged, onEntityChanged } from "../tools";
type CFunc<TName extends string, TArgs, TResult> = Record<TName, (args: TArgs) => Promise<TResult>>

export class ComposableFactory {
public static get<TDetails>(service: { get(id: string): Promise<TDetails> } & INotifyService<TDetails>, applyFactory?: () => (entity: TDetails) => void) {
public static get<TDetails>(service: { get(id: string): Promise<TDetails> } & INotifyService<TDetails>, applyFactory?: () => (entity: Ref<TDetails>) => void) {
return () => {
const apply = applyFactory ? applyFactory() : () => { };
let subscribersIds: number[] = [];
Expand All @@ -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<TDetails>);
}
finally {
getting.value = false;
Expand All @@ -43,7 +43,7 @@ export class ComposableFactory {
}
}

public static getMany<TDetails extends TInfos, TInfos, TFilter>(service: { getMany(filter?: TFilter): Promise<TInfos[]> } & INotifyService<TDetails>, applyFactory?: () => (entity: TInfos) => void) {
public static getMany<TDetails extends TInfos, TInfos, TFilter>(service: { getMany(filter?: TFilter): Promise<TInfos[]> } & INotifyService<TDetails>, applyFactory?: () => (entities: Ref<TInfos[]>) => void) {
return () => {
const apply = applyFactory ? applyFactory() : () => { };
let subscribersIds: number[] = [];
Expand All @@ -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;
Expand Down Expand Up @@ -117,15 +113,14 @@ export class ComposableFactory {
return () => {
let subscribersIds: number[] = [];

const synceds = ref([]) as Ref<TInfos[]>;

onUnmounted(() => {
subscribersIds.forEach(id => service.unsubscribe(id));
subscribersIds = [];
});

const sync = <TFilter>(entities: TInfos[], filter?: TFilter, customFilter?: (el: TDetails) => boolean) => {
synceds.value = entities;
const synceds = ref(entities) as Ref<TInfos[]>;

const filterMethod = customFilter || (filter ? FilterFactory.create(filter) : (el: TInfos) => true);

Expand All @@ -135,7 +130,30 @@ export class ComposableFactory {
}

return {
synceds,
sync
}
}
}

public static syncRef<TDetails extends TInfos, TInfos>(service: INotifyService<TDetails>) {
return () => {
let subscribersIds: number[] = [];


onUnmounted(() => {
subscribersIds.forEach(id => service.unsubscribe(id));
subscribersIds = [];
});

const sync = <TFilter>(entities: Ref<TInfos[]>, 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
}
}
Expand All @@ -150,10 +168,31 @@ export class ComposableFactory {
subscribersIds = [];
});

const track = (entity: Ref<TDetails>, 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<TDetails>;

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<TDetails>(service: INotifyService<TDetails>) {
return () => {
let subscribersIds: number[] = [];

onUnmounted(() => {
subscribersIds.forEach(id => service.unsubscribe(id));
subscribersIds = [];
});

const track = (entity: Ref<TDetails>, onChanged?: ((entity: Ref<TDetails>) => void)) => {
subscribersIds.push(service.subscribe("all", onEntityChanged(entity, onChanged)));

return entity;
}
Expand All @@ -164,8 +203,9 @@ export class ComposableFactory {
}
}

public static create<TCreateDTO, TDetails>(service: { create(payload: TCreateDTO): Promise<TDetails> } & INotifyService<TDetails>) {
public static create<TCreateDTO, TDetails>(service: { create(payload: TCreateDTO): Promise<TDetails> } & INotifyService<TDetails>, applyFactory?: () => (entity: Ref<TDetails>) => void) {
return () => {
const apply = applyFactory ? applyFactory() : () => { };
let subscribersIds: number[] = [];

onUnmounted(() => {
Expand All @@ -180,6 +220,7 @@ export class ComposableFactory {
creating.value = true;
try {
created.value = await service.create(payload);
if (apply) apply(created as Ref<TDetails>);
}
finally {
creating.value = false;
Expand All @@ -198,8 +239,9 @@ export class ComposableFactory {
}
}

public static update<TUpdateDTO, TDetails>(service: { update(id: string, payload: TUpdateDTO): Promise<TDetails> } & INotifyService<TDetails>) {
public static update<TUpdateDTO, TDetails>(service: { update(id: string, payload: TUpdateDTO): Promise<TDetails> } & INotifyService<TDetails>, applyFactory?: () => (entity: Ref<TDetails>) => void) {
return () => {
const apply = applyFactory ? applyFactory() : () => { };
let subscribersIds: number[] = [];

onUnmounted(() => {
Expand All @@ -214,6 +256,7 @@ export class ComposableFactory {
updating.value = true;
try {
updated.value = await service.update(id, payload);
if (apply) apply(updated as Ref<TDetails>);
}
finally {
updating.value = false;
Expand Down
4 changes: 2 additions & 2 deletions src/Bones.UI/tools/notifyTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function onCollectionChanged<TInfos, TDetails extends TInfos>(

export function onEntityChanged<TDetails>(
entity: Ref<TDetails | null>,
onChanged?: (entity: TDetails) => void,
onChanged?: (entity: Ref<TDetails>) => void,
identifier: (e1: TDetails) => any = e1 => (e1 as any).id): AllCallback<TDetails> {

const result: AllCallback<TDetails> = (ev: NotifyEvent, payload: any) => {
Expand All @@ -35,7 +35,7 @@ export function onEntityChanged<TDetails>(
if (ev === "add" || ev === "update") {
if (id === identifier(payload)) {
entity.value = payload;
if (onChanged) onChanged(payload);
if (onChanged) onChanged(entity as Ref<TDetails>);
}
}

Expand Down

0 comments on commit 82e9edc

Please sign in to comment.