@yh-ui/request 1.0.53 → 1.0.55

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.
@@ -38,9 +38,8 @@ function useLoadMore(service, options = {}) {
38
38
  return isLoadMore && !noMore.value && !loadingMore.value;
39
39
  });
40
40
  const loadService = loadMoreService || service;
41
- const loadData = async (isRefresh = false) => {
41
+ const loadData = async (requestedPage, isRefresh = false) => {
42
42
  if (loading.value || loadingMore.value) return;
43
- const requestedPage = current.value;
44
43
  const isLoadMoreOp = !isRefresh;
45
44
  loading.value = isLoadMoreOp ? false : true;
46
45
  if (isRefresh) refreshing.value = true;
@@ -48,7 +47,7 @@ function useLoadMore(service, options = {}) {
48
47
  error.value = void 0;
49
48
  try {
50
49
  const extraParams = params.value.slice(2);
51
- const response = await loadService(current.value, pageSize.value, ...extraParams);
50
+ const response = await loadService(requestedPage, pageSize.value, ...extraParams);
52
51
  const pageData = response.data;
53
52
  if (pageData && typeof pageData === "object") {
54
53
  const paginatedData = pageData;
@@ -56,7 +55,6 @@ function useLoadMore(service, options = {}) {
56
55
  }
57
56
  if (isRefresh) {
58
57
  data.value = pageData;
59
- current.value = requestedPage + 1;
60
58
  } else {
61
59
  const oldData = data.value;
62
60
  if (Array.isArray(oldData) && Array.isArray(pageData)) {
@@ -64,8 +62,8 @@ function useLoadMore(service, options = {}) {
64
62
  } else {
65
63
  data.value = pageData;
66
64
  }
67
- current.value = requestedPage + 1;
68
65
  }
66
+ current.value = requestedPage;
69
67
  onSuccess?.(pageData, params.value);
70
68
  } catch (err) {
71
69
  error.value = err;
@@ -79,21 +77,22 @@ function useLoadMore(service, options = {}) {
79
77
  };
80
78
  const loadMore = async () => {
81
79
  if (noMore.value || loadingMore.value) return;
82
- await loadData(false);
80
+ await loadData(current.value + 1, false);
83
81
  };
84
82
  const reload = async () => {
85
- current.value = initialPage;
86
- await loadData(true);
83
+ await loadData(initialPage, true);
87
84
  };
88
85
  const refresh = async () => {
89
86
  await reload();
90
87
  };
91
88
  const pagination = {
92
89
  loadPage: async page => {
93
- current.value = page;
94
- await loadData(true);
90
+ if (page < 1 || totalPages.value > 0 && page > totalPages.value) return;
91
+ await loadData(page, true);
92
+ },
93
+ nextPage: async () => {
94
+ await pagination.loadPage(current.value + 1);
95
95
  },
96
- nextPage: loadMore,
97
96
  prevPage: async () => {
98
97
  await pagination.loadPage(current.value - 1);
99
98
  },
@@ -112,9 +111,13 @@ function useLoadMore(service, options = {}) {
112
111
  }
113
112
  };
114
113
  if (!manual) {
115
- (0, _vue.onMounted)(() => {
116
- loadData();
117
- });
114
+ if ((0, _vue.getCurrentInstance)()) {
115
+ (0, _vue.onMounted)(() => {
116
+ loadData(current.value, false);
117
+ });
118
+ } else {
119
+ loadData(current.value, false);
120
+ }
118
121
  }
119
122
  return {
120
123
  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,
@@ -32,9 +38,8 @@ export function useLoadMore(service, options = {}) {
32
38
  return isLoadMore && !noMore.value && !loadingMore.value;
33
39
  });
34
40
  const loadService = loadMoreService || service;
35
- const loadData = async (isRefresh = false) => {
41
+ const loadData = async (requestedPage, isRefresh = false) => {
36
42
  if (loading.value || loadingMore.value) return;
37
- const requestedPage = current.value;
38
43
  const isLoadMoreOp = !isRefresh;
39
44
  loading.value = isLoadMoreOp ? false : true;
40
45
  if (isRefresh) refreshing.value = true;
@@ -42,7 +47,7 @@ export function useLoadMore(service, options = {}) {
42
47
  error.value = void 0;
43
48
  try {
44
49
  const extraParams = params.value.slice(2);
45
- const response = await loadService(current.value, pageSize.value, ...extraParams);
50
+ const response = await loadService(requestedPage, pageSize.value, ...extraParams);
46
51
  const pageData = response.data;
47
52
  if (pageData && typeof pageData === "object") {
48
53
  const paginatedData = pageData;
@@ -54,7 +59,6 @@ export function useLoadMore(service, options = {}) {
54
59
  }
55
60
  if (isRefresh) {
56
61
  data.value = pageData;
57
- current.value = requestedPage + 1;
58
62
  } else {
59
63
  const oldData = data.value;
60
64
  if (Array.isArray(oldData) && Array.isArray(pageData)) {
@@ -62,8 +66,8 @@ export function useLoadMore(service, options = {}) {
62
66
  } else {
63
67
  data.value = pageData;
64
68
  }
65
- current.value = requestedPage + 1;
66
69
  }
70
+ current.value = requestedPage;
67
71
  onSuccess?.(pageData, params.value);
68
72
  } catch (err) {
69
73
  error.value = err;
@@ -77,21 +81,22 @@ export function useLoadMore(service, options = {}) {
77
81
  };
78
82
  const loadMore = async () => {
79
83
  if (noMore.value || loadingMore.value) return;
80
- await loadData(false);
84
+ await loadData(current.value + 1, false);
81
85
  };
82
86
  const reload = async () => {
83
- current.value = initialPage;
84
- await loadData(true);
87
+ await loadData(initialPage, true);
85
88
  };
86
89
  const refresh = async () => {
87
90
  await reload();
88
91
  };
89
92
  const pagination = {
90
93
  loadPage: async (page) => {
91
- current.value = page;
92
- await loadData(true);
94
+ if (page < 1 || totalPages.value > 0 && page > totalPages.value) return;
95
+ await loadData(page, true);
96
+ },
97
+ nextPage: async () => {
98
+ await pagination.loadPage(current.value + 1);
93
99
  },
94
- nextPage: loadMore,
95
100
  prevPage: async () => {
96
101
  await pagination.loadPage(current.value - 1);
97
102
  },
@@ -110,9 +115,13 @@ export function useLoadMore(service, options = {}) {
110
115
  }
111
116
  };
112
117
  if (!manual) {
113
- onMounted(() => {
114
- loadData();
115
- });
118
+ if (getCurrentInstance()) {
119
+ onMounted(() => {
120
+ loadData(current.value, false);
121
+ });
122
+ } else {
123
+ loadData(current.value, false);
124
+ }
116
125
  }
117
126
  return {
118
127
  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.53",
3
+ "version": "1.0.55",
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.53"
35
+ "@yh-ui/utils": "^1.0.55"
36
36
  },
37
37
  "devDependencies": {
38
38
  "vue": "^3.5.35",