@varlet/axle 0.11.1 → 0.12.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.
package/lib/use.js CHANGED
@@ -3,27 +3,29 @@ import {
3
3
  } from "./chunk-G2ADBYYC.js";
4
4
 
5
5
  // src/use.ts
6
- import { getCurrentInstance, onUnmounted, ref as ref3 } from "vue";
6
+ import { getCurrentInstance, onActivated, onDeactivated, onMounted, onUnmounted, ref as ref3, watch as watch3 } from "vue";
7
7
  import { isFunction } from "rattail";
8
8
 
9
- // src/composables/useValues.ts
10
- import { computed } from "vue";
11
- function useValues(...valueCollection) {
12
- return computed({
13
- get: () => valueCollection.map((value) => value.value),
14
- set(values) {
15
- valueCollection.forEach((value, index) => {
16
- value.value = values[index];
17
- });
18
- }
19
- });
9
+ // src/composables/useAverageProgress.ts
10
+ import { ref, watch } from "vue";
11
+ function useAverageProgress(...progressCollection) {
12
+ const averageProgress = ref(0);
13
+ watch(
14
+ () => progressCollection.map((progress) => progress.value),
15
+ (newProgressCollection) => {
16
+ const total = newProgressCollection.reduce((total2, progress) => total2 + progress, 0);
17
+ averageProgress.value = total / progressCollection.length;
18
+ },
19
+ { immediate: true }
20
+ );
21
+ return averageProgress;
20
22
  }
21
23
 
22
24
  // src/composables/useHasLoading.ts
23
- import { ref, watch } from "vue";
25
+ import { ref as ref2, watch as watch2 } from "vue";
24
26
  function useHasLoading(...loadings) {
25
- const hasLoading = ref(false);
26
- watch(
27
+ const hasLoading = ref2(false);
28
+ watch2(
27
29
  () => loadings.map((loading) => loading.value),
28
30
  (newLoadings) => {
29
31
  hasLoading.value = newLoadings.some(Boolean);
@@ -33,31 +35,38 @@ function useHasLoading(...loadings) {
33
35
  return hasLoading;
34
36
  }
35
37
 
36
- // src/composables/useAverageProgress.ts
37
- import { ref as ref2, watch as watch2 } from "vue";
38
- function useAverageProgress(...progressCollection) {
39
- const averageProgress = ref2(0);
40
- watch2(
41
- () => progressCollection.map((progress) => progress.value),
42
- (newProgressCollection) => {
43
- const total = newProgressCollection.reduce((total2, progress) => total2 + progress, 0);
44
- averageProgress.value = total / progressCollection.length;
45
- },
46
- { immediate: true }
47
- );
48
- return averageProgress;
38
+ // src/composables/useValues.ts
39
+ import { computed } from "vue";
40
+ function useValues(...valueCollection) {
41
+ return computed({
42
+ get: () => valueCollection.map((value) => value.value),
43
+ set(values) {
44
+ valueCollection.forEach((value, index) => {
45
+ value.value = values[index];
46
+ });
47
+ }
48
+ });
49
49
  }
50
50
 
51
51
  // src/use.ts
52
52
  function normalizeValueGetter(valueGetter) {
53
53
  return isFunction(valueGetter) ? valueGetter() : valueGetter;
54
54
  }
55
+ var cacheBuffer = /* @__PURE__ */ new Map();
56
+ function cleanupCacheBuffer() {
57
+ cacheBuffer.forEach((value, key) => {
58
+ if (value.expiredTime && Date.now() >= value.expiredTime) {
59
+ cacheBuffer.delete(key);
60
+ }
61
+ });
62
+ }
55
63
  function createUseAxle(options) {
56
64
  const {
57
65
  axle,
58
66
  onTransform: defaultOnTransform,
59
67
  immediate: defaultImmediate = true,
60
- abortOnUnmount: defaultAbortOnUnmount = true
68
+ abortOnUnmount: defaultAbortOnUnmount = true,
69
+ cacheTime: defaultCacheTime = Infinity
61
70
  } = options;
62
71
  const useAxle = (options2) => {
63
72
  const {
@@ -65,11 +74,18 @@ function createUseAxle(options) {
65
74
  method,
66
75
  immediate = defaultImmediate,
67
76
  abortOnUnmount = defaultAbortOnUnmount,
77
+ cacheTime = defaultCacheTime,
78
+ cacheKey,
79
+ pollingInterval,
80
+ pollingOnHidden = true,
81
+ pollingOnDeactivated = false,
68
82
  value: initialValue,
69
83
  resetValue: initialResetValue,
70
84
  cloneResetValue: initialCloneResetValue,
71
85
  params: initialParamsOrGetter,
72
86
  config: initialConfigOrGetter,
87
+ watch: watchOptions,
88
+ runnable = () => true,
73
89
  onBefore = () => {
74
90
  },
75
91
  onAfter = () => {
@@ -85,86 +101,179 @@ function createUseAxle(options) {
85
101
  const error = ref3();
86
102
  const downloadProgress = ref3(0);
87
103
  const uploadProgress = ref3(0);
104
+ const pollingCanceled = ref3(false);
88
105
  const refs = {
89
106
  value,
90
107
  loading,
91
108
  error,
92
109
  downloadProgress,
93
- uploadProgress
110
+ uploadProgress,
111
+ pollingCanceled
94
112
  };
95
113
  const extra = {
96
114
  uploadProgress,
97
115
  downloadProgress,
98
116
  loading,
99
117
  error,
118
+ pollingCanceled,
100
119
  abort,
101
- resetValue
120
+ resetValue,
121
+ invalidateCache,
122
+ cancelPolling
102
123
  };
103
124
  let controller = new AbortController();
104
- const run = Object.assign(
105
- async (options3 = {}) => {
106
- var _a, _b, _c, _d, _e;
107
- if (controller.signal.aborted) {
108
- controller = new AbortController();
125
+ let pollingTimer = null;
126
+ let deactivated = false;
127
+ let hidden = false;
128
+ const run = Object.assign(async (options3 = {}) => {
129
+ var _a, _b, _c, _d, _e;
130
+ if (!runnable()) {
131
+ return;
132
+ }
133
+ pollingCanceled.value = false;
134
+ if (controller.signal.aborted) {
135
+ controller = new AbortController();
136
+ }
137
+ const resetValueOption = (_b = (_a = options3.resetValue) != null ? _a : initialResetValue) != null ? _b : false;
138
+ if (resetValueOption === true) {
139
+ resetValue(options3);
140
+ }
141
+ uploadProgress.value = 0;
142
+ downloadProgress.value = 0;
143
+ const url = (_c = options3.url) != null ? _c : normalizeValueGetter(initialUrlOrGetter);
144
+ const params = (_d = options3.params) != null ? _d : normalizeValueGetter(initialParamsOrGetter);
145
+ const config = (_e = options3.config) != null ? _e : normalizeValueGetter(initialConfigOrGetter);
146
+ const normalizedCacheKey = cacheKey ? normalizeValueGetter(cacheKey) : void 0;
147
+ onBefore(refs);
148
+ loading.value = true;
149
+ try {
150
+ cleanupCacheBuffer();
151
+ const response = await getResponse();
152
+ if (shouldSetCacheResponse(normalizedCacheKey)) {
153
+ const cache = cacheBuffer.get(normalizedCacheKey);
154
+ cache.response = response;
155
+ cache.expiredTime = Date.now() + cacheTime;
109
156
  }
110
- const resetValueOption = (_b = (_a = options3.resetValue) != null ? _a : initialResetValue) != null ? _b : false;
111
- if (resetValueOption === true) {
112
- resetValue(options3);
157
+ value.value = await onTransform(response, refs);
158
+ error.value = void 0;
159
+ onSuccess(response, refs);
160
+ loading.value = false;
161
+ onAfter(refs);
162
+ return response;
163
+ } catch (responseError) {
164
+ error.value = responseError;
165
+ onError(responseError, refs);
166
+ loading.value = false;
167
+ onAfter(refs);
168
+ throw responseError;
169
+ } finally {
170
+ clearPolling();
171
+ startPolling();
172
+ }
173
+ function getResponse() {
174
+ if (shouldUseCache(normalizedCacheKey)) {
175
+ return JSON.parse(JSON.stringify(cacheBuffer.get(normalizedCacheKey).response));
113
176
  }
114
- uploadProgress.value = 0;
115
- downloadProgress.value = 0;
116
- const url = (_c = options3.url) != null ? _c : normalizeValueGetter(initialUrlOrGetter);
117
- const params = (_d = options3.params) != null ? _d : normalizeValueGetter(initialParamsOrGetter);
118
- const config = (_e = options3.config) != null ? _e : normalizeValueGetter(initialConfigOrGetter);
119
- onBefore(refs);
120
- loading.value = true;
121
- try {
122
- const response = await axle[method](url, params, __spreadValues({
123
- signal: controller.signal,
124
- onUploadProgress(event) {
125
- var _a2;
126
- uploadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
127
- },
128
- onDownloadProgress(event) {
129
- var _a2;
130
- downloadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
131
- }
132
- }, config));
133
- value.value = await onTransform(response, refs);
134
- error.value = void 0;
135
- onSuccess(response, refs);
136
- loading.value = false;
137
- onAfter(refs);
138
- return response;
139
- } catch (responseError) {
140
- error.value = responseError;
141
- onError(responseError, refs);
142
- loading.value = false;
143
- onAfter(refs);
144
- throw responseError;
177
+ if (shouldAwaitCachePromise(normalizedCacheKey)) {
178
+ return cacheBuffer.get(normalizedCacheKey).promise;
145
179
  }
146
- },
147
- {
148
- uploadProgress,
149
- downloadProgress,
150
- loading,
151
- error,
152
- abort,
153
- resetValue
154
- }
155
- );
180
+ const promise = fetchResponse();
181
+ if (shouldSetCachePromise(normalizedCacheKey)) {
182
+ cacheBuffer.set(normalizedCacheKey, { promise });
183
+ }
184
+ return promise;
185
+ }
186
+ function fetchResponse() {
187
+ return axle[method](url, params, __spreadValues({
188
+ signal: controller.signal,
189
+ onUploadProgress(event) {
190
+ var _a2;
191
+ uploadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
192
+ },
193
+ onDownloadProgress(event) {
194
+ var _a2;
195
+ downloadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
196
+ }
197
+ }, config));
198
+ }
199
+ function shouldUseCache(key) {
200
+ if (!key || !cacheBuffer.has(key)) {
201
+ return false;
202
+ }
203
+ const buffer = cacheBuffer.get(key);
204
+ return buffer.response != null;
205
+ }
206
+ function shouldAwaitCachePromise(key) {
207
+ if (!key || !cacheBuffer.has(key)) {
208
+ return false;
209
+ }
210
+ const buffer = cacheBuffer.get(key);
211
+ return buffer.promise;
212
+ }
213
+ function shouldSetCachePromise(key) {
214
+ if (!key || cacheBuffer.has(key)) {
215
+ return false;
216
+ }
217
+ return true;
218
+ }
219
+ function shouldSetCacheResponse(key) {
220
+ if (!key || !cacheBuffer.has(key)) {
221
+ return false;
222
+ }
223
+ const buffer = cacheBuffer.get(key);
224
+ return buffer.response == null;
225
+ }
226
+ }, extra);
156
227
  if (immediate) {
157
- run({
228
+ runWithInitialConfig();
229
+ }
230
+ if (getCurrentInstance()) {
231
+ onActivated(() => {
232
+ deactivated = false;
233
+ addVisibilityChangeListener();
234
+ startPolling();
235
+ });
236
+ onMounted(() => {
237
+ addVisibilityChangeListener();
238
+ });
239
+ onDeactivated(() => {
240
+ deactivated = true;
241
+ removeVisibilityChangeListener();
242
+ clearPolling();
243
+ });
244
+ onUnmounted(() => {
245
+ removeVisibilityChangeListener();
246
+ cancelPolling();
247
+ });
248
+ if (abortOnUnmount) {
249
+ onUnmounted(() => {
250
+ abort();
251
+ });
252
+ }
253
+ if (watchOptions) {
254
+ const normalizedWatchOptions = watchOptions === true ? { params: true, config: true } : __spreadValues({ params: false, config: false }, watchOptions);
255
+ const enableWatchParams = isFunction(initialParamsOrGetter) && normalizedWatchOptions.params;
256
+ const enableWatchConfig = isFunction(initialConfigOrGetter) && normalizedWatchOptions.config;
257
+ watch3(
258
+ () => [
259
+ enableWatchParams ? normalizeValueGetter(initialParamsOrGetter) : void 0,
260
+ enableWatchConfig ? normalizeValueGetter(initialConfigOrGetter) : void 0,
261
+ normalizedWatchOptions._custom ? normalizedWatchOptions._custom() : void 0
262
+ ],
263
+ () => {
264
+ runWithInitialConfig();
265
+ },
266
+ { deep: true }
267
+ );
268
+ }
269
+ }
270
+ function runWithInitialConfig() {
271
+ return run({
158
272
  url: normalizeValueGetter(initialUrlOrGetter),
159
273
  params: normalizeValueGetter(initialParamsOrGetter),
160
274
  config: normalizeValueGetter(initialConfigOrGetter)
161
275
  });
162
276
  }
163
- if (abortOnUnmount && getCurrentInstance()) {
164
- onUnmounted(() => {
165
- abort();
166
- });
167
- }
168
277
  function resetValue(options3 = {}) {
169
278
  var _a, _b;
170
279
  const cloneResetValue = (_b = (_a = options3.cloneResetValue) != null ? _a : initialCloneResetValue) != null ? _b : false;
@@ -174,11 +283,55 @@ function createUseAxle(options) {
174
283
  function abort() {
175
284
  controller.abort();
176
285
  }
286
+ function addVisibilityChangeListener() {
287
+ if (typeof document !== "undefined") {
288
+ document.addEventListener("visibilitychange", handleVisibilityChange);
289
+ }
290
+ }
291
+ function removeVisibilityChangeListener() {
292
+ if (typeof document !== "undefined") {
293
+ document.removeEventListener("visibilitychange", handleVisibilityChange);
294
+ }
295
+ }
296
+ function handleVisibilityChange() {
297
+ hidden = document.visibilityState === "hidden";
298
+ if (pollingOnHidden) {
299
+ return;
300
+ }
301
+ hidden ? clearPolling() : startPolling();
302
+ }
303
+ function startPolling() {
304
+ if (pollingInterval == null || pollingCanceled.value || pollingTimer != null || deactivated && !pollingOnDeactivated || hidden && !pollingOnHidden) {
305
+ return;
306
+ }
307
+ pollingTimer = setTimeout(() => {
308
+ runWithInitialConfig();
309
+ }, pollingInterval);
310
+ }
311
+ function clearPolling() {
312
+ if (pollingTimer == null) {
313
+ return;
314
+ }
315
+ clearTimeout(pollingTimer);
316
+ pollingTimer = null;
317
+ }
318
+ function cancelPolling() {
319
+ clearPolling();
320
+ pollingCanceled.value = true;
321
+ }
322
+ function invalidateCache() {
323
+ const normalizedCacheKey = cacheKey ? normalizeValueGetter(cacheKey) : void 0;
324
+ if (!normalizedCacheKey || !cacheBuffer.has(normalizedCacheKey)) {
325
+ return;
326
+ }
327
+ cacheBuffer.delete(normalizedCacheKey);
328
+ }
177
329
  return [value, run, extra];
178
330
  };
179
331
  return useAxle;
180
332
  }
181
333
  export {
334
+ cleanupCacheBuffer,
182
335
  createUseAxle,
183
336
  normalizeValueGetter,
184
337
  useAverageProgress,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.11.1",
3
+ "version": "0.12.0",
4
4
  "description": "Progressive request tool based on axios",
5
5
  "keywords": [
6
6
  "axle",
@@ -55,14 +55,12 @@
55
55
  "axios": "^1.7.3",
56
56
  "crypto-js": "^4.2.0",
57
57
  "lodash": "4.17.21",
58
- "minimatch": "^9.0.3",
59
58
  "qs": "^6.11.0",
60
59
  "rattail": "1.0.0"
61
60
  },
62
61
  "devDependencies": {
63
62
  "@types/crypto-js": "^4.2.1",
64
63
  "@types/lodash": "4.14.202",
65
- "@types/minimatch": "^5.1.2",
66
64
  "@types/qs": "^6.9.7",
67
65
  "tsup": "7.2.0",
68
66
  "typescript": "^5.1.5",