@varlet/axle 1.0.2 → 1.1.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/dist/use.d.mts CHANGED
@@ -9,6 +9,7 @@ interface RunOptions<V, P, D> {
9
9
  config?: AxleRequestConfig<D>;
10
10
  resetValue?: boolean;
11
11
  cloneResetValue?: boolean | ((value: V) => V);
12
+ silent?: boolean;
12
13
  }
13
14
  type UseAxleExtra<V> = {
14
15
  loading: Ref<boolean>;
@@ -45,11 +46,13 @@ interface UseAxleOptions<V = any, R = any, P = Record<string, any>, D = Record<s
45
46
  resetValue?: boolean;
46
47
  cloneResetValue?: boolean | ((value: V) => V);
47
48
  abortOnUnmount?: boolean;
49
+ abortPrevious?: boolean;
48
50
  cacheKey?: string | (() => string);
49
51
  cacheTime?: number;
50
52
  config?: AxleRequestConfig<D> | (() => AxleRequestConfig<D>);
51
53
  watch?: WatchOptions;
52
- pollingInterval?: number;
54
+ pollingInterval?: number | (() => number | null | undefined);
55
+ pollingSilent?: boolean;
53
56
  pollingOnHidden?: boolean;
54
57
  pollingOnDeactivated?: boolean;
55
58
  refreshOnWindowFocus?: boolean;
@@ -68,6 +71,8 @@ interface CreateUseAxleOptions {
68
71
  axle: AxleInstance;
69
72
  immediate?: boolean;
70
73
  abortOnUnmount?: boolean;
74
+ abortPrevious?: boolean;
75
+ pollingSilent?: boolean;
71
76
  cacheTime?: number;
72
77
  onTransform?(response: any, refs: any): any;
73
78
  }
package/dist/use.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { isCancel } from "axios";
1
2
  import { isFunction } from "rattail";
2
3
  import { getCurrentInstance, onActivated, onDeactivated, onMounted, onUnmounted, ref, watch } from "vue";
3
4
  //#region src/use.ts
@@ -11,9 +12,9 @@ function cleanupCacheBuffer() {
11
12
  });
12
13
  }
