@hf-chimera/store
Version:
Cross-end reactivity API
91 lines (89 loc) • 2.61 kB
JavaScript
import "../defaults-CLUQg2zK.js";
import "../src-C74sq0jQ.js";
import "../qb-pchs-vdM.js";
import { t as normalizeParams } from "../params-DmOyCS2B.js";
import { computed, customRef, isRef, watch } from "vue";
//#region packages/adapters/vue/composables.ts
const isFunction = (value) => typeof value === "function";
const toValue = (value) => isFunction(value) ? value() : isRef(value) ? value.value : value;
const CHIMERA_COLLECTION_UPDATE_EVENTS = [
"ready",
"updated",
"selfUpdated",
"selfItemCreated",
"itemAdded",
"itemUpdated",
"selfItemUpdated",
"itemDeleted",
"selfItemDeleted",
"error"
];
const CHIMERA_ITEM_UPDATE_EVENTS = [
"initialized",
"selfCreated",
"ready",
"updated",
"selfUpdated",
"deleted",
"selfDeleted",
"error"
];
const createChimeraComposables = (store) => {
const useChimeraRepository = (entityName) => computed(() => store.from(toValue(entityName)));
return {
useChimeraStore: () => store,
useChimeraRepository,
useChimeraCollection: (entityName, params) => {
const repository = useChimeraRepository(entityName);
const normalizedParams = computed(() => normalizeParams(toValue(params)));
const collection = computed(() => repository.value.getCollection(normalizedParams.value));
return customRef((track, trigger) => {
watch(collection, (collection$1, _, onCleanup) => {
const handler = () => trigger();
CHIMERA_COLLECTION_UPDATE_EVENTS.forEach((event) => {
collection$1.on(event, handler);
});
onCleanup(() => CHIMERA_COLLECTION_UPDATE_EVENTS.forEach((event) => {
collection$1.off(event, handler);
}));
}, { immediate: true });
return {
get() {
track();
return collection.value;
},
set() {
console.warn("ChimeraCollectionRef is readonly");
}
};
});
},
useChimeraItem: (entityName, id, meta) => {
const repository = useChimeraRepository(entityName);
const item = computed(() => repository.value.getItem(toValue(id), toValue(meta)));
return customRef((track, trigger) => {
watch(item, (item$1, _, onCleanup) => {
const handler = () => trigger();
CHIMERA_ITEM_UPDATE_EVENTS.forEach((event) => {
item$1.on(event, handler);
});
onCleanup(() => CHIMERA_ITEM_UPDATE_EVENTS.forEach((event) => {
item$1.off(event, handler);
}));
}, { immediate: true });
return {
get() {
track();
return item.value;
},
set() {
console.warn("ChimeraItemRef is readonly");
}
};
});
}
};
};
//#endregion
export { createChimeraComposables };
//# sourceMappingURL=vue.js.map