refactor: use fetch cache arg instead of cache busting (#3584)
This commit is contained in:
@ -119,19 +119,21 @@ export type FileMetadata = {
|
||||
const getFileMetadataKey = (id: string) => `gh.${id}.meta`;
|
||||
|
||||
export const readFileMetadata = async (
|
||||
id: string,
|
||||
id: string | null | undefined,
|
||||
fetchMetadata: () => Promise<FileMetadata>,
|
||||
localForage: LocalForage,
|
||||
) => {
|
||||
const key = getFileMetadataKey(id);
|
||||
const cached = await localForage.getItem<FileMetadata>(key);
|
||||
const key = id ? getFileMetadataKey(id) : null;
|
||||
const cached = key && (await localForage.getItem<FileMetadata>(key));
|
||||
if (cached) {
|
||||
return cached;
|
||||
} else {
|
||||
const metadata = await fetchMetadata();
|
||||
await localForage.setItem<FileMetadata>(key, metadata);
|
||||
return metadata;
|
||||
}
|
||||
|
||||
const metadata = await fetchMetadata();
|
||||
if (key) {
|
||||
await localForage.setItem<FileMetadata>(key, metadata);
|
||||
}
|
||||
return metadata;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -162,7 +162,7 @@ type ReadFile = (
|
||||
options: { parseText: boolean },
|
||||
) => Promise<string | Blob>;
|
||||
|
||||
type ReadFileMetadata = (path: string, id: string) => Promise<FileMetadata>;
|
||||
type ReadFileMetadata = (path: string, id: string | null | undefined) => Promise<FileMetadata>;
|
||||
|
||||
type ReadUnpublishedFile = (
|
||||
key: string,
|
||||
@ -183,9 +183,7 @@ const fetchFiles = async (
|
||||
try {
|
||||
const [data, fileMetadata] = await Promise.all([
|
||||
readFile(file.path, file.id, { parseText: true }),
|
||||
file.id
|
||||
? readFileMetadata(file.path, file.id)
|
||||
: Promise.resolve({ author: '', updatedOn: '' }),
|
||||
readFileMetadata(file.path, file.id),
|
||||
]);
|
||||
resolve({ file: { ...file, ...fileMetadata }, data: data as string });
|
||||
sem.leave();
|
||||
|
@ -49,26 +49,23 @@ const ensureRequestArg = func => req => func(maybeRequestArg(req));
|
||||
const ensureRequestArg2 = func => (arg, req) => func(arg, maybeRequestArg(req));
|
||||
|
||||
// This actually performs the built request object
|
||||
const performRequest = ensureRequestArg(req => fetch(...toFetchArguments(req)));
|
||||
const performRequest = ensureRequestArg(req => {
|
||||
const args = toFetchArguments(req);
|
||||
return fetch(...args);
|
||||
});
|
||||
|
||||
// Each of the following functions takes options and returns another
|
||||
// function that performs the requested action on a request. They each
|
||||
// default to containing an empty object, so you can simply call them
|
||||
// without arguments to generate a request with only those properties.
|
||||
// function that performs the requested action on a request.
|
||||
const getCurriedRequestProcessor = flow([ensureRequestArg2, curry]);
|
||||
const getPropSetFunctions = path => [
|
||||
getCurriedRequestProcessor((val, req) => req.setIn(path, val)),
|
||||
getCurriedRequestProcessor((val, req) => (req.getIn(path) ? req : req.setIn(path, val))),
|
||||
];
|
||||
const getPropMergeFunctions = path => [
|
||||
getCurriedRequestProcessor((obj, req) => req.updateIn(path, (p = Map()) => p.merge(obj))),
|
||||
getCurriedRequestProcessor((obj, req) => req.updateIn(path, (p = Map()) => Map(obj).merge(p))),
|
||||
];
|
||||
const getPropSetFunction = path => getCurriedRequestProcessor((val, req) => req.setIn(path, val));
|
||||
const getPropMergeFunction = path =>
|
||||
getCurriedRequestProcessor((obj, req) => req.updateIn(path, (p = Map()) => p.merge(obj)));
|
||||
|
||||
const [withMethod, withDefaultMethod] = getPropSetFunctions(['method']);
|
||||
const [withBody, withDefaultBody] = getPropSetFunctions(['body']);
|
||||
const [withParams, withDefaultParams] = getPropMergeFunctions(['params']);
|
||||
const [withHeaders, withDefaultHeaders] = getPropMergeFunctions(['headers']);
|
||||
const withMethod = getPropSetFunction(['method']);
|
||||
const withBody = getPropSetFunction(['body']);
|
||||
const withNoCache = getPropSetFunction(['cache'])('no-cache');
|
||||
const withParams = getPropMergeFunction(['params']);
|
||||
const withHeaders = getPropMergeFunction(['headers']);
|
||||
|
||||
// withRoot sets a root URL, unless the URL is already absolute
|
||||
const absolutePath = new RegExp('^(?:[a-z]+:)?//', 'i');
|
||||
@ -83,24 +80,15 @@ const withRoot = getCurriedRequestProcessor((root, req) =>
|
||||
}),
|
||||
);
|
||||
|
||||
// withTimestamp needs no argument and has to run as late as possible,
|
||||
// so it calls `withParams` only when it's actually called with a
|
||||
// request.
|
||||
const withTimestamp = ensureRequestArg(req => withParams({ ts: new Date().getTime() }, req));
|
||||
|
||||
export default {
|
||||
toURL,
|
||||
fromURL,
|
||||
fromFetchArguments,
|
||||
performRequest,
|
||||
withMethod,
|
||||
withDefaultMethod,
|
||||
withBody,
|
||||
withDefaultBody,
|
||||
withHeaders,
|
||||
withDefaultHeaders,
|
||||
withParams,
|
||||
withDefaultParams,
|
||||
withRoot,
|
||||
withTimestamp,
|
||||
withNoCache,
|
||||
};
|
||||
|
Reference in New Issue
Block a user