13
14
  function createUseAxle(options) {
14
- const { axle, onTransform: defaultOnTransform, immediate: defaultImmediate = true, abortOnUnmount: defaultAbortOnUnmount = true, cacheTime: defaultCacheTime = Infinity } = options;
15
+ const { axle, onTransform: defaultOnTransform, immediate: defaultImmediate = true, abortOnUnmount: defaultAbortOnUnmount = true, abortPrevious: defaultAbortPrevious = false, pollingSilent: defaultPollingSilent = false, cacheTime: defaultCacheTime = Infinity } = options;
15
16
  const useAxle = (options) => {
16
- const { url: initialUrlOrGetter, method, immediate = defaultImmediate, abortOnUnmount = defaultAbortOnUnmount, cacheTime = defaultCacheTime, cacheKey, pollingInterval, pollingOnHidden = true, pollingOnDeactivated = false, refreshOnWindowFocus = false, value: initialValue, resetValue: initialResetValue, cloneResetValue: initialCloneResetValue, params: initialParamsOrGetter, config: initialConfigOrGetter, watch: watchOptions, runnable = () => true, onBefore = () => {}, onAfter = () => {}, onTransform = defaultOnTransform ?? ((response) => response), onSuccess = () => {}, onError = () => {} } = options;
17
+ const { url: initialUrlOrGetter, method, immediate = defaultImmediate, abortOnUnmount = defaultAbortOnUnmount, abortPrevious = defaultAbortPrevious, cacheTime = defaultCacheTime, cacheKey, pollingInterval, pollingSilent = defaultPollingSilent, pollingOnHidden = true, pollingOnDeactivated = false, refreshOnWindowFocus = false, value: initialValue, resetValue: initialResetValue, cloneResetValue: initialCloneResetValue, params: initialParamsOrGetter, config: initialConfigOrGetter, watch: watchOptions, runnable = () => true, onBefore = () => {}, onAfter = () => {}, onTransform = defaultOnTransform ?? ((response) => response), onSuccess = () => {}, onError = () => {} } = options;
17
18
  const value = ref(initialValue);
18
19
  const loading = ref(false);
19
20
  const error = ref();
@@ -40,14 +41,22 @@ function createUseAxle(options) {
40
41
  cancelPolling
41
42
  };
42
43
  let controller = new AbortController();
44
+ let latestRunId = 0;
45
+ let cachedRequest;
43
46
  let pollingTimer = null;
47
+ let running = false;
44
48
  let deactivated = false;
45
49
  let hidden = false;
46
50
  let unmounted = false;
47
51
  const run = Object.assign(async (options = {}) => {
48
52
  if (!runnable() || abortOnUnmount && unmounted) return;
53
+ if (abortPrevious && !isCachedRequestPending()) controller.abort();
54
+ if (abortPrevious) cachedRequest = void 0;
55
+ if (abortPrevious || controller.signal.aborted) controller = new AbortController();
56
+ const runId = ++latestRunId;
57
+ const runController = controller;
58
+ running = true;
49
59
  pollingCanceled.value = false;
50
- if (controller.signal.aborted) controller = new AbortController();
51
60
  if ((options.resetValue ?? initialResetValue ?? false) === true) resetValue(options);
52
61
  uploadProgress.value = 0;
53
62
  downloadProgress.value = 0;
@@ -56,7 +65,7 @@ function createUseAxle(options) {
56
65
  const config = options.config ?? normalizeValueGetter(initialConfigOrGetter);
57
66
  const normalizedCacheKey = cacheKey ? normalizeValueGetter(cacheKey) : void 0;
58
67
  onBefore(refs);
59
- loading.value = true;
68
+ loading.value = !options.silent;
60
69
  try {
61
70
  cleanupCacheBuffer();
62
71
  const response = await getResponse();
@@ -65,37 +74,53 @@ function createUseAxle(options) {
65
74
  cache.response = response;
66
75
  cache.expiredTime = Date.now() + cacheTime;
67
76
  }
68
- value.value = await onTransform(response, refs);
77
+ if (!isLatestRun()) return response;
78
+ const transformedValue = await onTransform(response, refs);
79
+ if (!isLatestRun()) return response;
80
+ value.value = transformedValue;
69
81
  error.value = void 0;
70
82
  onSuccess(response, refs);
71
83
  loading.value = false;
72
84
  onAfter(refs);
73
85
  return response;
74
86
  } catch (responseError) {
87
+ if (!isLatestRun()) throw responseError;
75
88
  error.value = responseError;
76
89
  onError(responseError, refs);
77
90
  loading.value = false;
78
91
  onAfter(refs);
79
92
  throw responseError;
80
93
  } finally {
81
- clearPolling();
82
- startPolling();
94
+ if (isLatestRun()) {
95
+ running = false;
96
+ clearPolling();
97
+ startPolling();
98
+ }
99
+ }
100
+ function isLatestRun() {
101
+ return !abortPrevious || runId === latestRunId;
83
102
  }
84
103
  function getResponse() {
85
104
  if (shouldUseCache(normalizedCacheKey)) return JSON.parse(JSON.stringify(cacheBuffer.get(normalizedCacheKey).response));
86
105
  if (shouldAwaitCachePromise(normalizedCacheKey)) return cacheBuffer.get(normalizedCacheKey).promise;
87
106
  const promise = fetchResponse();
88
- if (shouldSetCachePromise(normalizedCacheKey)) cacheBuffer.set(normalizedCacheKey, { promise });
107
+ if (shouldSetCachePromise(normalizedCacheKey)) {
108
+ cacheBuffer.set(normalizedCacheKey, { promise });
109
+ cachedRequest = {
110
+ key: normalizedCacheKey,
111
+ promise
112
+ };
113
+ }
89
114
  return promise;
90
115
  }
91
116
  function fetchResponse() {
92
117
  return axle[method](url, params, {
93
- signal: controller.signal,
118
+ signal: runController.signal,
94
119
  onUploadProgress(event) {
95
- uploadProgress.value = event.progress ?? 0;
120
+ if (isLatestRun()) uploadProgress.value = event.progress ?? 0;
96
121
  },
97
122
  onDownloadProgress(event) {
98
- downloadProgress.value = event.progress ?? 0;
123
+ if (isLatestRun()) downloadProgress.value = event.progress ?? 0;
99
124
  },
100
125
  ...config
101
126
  });
@@ -163,12 +188,20 @@ function createUseAxle(options) {
163
188
  runWithInitialConfig();
164
189
  });
165
190
  }
191
+ if (isFunction(pollingInterval)) watch(pollingInterval, () => {
192
+ clearPolling();
193
+ startPolling();
194
+ });
166
195
  }
167
- function runWithInitialConfig() {
196
+ function runWithInitialConfig({ silent = false } = {}) {
168
197
  return run({
169
198
  url: normalizeValueGetter(initialUrlOrGetter),
170
199
  params: normalizeValueGetter(initialParamsOrGetter),
171
- config: normalizeValueGetter(initialConfigOrGetter)
200
+ config: normalizeValueGetter(initialConfigOrGetter),
201
+ silent
202
+ }).catch((runError) => {
203
+ if (isCancel(runError)) return;
204
+ throw runError;
172
205
  });
173
206
  }
174
207
  function resetValue(options = {}) {
@@ -176,6 +209,13 @@ function createUseAxle(options) {
176
209
  }
177
210
  function abort() {
178
211
  controller.abort();
212
+ if (isCachedRequestPending()) cacheBuffer.delete(cachedRequest.key);
213
+ cachedRequest = void 0;
214
+ }
215
+ function isCachedRequestPending() {
216
+ if (!cachedRequest) return false;
217
+ const buffer = cacheBuffer.get(cachedRequest.key);
218
+ return buffer?.promise === cachedRequest.promise && buffer.response == null;
179
219
  }
180
220
  function addWindowFocusListener() {
181
221
  if (typeof window !== "undefined") window.addEventListener("focus", handleWindowFocus);
@@ -198,10 +238,12 @@ function createUseAxle(options) {
198
238
  hidden ? clearPolling() : startPolling();
199
239
  }
200
240
  function startPolling() {
201
- if (pollingInterval == null || pollingCanceled.value || pollingTimer != null || deactivated && !pollingOnDeactivated || hidden && !pollingOnHidden) return;
241
+ const interval = normalizeValueGetter(pollingInterval);
242
+ if (interval == null || running || pollingCanceled.value || pollingTimer != null || deactivated && !pollingOnDeactivated || hidden && !pollingOnHidden) return;
202
243
  pollingTimer = setTimeout(() => {
203
- runWithInitialConfig();
204
- }, pollingInterval);
244
+ pollingTimer = null;
245
+ runWithInitialConfig({ silent: pollingSilent });
246
+ }, interval);
205
247
  }
206
248
  function clearPolling() {
207
249
  if (pollingTimer == null) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Progressive request tool based on axios",
5
5
  "keywords": [
6
6
  "axios",
@@ -56,21 +56,34 @@
56
56
  "crypto-js": "^4.2.0",
57
57
  "lodash": "4.17.21",
58
58
  "qs": "^6.11.0",
59
- "rattail": "^1.8.1"
59
+ "rattail": "^2.0.3"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@types/crypto-js": "^4.2.1",
63
63
  "@types/lodash": "4.14.202",
64
64
  "@types/qs": "^6.9.7",
65
- "typescript": "^5.1.5",
66
- "vite-plus": "^0.1.16",
67
- "vue": "3.3.4"
65
+ "oxc-minify": "^0.125.0",
66
+ "typescript": "5.3.3",
67
+ "vite-plus": "0.1.18",
68
+ "vitepress": "2.0.0-alpha.17",
69
+ "vue": "3.5.32"
68
70
  },
69
71
  "peerDependencies": {
70
72
  "vue": "^3.2.0"
71
73
  },
74
+ "engines": {
75
+ "pnpm": ">=10.0.0"
76
+ },
72
77
  "scripts": {
73
78
  "build": "vp pack",
74
- "dev": "vp pack --watch"
79
+ "clean": "rt clean",
80
+ "dev": "vp pack --watch",
81
+ "docs:build": "vitepress build docs",
82
+ "docs:dev": "vitepress dev docs",
83
+ "docs:preview": "vitepress preview docs",
84
+ "lint": "vp lint --fix",
85
+ "format": "vp fmt",
86
+ "release": "pnpm build && rt release",
87
+ "test": "vp test run"
75
88
  }
76
89
  }