@varlet/axle 1.1.0 → 1.1.1

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/use.mjs +34 -27
  2. package/package.json +1 -1
package/dist/use.mjs CHANGED
@@ -42,7 +42,7 @@ function createUseAxle(options) {
42
42
  };
43
43
  let controller = new AbortController();
44
44
  let latestRunId = 0;
45
- let cachedRequest;
45
+ let pendingCaches = [];
46
46
  let pollingTimer = null;
47
47
  let running = false;
48
48
  let deactivated = false;
@@ -50,27 +50,24 @@ function createUseAxle(options) {
50
50
  let unmounted = false;
51
51
  const run = Object.assign(async (options = {}) => {
52
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();
53
+ if (abortPrevious && getPendingCaches().length === 0) controller.abort();
54
+ if (controller.signal.aborted) controller = new AbortController();
56
55
  const runId = ++latestRunId;
57
56
  const runController = controller;
58
57
  running = true;
59
58
  pollingCanceled.value = false;
60
- if ((options.resetValue ?? initialResetValue ?? false) === true) resetValue(options);
61
- uploadProgress.value = 0;
62
- downloadProgress.value = 0;
63
- const url = options.url ?? normalizeValueGetter(initialUrlOrGetter);
64
- const params = options.params ?? normalizeValueGetter(initialParamsOrGetter);
65
- const config = options.config ?? normalizeValueGetter(initialConfigOrGetter);
66
- const normalizedCacheKey = cacheKey ? normalizeValueGetter(cacheKey) : void 0;
67
- onBefore(refs);
68
- loading.value = !options.silent;
59
+ clearPolling();
69
60
  try {
61
+ if ((options.resetValue ?? initialResetValue ?? false) === true) resetValue(options);
62
+ uploadProgress.value = 0;
63
+ downloadProgress.value = 0;
64
+ const request = createRequest();
65
+ onBefore(refs);
66
+ if (!options.silent) loading.value = true;
70
67
  cleanupCacheBuffer();
71
- const response = await getResponse();
72
- if (shouldSetCacheResponse(normalizedCacheKey)) {
73
- const cache = cacheBuffer.get(normalizedCacheKey);
68
+ const response = await getResponse(request);
69
+ if (shouldSetCacheResponse(request.cacheKey)) {
70
+ const cache = cacheBuffer.get(request.cacheKey);
74
71
  cache.response = response;
75
72
  cache.expiredTime = Date.now() + cacheTime;
76
73
  }
@@ -100,20 +97,29 @@ function createUseAxle(options) {
100
97
  function isLatestRun() {
101
98
  return !abortPrevious || runId === latestRunId;
102
99
  }
103
- function getResponse() {
100
+ function createRequest() {
101
+ return {
102
+ url: options.url ?? normalizeValueGetter(initialUrlOrGetter),
103
+ params: options.params ?? normalizeValueGetter(initialParamsOrGetter),
104
+ config: options.config ?? normalizeValueGetter(initialConfigOrGetter),
105
+ cacheKey: cacheKey ? normalizeValueGetter(cacheKey) : void 0
106
+ };
107
+ }
108
+ function getResponse(request) {
109
+ const { cacheKey: normalizedCacheKey } = request;
104
110
  if (shouldUseCache(normalizedCacheKey)) return JSON.parse(JSON.stringify(cacheBuffer.get(normalizedCacheKey).response));
105
111
  if (shouldAwaitCachePromise(normalizedCacheKey)) return cacheBuffer.get(normalizedCacheKey).promise;
106
- const promise = fetchResponse();
112
+ const promise = fetchResponse(request);
107
113
  if (shouldSetCachePromise(normalizedCacheKey)) {
108
114
  cacheBuffer.set(normalizedCacheKey, { promise });
109
- cachedRequest = {
115
+ pendingCaches = [...getPendingCaches(), {
110
116
  key: normalizedCacheKey,
111
117
  promise
112
- };
118
+ }];
113
119
  }
114
120
  return promise;
115
121
  }
116
- function fetchResponse() {
122
+ function fetchResponse({ url, params, config }) {
117
123
  return axle[method](url, params, {
118
124
  signal: runController.signal,
119
125
  onUploadProgress(event) {
@@ -209,13 +215,14 @@ function createUseAxle(options) {
209
215
  }
210
216
  function abort() {
211
217
  controller.abort();
212
- if (isCachedRequestPending()) cacheBuffer.delete(cachedRequest.key);
213
- cachedRequest = void 0;
218
+ getPendingCaches().forEach(({ key }) => cacheBuffer.delete(key));
219
+ pendingCaches = [];
214
220
  }
215
- function isCachedRequestPending() {
216
- if (!cachedRequest) return false;
217
- const buffer = cacheBuffer.get(cachedRequest.key);
218
- return buffer?.promise === cachedRequest.promise && buffer.response == null;
221
+ function getPendingCaches() {
222
+ return pendingCaches.filter(({ key, promise }) => {
223
+ const buffer = cacheBuffer.get(key);
224
+ return buffer?.promise === promise && buffer.response == null;
225
+ });
219
226
  }
220
227
  function addWindowFocusListener() {
221
228
  if (typeof window !== "undefined") window.addEventListener("focus", handleWindowFocus);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Progressive request tool based on axios",
5
5
  "keywords": [
6
6
  "axios",