@varlet/axle 1.0.0 → 1.0.2

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