Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: change the default import to import on demand #2230

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions packages/hooks/src/useRequest/src/plugins/useCachePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { useRef } from 'react';
import useCreation from '../../../useCreation';
import useUnmount from '../../../useUnmount';
import type { Plugin } from '../types';
import * as cache from '../utils/cache';
import { setCache, getCache } from '../utils/cache';
import type { CachedData } from '../utils/cache';
import * as cachePromise from '../utils/cachePromise';
import * as cacheSubscribe from '../utils/cacheSubscribe';
import { setCachePromise, getCachePromise } from '../utils/cachePromise';
import { trigger, subscribe } from '../utils/cacheSubscribe';

const useCachePlugin: Plugin<any, any[]> = (
fetchInstance,
Expand All @@ -25,16 +25,16 @@ const useCachePlugin: Plugin<any, any[]> = (
if (customSetCache) {
customSetCache(cachedData);
} else {
cache.setCache(key, cacheTime, cachedData);
setCache(key, cacheTime, cachedData);
}
cacheSubscribe.trigger(key, cachedData.data);
trigger(key, cachedData.data);
};

const _getCache = (key: string, params: any[] = []) => {
if (customGetCache) {
return customGetCache(params);
}
return cache.getCache(key);
return getCache(key);
};

useCreation(() => {
Expand All @@ -53,7 +53,7 @@ const useCachePlugin: Plugin<any, any[]> = (
}

// subscribe same cachekey update, trigger update
unSubscribeRef.current = cacheSubscribe.subscribe(cacheKey, (data) => {
unSubscribeRef.current = subscribe(cacheKey, (data) => {
fetchInstance.setState({ data });
});
}, []);
Expand Down Expand Up @@ -91,7 +91,7 @@ const useCachePlugin: Plugin<any, any[]> = (
}
},
onRequest: (service, args) => {
let servicePromise = cachePromise.getCachePromise(cacheKey);
let servicePromise = getCachePromise(cacheKey);

// If has servicePromise, and is not trigger by self, then use it
if (servicePromise && servicePromise !== currentPromiseRef.current) {
Expand All @@ -100,7 +100,7 @@ const useCachePlugin: Plugin<any, any[]> = (

servicePromise = service(...args);
currentPromiseRef.current = servicePromise;
cachePromise.setCachePromise(cacheKey, servicePromise);
setCachePromise(cacheKey, servicePromise);
return { servicePromise };
},
onSuccess: (data, params) => {
Expand All @@ -113,7 +113,7 @@ const useCachePlugin: Plugin<any, any[]> = (
time: new Date().getTime(),
});
// resubscribe
unSubscribeRef.current = cacheSubscribe.subscribe(cacheKey, (d) => {
unSubscribeRef.current = subscribe(cacheKey, (d) => {
fetchInstance.setState({ data: d });
});
}
Expand All @@ -128,7 +128,7 @@ const useCachePlugin: Plugin<any, any[]> = (
time: new Date().getTime(),
});
// resubscribe
unSubscribeRef.current = cacheSubscribe.subscribe(cacheKey, (d) => {
unSubscribeRef.current = subscribe(cacheKey, (d) => {
fetchInstance.setState({ data: d });
});
}
Expand Down