@directus/composables 11.3.0 → 11.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +30 -36
  2. package/package.json +5 -4
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { computed, defineComponent, inject, isRef, nextTick, onBeforeUnmount, onMounted, onUnmounted, provide, reactive, ref, shallowRef, toRef, toRefs, unref, watch } from "vue";
2
2
  import { API_INJECT, EXTENSIONS_INJECT, SDK_INJECT, STORES_INJECT } from "@directus/constants";
3
3
  import { nanoid } from "nanoid";
4
- import { capitalize, isEqual, isNil, throttle } from "lodash-es";
4
+ import { capitalize, isEmpty, isEqual, isNil, throttle } from "lodash-es";
5
5
  import { getEndpoint, moveInArray } from "@directus/utils";
6
+ import { useMemoize } from "@vueuse/core";
6
7
  import axios from "axios";
7
8
 
8
9
  //#region src/use-system.ts
@@ -884,12 +885,24 @@ function useItems(collection, query) {
884
885
  if (itemCount.value < (unref(limit) ?? 100)) return 1;
885
886
  return Math.ceil(itemCount.value / (unref(limit) ?? 100));
886
887
  });
887
- const existingRequests = {
888
- items: null,
889
- total: null,
890
- filter: null
891
- };
888
+ let itemsAbort = null;
889
+ let totalCountGeneration = 0;
890
+ let itemCountGeneration = 0;
892
891
  let loadingTimeout = null;
892
+ const fetchAggregate = useMemoize(async (url, filter$1, search$1) => {
893
+ const aggregate = primaryKeyField.value ? { countDistinct: primaryKeyField.value.field } : { count: "*" };
894
+ const response = await api.get(url, { params: {
895
+ aggregate,
896
+ ...filter$1 ? { filter: filter$1 } : {},
897
+ ...search$1 ? { search: search$1 } : {}
898
+ } });
899
+ return primaryKeyField.value ? Number(response.data.data[0].countDistinct[primaryKeyField.value.field]) : Number(response.data.data[0].count);
900
+ }, { getKey(url, filter$1, search$1) {
901
+ const key = { url };
902
+ if (!isEmpty(filter$1)) key.filter = filter$1;
903
+ if (!isEmpty(search$1)) key.search = search$1;
904
+ return JSON.stringify(key);
905
+ } });
893
906
  const fetchItems = throttle((shouldUpdateCount) => {
894
907
  Promise.all([getItems(), shouldUpdateCount ? getItemCount() : Promise.resolve()]);
895
908
  }, 500);
@@ -940,8 +953,8 @@ function useItems(collection, query) {
940
953
  async function getItems() {
941
954
  if (!endpoint.value) return;
942
955
  let isCurrentRequestCanceled = false;
943
- if (existingRequests.items) existingRequests.items.abort();
944
- existingRequests.items = new AbortController();
956
+ if (itemsAbort) itemsAbort.abort();
957
+ itemsAbort = new AbortController();
945
958
  error.value = null;
946
959
  if (loadingTimeout) clearTimeout(loadingTimeout);
947
960
  loadingTimeout = setTimeout(() => {
@@ -962,9 +975,9 @@ function useItems(collection, query) {
962
975
  filter: unref(filter),
963
976
  deep: unref(deep)
964
977
  },
965
- signal: existingRequests.items.signal
978
+ signal: itemsAbort.signal
966
979
  })).data.data;
967
- existingRequests.items = null;
980
+ itemsAbort = null;
968
981
  /**
969
982
  * @NOTE
970
983
  *
@@ -1011,19 +1024,10 @@ function useItems(collection, query) {
1011
1024
  }
1012
1025
  async function getTotalCount() {
1013
1026
  if (!endpoint.value) return;
1027
+ const currentGeneration = ++totalCountGeneration;
1014
1028
  try {
1015
- if (existingRequests.total) existingRequests.total.abort();
1016
- existingRequests.total = new AbortController();
1017
- const aggregate = primaryKeyField.value ? { countDistinct: primaryKeyField.value.field } : { count: "*" };
1018
- const response = await api.get(endpoint.value, {
1019
- params: {
1020
- aggregate,
1021
- filter: unref(filterSystem)
1022
- },
1023
- signal: existingRequests.total.signal
1024
- });
1025
- const count = primaryKeyField.value ? Number(response.data.data[0].countDistinct[primaryKeyField.value.field]) : Number(response.data.data[0].count);
1026
- existingRequests.total = null;
1029
+ const count = await fetchAggregate(endpoint.value, filterSystem?.value, void 0);
1030
+ if (currentGeneration !== totalCountGeneration) return;
1027
1031
  totalCount.value = count;
1028
1032
  } catch (err) {
1029
1033
  if (!axios.isCancel(err)) throw err;
@@ -1031,26 +1035,16 @@ function useItems(collection, query) {
1031
1035
  }
1032
1036
  async function getItemCount() {
1033
1037
  if (!endpoint.value) return;
1038
+ const currentGeneration = ++itemCountGeneration;
1034
1039
  loadingItemCount.value = true;
1035
1040
  try {
1036
- if (existingRequests.filter) existingRequests.filter.abort();
1037
- existingRequests.filter = new AbortController();
1038
- const aggregate = primaryKeyField.value ? { countDistinct: primaryKeyField.value.field } : { count: "*" };
1039
- const response = await api.get(endpoint.value, {
1040
- params: {
1041
- filter: unref(filter),
1042
- search: unref(search),
1043
- aggregate
1044
- },
1045
- signal: existingRequests.filter.signal
1046
- });
1047
- const count = primaryKeyField.value ? Number(response.data.data[0].countDistinct[primaryKeyField.value.field]) : Number(response.data.data[0].count);
1048
- existingRequests.filter = null;
1041
+ const count = await fetchAggregate(endpoint.value, filter.value, search.value);
1042
+ if (currentGeneration !== itemCountGeneration) return;
1049
1043
  itemCount.value = count;
1050
1044
  } catch (err) {
1051
1045
  if (!axios.isCancel(err)) throw err;
1052
1046
  } finally {
1053
- loadingItemCount.value = false;
1047
+ if (currentGeneration === itemCountGeneration) loadingItemCount.value = false;
1054
1048
  }
1055
1049
  }
1056
1050
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@directus/composables",
3
- "version": "11.3.0",
3
+ "version": "11.4.0",
4
4
  "description": "Shared Vue composables for Directus use",
5
5
  "homepage": "https://directus.io",
6
6
  "repository": {
@@ -22,7 +22,8 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "axios": "1.13.5",
25
+ "@vueuse/core": "14.0.0",
26
+ "axios": "1.15.0",
26
27
  "lodash-es": "4.18.1",
27
28
  "nanoid": "5.1.6",
28
29
  "@directus/utils": "13.4.0",
@@ -37,8 +38,8 @@
37
38
  "typescript": "5.9.3",
38
39
  "vitest": "3.2.4",
39
40
  "vue": "3.5.24",
40
- "@directus/extensions": "3.0.23",
41
- "@directus/types": "15.0.1",
41
+ "@directus/extensions": "3.0.24",
42
+ "@directus/types": "15.0.2",
42
43
  "@directus/sdk": "21.2.2"
43
44
  },
44
45
  "peerDependencies": {