@yh-ui/request 1.0.54 → 1.0.56

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.
@@ -31,8 +31,16 @@ function useLoadMore(service, options = {}) {
31
31
  const loadingMore = (0, _vue.ref)(false);
32
32
  const error = (0, _vue.shallowRef)(void 0);
33
33
  const params = (0, _vue.ref)(defaultParams);
34
+ let lastRefreshTime = 0;
35
+ const lastLoadedPageSize = (0, _vue.ref)(null);
34
36
  const noMore = (0, _vue.computed)(() => {
35
- return current.value >= totalPages.value && total.value > 0;
37
+ if (total.value > 0) {
38
+ return current.value >= totalPages.value;
39
+ }
40
+ if (lastLoadedPageSize.value !== null && lastLoadedPageSize.value < pageSize.value) {
41
+ return true;
42
+ }
43
+ return false;
36
44
  });
37
45
  const canLoadMore = (0, _vue.computed)(() => {
38
46
  return isLoadMore && !noMore.value && !loadingMore.value;
@@ -42,23 +50,63 @@ function useLoadMore(service, options = {}) {
42
50
  if (loading.value || loadingMore.value) return;
43
51
  const isLoadMoreOp = !isRefresh;
44
52
  loading.value = isLoadMoreOp ? false : true;
45
- if (isRefresh) refreshing.value = true;
53
+ if (isRefresh) {
54
+ refreshing.value = true;
55
+ total.value = 0;
56
+ lastLoadedPageSize.value = null;
57
+ }
46
58
  if (isLoadMoreOp) loadingMore.value = true;
47
59
  error.value = void 0;
48
60
  try {
49
- const extraParams = params.value.slice(2);
61
+ const extraParams = params.value;
50
62
  const response = await loadService(requestedPage, pageSize.value, ...extraParams);
51
63
  const pageData = response.data;
64
+ let totalNum;
52
65
  if (pageData && typeof pageData === "object") {
53
66
  const paginatedData = pageData;
54
- if (typeof paginatedData.total !== "undefined") total.value = paginatedData.total;else if (typeof paginatedData.totalCount !== "undefined") total.value = paginatedData.totalCount;else if (typeof paginatedData.totalElements !== "undefined") total.value = paginatedData.totalElements;
67
+ if (typeof paginatedData.total !== "undefined") totalNum = paginatedData.total;else if (typeof paginatedData.totalCount !== "undefined") totalNum = paginatedData.totalCount;else if (typeof paginatedData.totalElements !== "undefined") totalNum = paginatedData.totalElements;
68
+ }
69
+ if (typeof totalNum === "undefined" && response && typeof response === "object") {
70
+ const resp = response;
71
+ if (typeof resp.total !== "undefined") totalNum = resp.total;else if (typeof resp.totalCount !== "undefined") totalNum = resp.totalCount;else if (typeof resp.totalElements !== "undefined") totalNum = resp.totalElements;
72
+ }
73
+ if (typeof totalNum !== "undefined") {
74
+ total.value = totalNum;
55
75
  }
76
+ let loadedSize = 0;
77
+ if (Array.isArray(pageData)) {
78
+ loadedSize = pageData.length;
79
+ } else if (pageData && typeof pageData === "object") {
80
+ const arrayKeys = Object.keys(pageData).filter(k => Array.isArray(pageData[k]));
81
+ if (arrayKeys.length > 0) {
82
+ const preferredKey = arrayKeys.find(k => ["list", "data", "items"].includes(k));
83
+ if (preferredKey) {
84
+ loadedSize = pageData[preferredKey].length;
85
+ } else {
86
+ loadedSize = pageData[arrayKeys[0]].length;
87
+ }
88
+ }
89
+ }
90
+ lastLoadedPageSize.value = loadedSize;
56
91
  if (isRefresh) {
57
92
  data.value = pageData;
93
+ lastRefreshTime = Date.now();
58
94
  } else {
59
95
  const oldData = data.value;
60
96
  if (Array.isArray(oldData) && Array.isArray(pageData)) {
61
97
  data.value = [...oldData, ...pageData];
98
+ } else if (oldData && typeof oldData === "object" && pageData && typeof pageData === "object") {
99
+ const merged = {
100
+ ...pageData
101
+ };
102
+ for (const key of Object.keys(pageData)) {
103
+ const oldValue = oldData[key];
104
+ const newValue = pageData[key];
105
+ if (Array.isArray(oldValue) && Array.isArray(newValue)) {
106
+ merged[key] = [...oldValue, ...newValue];
107
+ }
108
+ }
109
+ data.value = merged;
62
110
  } else {
63
111
  data.value = pageData;
64
112
  }
@@ -77,6 +125,7 @@ function useLoadMore(service, options = {}) {
77
125
  };
78
126
  const loadMore = async () => {
79
127
  if (noMore.value || loadingMore.value) return;
128
+ if (Date.now() - lastRefreshTime < 100) return;
80
129
  await loadData(current.value + 1, false);
81
130
  };
82
131
  const reload = async () => {
@@ -90,7 +139,9 @@ function useLoadMore(service, options = {}) {
90
139
  if (page < 1 || totalPages.value > 0 && page > totalPages.value) return;
91
140
  await loadData(page, true);
92
141
  },
93
- nextPage: loadMore,
142
+ nextPage: async () => {
143
+ await pagination.loadPage(current.value + 1);
144
+ },
94
145
  prevPage: async () => {
95
146
  await pagination.loadPage(current.value - 1);
96
147
  },
@@ -109,9 +160,13 @@ function useLoadMore(service, options = {}) {
109
160
  }
110
161
  };
111
162
  if (!manual) {
112
- (0, _vue.onMounted)(() => {
113
- loadData(current.value, false);
114
- });
163
+ if ((0, _vue.getCurrentInstance)()) {
164
+ (0, _vue.onMounted)(() => {
165
+ loadData(current.value, true);
166
+ });
167
+ } else {
168
+ loadData(current.value, true);
169
+ }
115
170
  }
116
171
  return {
117
172
  current,
@@ -1,4 +1,10 @@
1
- import { ref, shallowRef, computed, onMounted } from "vue";
1
+ import {
2
+ ref,
3
+ shallowRef,
4
+ computed,
5
+ onMounted,
6
+ getCurrentInstance
7
+ } from "vue";
2
8
  export function useLoadMore(service, options = {}) {
3
9
  const {
4
10
  initialPage = 1,
@@ -25,8 +31,16 @@ export function useLoadMore(service, options = {}) {
25
31
  const loadingMore = ref(false);
26
32
  const error = shallowRef(void 0);
27
33
  const params = ref(defaultParams);
34
+ let lastRefreshTime = 0;
35
+ const lastLoadedPageSize = ref(null);
28
36
  const noMore = computed(() => {
29
- return current.value >= totalPages.value && total.value > 0;
37
+ if (total.value > 0) {
38
+ return current.value >= totalPages.value;
39
+ }
40
+ if (lastLoadedPageSize.value !== null && lastLoadedPageSize.value < pageSize.value) {
41
+ return true;
42
+ }
43
+ return false;
30
44
  });
31
45
  const canLoadMore = computed(() => {
32
46
  return isLoadMore && !noMore.value && !loadingMore.value;
@@ -36,27 +50,69 @@ export function useLoadMore(service, options = {}) {
36
50
  if (loading.value || loadingMore.value) return;
37
51
  const isLoadMoreOp = !isRefresh;
38
52
  loading.value = isLoadMoreOp ? false : true;
39
- if (isRefresh) refreshing.value = true;
53
+ if (isRefresh) {
54
+ refreshing.value = true;
55
+ total.value = 0;
56
+ lastLoadedPageSize.value = null;
57
+ }
40
58
  if (isLoadMoreOp) loadingMore.value = true;
41
59
  error.value = void 0;
42
60
  try {
43
- const extraParams = params.value.slice(2);
61
+ const extraParams = params.value;
44
62
  const response = await loadService(requestedPage, pageSize.value, ...extraParams);
45
63
  const pageData = response.data;
64
+ let totalNum;
46
65
  if (pageData && typeof pageData === "object") {
47
66
  const paginatedData = pageData;
48
- if (typeof paginatedData.total !== "undefined") total.value = paginatedData.total;
67
+ if (typeof paginatedData.total !== "undefined") totalNum = paginatedData.total;
49
68
  else if (typeof paginatedData.totalCount !== "undefined")
50
- total.value = paginatedData.totalCount;
69
+ totalNum = paginatedData.totalCount;
51
70
  else if (typeof paginatedData.totalElements !== "undefined")
52
- total.value = paginatedData.totalElements;
71
+ totalNum = paginatedData.totalElements;
72
+ }
73
+ if (typeof totalNum === "undefined" && response && typeof response === "object") {
74
+ const resp = response;
75
+ if (typeof resp.total !== "undefined") totalNum = resp.total;
76
+ else if (typeof resp.totalCount !== "undefined") totalNum = resp.totalCount;
77
+ else if (typeof resp.totalElements !== "undefined") totalNum = resp.totalElements;
78
+ }
79
+ if (typeof totalNum !== "undefined") {
80
+ total.value = totalNum;
53
81
  }
82
+ let loadedSize = 0;
83
+ if (Array.isArray(pageData)) {
84
+ loadedSize = pageData.length;
85
+ } else if (pageData && typeof pageData === "object") {
86
+ const arrayKeys = Object.keys(pageData).filter(
87
+ (k) => Array.isArray(pageData[k])
88
+ );
89
+ if (arrayKeys.length > 0) {
90
+ const preferredKey = arrayKeys.find((k) => ["list", "data", "items"].includes(k));
91
+ if (preferredKey) {
92
+ loadedSize = pageData[preferredKey].length;
93
+ } else {
94
+ loadedSize = pageData[arrayKeys[0]].length;
95
+ }
96
+ }
97
+ }
98
+ lastLoadedPageSize.value = loadedSize;
54
99
  if (isRefresh) {
55
100
  data.value = pageData;
101
+ lastRefreshTime = Date.now();
56
102
  } else {
57
103
  const oldData = data.value;
58
104
  if (Array.isArray(oldData) && Array.isArray(pageData)) {
59
105
  data.value = [...oldData, ...pageData];
106
+ } else if (oldData && typeof oldData === "object" && pageData && typeof pageData === "object") {
107
+ const merged = { ...pageData };
108
+ for (const key of Object.keys(pageData)) {
109
+ const oldValue = oldData[key];
110
+ const newValue = pageData[key];
111
+ if (Array.isArray(oldValue) && Array.isArray(newValue)) {
112
+ merged[key] = [...oldValue, ...newValue];
113
+ }
114
+ }
115
+ data.value = merged;
60
116
  } else {
61
117
  data.value = pageData;
62
118
  }
@@ -75,6 +131,7 @@ export function useLoadMore(service, options = {}) {
75
131
  };
76
132
  const loadMore = async () => {
77
133
  if (noMore.value || loadingMore.value) return;
134
+ if (Date.now() - lastRefreshTime < 100) return;
78
135
  await loadData(current.value + 1, false);
79
136
  };
80
137
  const reload = async () => {
@@ -88,7 +145,9 @@ export function useLoadMore(service, options = {}) {
88
145
  if (page < 1 || totalPages.value > 0 && page > totalPages.value) return;
89
146
  await loadData(page, true);
90
147
  },
91
- nextPage: loadMore,
148
+ nextPage: async () => {
149
+ await pagination.loadPage(current.value + 1);
150
+ },
92
151
  prevPage: async () => {
93
152
  await pagination.loadPage(current.value - 1);
94
153
  },
@@ -107,9 +166,13 @@ export function useLoadMore(service, options = {}) {
107
166
  }
108
167
  };
109
168
  if (!manual) {
110
- onMounted(() => {
111
- loadData(current.value, false);
112
- });
169
+ if (getCurrentInstance()) {
170
+ onMounted(() => {
171
+ loadData(current.value, true);
172
+ });
173
+ } else {
174
+ loadData(current.value, true);
175
+ }
113
176
  }
114
177
  return {
115
178
  current,
@@ -139,9 +139,13 @@ function usePagination(service, options = {}) {
139
139
  }
140
140
  });
141
141
  if (!manual) {
142
- (0, _vue.onMounted)(() => {
142
+ if ((0, _vue.getCurrentInstance)()) {
143
+ (0, _vue.onMounted)(() => {
144
+ loadData();
145
+ });
146
+ } else {
143
147
  loadData();
144
- });
148
+ }
145
149
  }
146
150
  return {
147
151
  current,
@@ -1,4 +1,11 @@
1
- import { ref, shallowRef, computed, watch, onMounted } from "vue";
1
+ import {
2
+ ref,
3
+ shallowRef,
4
+ computed,
5
+ watch,
6
+ onMounted,
7
+ getCurrentInstance
8
+ } from "vue";
2
9
  export function usePagination(service, options = {}) {
3
10
  const {
4
11
  defaultPagination = { current: 1, pageSize: 10 },
@@ -130,9 +137,13 @@ export function usePagination(service, options = {}) {
130
137
  }
131
138
  });
132
139
  if (!manual) {
133
- onMounted(() => {
140
+ if (getCurrentInstance()) {
141
+ onMounted(() => {
142
+ loadData();
143
+ });
144
+ } else {
134
145
  loadData();
135
- });
146
+ }
136
147
  }
137
148
  return {
138
149
  current,
package/dist/useQueue.cjs CHANGED
@@ -237,9 +237,11 @@ function useQueue(options = {}) {
237
237
  const getTask = taskId => {
238
238
  return taskMap.get(taskId);
239
239
  };
240
- (0, _vue.onUnmounted)(() => {
241
- cancelAll();
242
- });
240
+ if ((0, _vue.getCurrentInstance)()) {
241
+ (0, _vue.onUnmounted)(() => {
242
+ cancelAll();
243
+ });
244
+ }
243
245
  return {
244
246
  tasks,
245
247
  pendingTasks,
package/dist/useQueue.mjs CHANGED
@@ -1,4 +1,10 @@
1
- import { ref, reactive, computed, onUnmounted } from "vue";
1
+ import {
2
+ ref,
3
+ reactive,
4
+ computed,
5
+ onUnmounted,
6
+ getCurrentInstance
7
+ } from "vue";
2
8
  let taskIdCounter = 0;
3
9
  function generateTaskId() {
4
10
  return `task_${Date.now()}_${++taskIdCounter}`;
@@ -231,9 +237,11 @@ export function useQueue(options = {}) {
231
237
  const getTask = (taskId) => {
232
238
  return taskMap.get(taskId);
233
239
  };
234
- onUnmounted(() => {
235
- cancelAll();
236
- });
240
+ if (getCurrentInstance()) {
241
+ onUnmounted(() => {
242
+ cancelAll();
243
+ });
244
+ }
237
245
  return {
238
246
  tasks,
239
247
  pendingTasks,
@@ -150,15 +150,23 @@ function useRequest(service, options = {}) {
150
150
  wrappedRun.cancel = () => throttled.cancel();
151
151
  cancelFn = () => throttled.cancel();
152
152
  }
153
- (0, _vue.onUnmounted)(() => {
154
- cancelFn?.();
155
- });
153
+ if ((0, _vue.getCurrentInstance)()) {
154
+ (0, _vue.onUnmounted)(() => {
155
+ cancel();
156
+ });
157
+ }
156
158
  if (!manual && defaultParams.length > 0) {
157
- (0, _vue.onMounted)(() => {
159
+ if ((0, _vue.getCurrentInstance)()) {
160
+ (0, _vue.onMounted)(() => {
161
+ if (!debounceWait && !throttleWait) {
162
+ run(...defaultParams).catch(() => {});
163
+ }
164
+ });
165
+ } else {
158
166
  if (!debounceWait && !throttleWait) {
159
167
  run(...defaultParams).catch(() => {});
160
168
  }
161
- });
169
+ }
162
170
  }
163
171
  const disabled = (0, _vue.computed)(() => loading.value);
164
172
  return {
@@ -247,14 +255,16 @@ function useRequestSWR(cacheKey, service, options = {}) {
247
255
  swrRefresh();
248
256
  }
249
257
  };
250
- (0, _vue.onMounted)(() => {
251
- window.addEventListener("visibilitychange", handleFocus);
252
- window.addEventListener("focus", handleFocus);
253
- });
254
- (0, _vue.onUnmounted)(() => {
255
- window.removeEventListener("visibilitychange", handleFocus);
256
- window.removeEventListener("focus", handleFocus);
257
- });
258
+ if ((0, _vue.getCurrentInstance)()) {
259
+ (0, _vue.onMounted)(() => {
260
+ window.addEventListener("visibilitychange", handleFocus);
261
+ window.addEventListener("focus", handleFocus);
262
+ });
263
+ (0, _vue.onUnmounted)(() => {
264
+ window.removeEventListener("visibilitychange", handleFocus);
265
+ window.removeEventListener("focus", handleFocus);
266
+ });
267
+ }
258
268
  }
259
269
  if (refreshDeps && refreshDeps.length > 0 && !manual) {
260
270
  (0, _vue.watch)(() => refreshDeps.map(dep => dep.value), () => {
@@ -336,14 +346,20 @@ function useRequestPolling(service, options = {}) {
336
346
  startPolling();
337
347
  }
338
348
  };
339
- (0, _vue.onUnmounted)(() => {
340
- pause();
341
- cancel();
342
- });
343
- if (polling) {
344
- (0, _vue.onMounted)(() => {
345
- startPolling();
349
+ if ((0, _vue.getCurrentInstance)()) {
350
+ (0, _vue.onUnmounted)(() => {
351
+ pause();
352
+ cancel();
346
353
  });
354
+ if (polling) {
355
+ (0, _vue.onMounted)(() => {
356
+ startPolling();
357
+ });
358
+ }
359
+ } else {
360
+ if (polling) {
361
+ startPolling();
362
+ }
347
363
  }
348
364
  return {
349
365
  loading,
@@ -155,16 +155,25 @@ export function useRequest(service, options = {}) {
155
155
  wrappedRun.cancel = () => throttled.cancel();
156
156
  cancelFn = () => throttled.cancel();
157
157
  }
158
- onUnmounted(() => {
159
- cancelFn?.();
160
- });
158
+ if (getCurrentInstance()) {
159
+ onUnmounted(() => {
160
+ cancel();
161
+ });
162
+ }
161
163
  if (!manual && defaultParams.length > 0) {
162
- onMounted(() => {
164
+ if (getCurrentInstance()) {
165
+ onMounted(() => {
166
+ if (!debounceWait && !throttleWait) {
167
+ run(...defaultParams).catch(() => {
168
+ });
169
+ }
170
+ });
171
+ } else {
163
172
  if (!debounceWait && !throttleWait) {
164
173
  run(...defaultParams).catch(() => {
165
174
  });
166
175
  }
167
- });
176
+ }
168
177
  }
169
178
  const disabled = computed(() => loading.value);
170
179
  return {
@@ -259,14 +268,16 @@ export function useRequestSWR(cacheKey, service, options = {}) {
259
268
  swrRefresh();
260
269
  }
261
270
  };
262
- onMounted(() => {
263
- window.addEventListener("visibilitychange", handleFocus);
264
- window.addEventListener("focus", handleFocus);
265
- });
266
- onUnmounted(() => {
267
- window.removeEventListener("visibilitychange", handleFocus);
268
- window.removeEventListener("focus", handleFocus);
269
- });
271
+ if (getCurrentInstance()) {
272
+ onMounted(() => {
273
+ window.addEventListener("visibilitychange", handleFocus);
274
+ window.addEventListener("focus", handleFocus);
275
+ });
276
+ onUnmounted(() => {
277
+ window.removeEventListener("visibilitychange", handleFocus);
278
+ window.removeEventListener("focus", handleFocus);
279
+ });
280
+ }
270
281
  }
271
282
  if (refreshDeps && refreshDeps.length > 0 && !manual) {
272
283
  watch(
@@ -353,14 +364,20 @@ export function useRequestPolling(service, options = {}) {
353
364
  startPolling();
354
365
  }
355
366
  };
356
- onUnmounted(() => {
357
- pause();
358
- cancel();
359
- });
360
- if (polling) {
361
- onMounted(() => {
362
- startPolling();
367
+ if (getCurrentInstance()) {
368
+ onUnmounted(() => {
369
+ pause();
370
+ cancel();
363
371
  });
372
+ if (polling) {
373
+ onMounted(() => {
374
+ startPolling();
375
+ });
376
+ }
377
+ } else {
378
+ if (polling) {
379
+ startPolling();
380
+ }
364
381
  }
365
382
  return {
366
383
  loading,
package/dist/useSSE.cjs CHANGED
@@ -226,9 +226,11 @@ function useSSE(options = {}) {
226
226
  messages.value = [];
227
227
  error.value = void 0;
228
228
  };
229
- (0, _vue.onUnmounted)(() => {
230
- stop();
231
- });
229
+ if ((0, _vue.getCurrentInstance)()) {
230
+ (0, _vue.onUnmounted)(() => {
231
+ stop();
232
+ });
233
+ }
232
234
  return {
233
235
  loading,
234
236
  content,
package/dist/useSSE.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { ref, shallowRef, onUnmounted } from "vue";
1
+ import { ref, shallowRef, onUnmounted, getCurrentInstance } from "vue";
2
2
  export function useSSE(options = {}) {
3
3
  const {
4
4
  parseJSON = true,
@@ -211,9 +211,11 @@ export function useSSE(options = {}) {
211
211
  messages.value = [];
212
212
  error.value = void 0;
213
213
  };
214
- onUnmounted(() => {
215
- stop();
216
- });
214
+ if (getCurrentInstance()) {
215
+ onUnmounted(() => {
216
+ stop();
217
+ });
218
+ }
217
219
  return {
218
220
  loading,
219
221
  content,
@@ -314,9 +314,11 @@ function useWebSocket(options) {
314
314
  const state = client.state;
315
315
  const isConnected = client.isConnected;
316
316
  const lastMessage = client.lastMessage;
317
- (0, _vue.onUnmounted)(() => {
318
- client.dispose();
319
- });
317
+ if ((0, _vue.getCurrentInstance)()) {
318
+ (0, _vue.onUnmounted)(() => {
319
+ client.dispose();
320
+ });
321
+ }
320
322
  return {
321
323
  /** 当前状态 */
322
324
  state,
@@ -1,4 +1,4 @@
1
- import { ref, onUnmounted } from "vue";
1
+ import { ref, onUnmounted, getCurrentInstance } from "vue";
2
2
  export class WebSocketClient {
3
3
  ws = null;
4
4
  url = "";
@@ -298,9 +298,11 @@ export function useWebSocket(options) {
298
298
  const state = client.state;
299
299
  const isConnected = client.isConnected;
300
300
  const lastMessage = client.lastMessage;
301
- onUnmounted(() => {
302
- client.dispose();
303
- });
301
+ if (getCurrentInstance()) {
302
+ onUnmounted(() => {
303
+ client.dispose();
304
+ });
305
+ }
304
306
  return {
305
307
  /** 当前状态 */
306
308
  state,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yh-ui/request",
3
- "version": "1.0.54",
3
+ "version": "1.0.56",
4
4
  "description": "YH-UI HTTP request hooks - Enterprise-grade request management",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -32,7 +32,7 @@
32
32
  "postpack": "node ../../scripts/prepare-package-manifest.mjs restore"
33
33
  },
34
34
  "dependencies": {
35
- "@yh-ui/utils": "^1.0.54"
35
+ "@yh-ui/utils": "^1.0.56"
36
36
  },
37
37
  "devDependencies": {
38
38
  "vue": "^3.5.35",