Skip to content

Commit

Permalink
Return undefined methods if cache api not available
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreSi committed Aug 8, 2023
1 parent bd3490c commit 94070e8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { unzipFirstEntryOfBlob } from '../../Utils/Zip.js/Utils';
export const CLOUD_PROJECT_AUTOSAVE_CACHE_KEY =
'gdevelop-cloud-project-autosave';
const CLOUD_PROJECT_AUTOSAVE_PREFIX = 'cache-autosave:';
export const isCacheApiAvailable = 'caches' in window;

class CloudProjectReadingError extends Error {
constructor() {
Expand Down Expand Up @@ -97,44 +98,47 @@ export const generateOnEnsureCanAccessResources = (

export const generateGetAutoSaveCreationDate = (
authenticatedUser: AuthenticatedUser
) => async (
fileMetadata: FileMetadata,
compareLastModified: boolean
): Promise<?number> => {
if (!('caches' in window)) return null;
const { profile } = authenticatedUser;
if (!profile) return null;

const hasCache = await caches.has(CLOUD_PROJECT_AUTOSAVE_CACHE_KEY);
if (!hasCache) return null;

const cloudProjectId = fileMetadata.fileIdentifier;
const cache = await caches.open(CLOUD_PROJECT_AUTOSAVE_CACHE_KEY);
const cacheKey = `${profile.id}/${cloudProjectId}`;
const cachedResponse = await cache.match(cacheKey);
if (!cachedResponse) return null;

const cachedResponseBody = await cachedResponse.text();
const autoSavedTime = JSON.parse(cachedResponseBody).createdAt;
if (!compareLastModified) return autoSavedTime;

const saveTime = fileMetadata.lastModifiedDate;
if (!saveTime) return null;

return autoSavedTime > saveTime + 5000 ? autoSavedTime : null;
};

export const generateOnGetAutoSave = (
authenticatedUser: AuthenticatedUser
) => async (fileMetadata: FileMetadata): Promise<FileMetadata> => {
if (!('caches' in window)) return fileMetadata;
return {
...fileMetadata,
fileIdentifier: CLOUD_PROJECT_AUTOSAVE_PREFIX + fileMetadata.fileIdentifier,
};
};
) =>
isCacheApiAvailable
? async (
fileMetadata: FileMetadata,
compareLastModified: boolean
): Promise<?number> => {
const { profile } = authenticatedUser;
if (!profile) return null;

const hasCache = await caches.has(CLOUD_PROJECT_AUTOSAVE_CACHE_KEY);
if (!hasCache) return null;

const cloudProjectId = fileMetadata.fileIdentifier;
const cache = await caches.open(CLOUD_PROJECT_AUTOSAVE_CACHE_KEY);
const cacheKey = `${profile.id}/${cloudProjectId}`;
const cachedResponse = await cache.match(cacheKey);
if (!cachedResponse) return null;

const cachedResponseBody = await cachedResponse.text();
const autoSavedTime = JSON.parse(cachedResponseBody).createdAt;
if (!compareLastModified) return autoSavedTime;

const saveTime = fileMetadata.lastModifiedDate;
if (!saveTime) return null;

return autoSavedTime > saveTime + 5000 ? autoSavedTime : null;
}
: undefined;

export const generateOnGetAutoSave = (authenticatedUser: AuthenticatedUser) =>
isCacheApiAvailable
? async (fileMetadata: FileMetadata): Promise<FileMetadata> => {
return {
...fileMetadata,
fileIdentifier:
CLOUD_PROJECT_AUTOSAVE_PREFIX + fileMetadata.fileIdentifier,
};
}
: undefined;

export const burstCloudProjectAutoSaveCache = async () => {
if (!('caches' in window)) return;
if (!isCacheApiAvailable) return;
await caches.delete(CLOUD_PROJECT_AUTOSAVE_CACHE_KEY);
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
createZipWithSingleTextFile,
unzipFirstEntryOfBlob,
} from '../../Utils/Zip.js/Utils';
import { CLOUD_PROJECT_AUTOSAVE_CACHE_KEY } from './CloudProjectOpener';
import {
CLOUD_PROJECT_AUTOSAVE_CACHE_KEY,
isCacheApiAvailable,
} from './CloudProjectOpener';

const zipProject = async (project: gdProject): Promise<[Blob, string]> => {
const projectJson = serializeToJSON(project);
Expand Down Expand Up @@ -281,20 +284,22 @@ export const onRenderNewProjectSaveAsLocationChooser = ({

export const generateOnAutoSaveProject = (
authenticatedUser: AuthenticatedUser
) => async (project: gdProject, fileMetadata: FileMetadata): Promise<void> => {
if (!('caches' in window)) return;
const { profile } = authenticatedUser;
if (!profile) return;
const cloudProjectId = fileMetadata.fileIdentifier;
const cache = await caches.open(CLOUD_PROJECT_AUTOSAVE_CACHE_KEY);
const cacheKey = `${profile.id}/${cloudProjectId}`;
cache.put(
cacheKey,
new Response(
JSON.stringify({
project: serializeToJSON(project),
createdAt: Date.now(),
})
)
);
};
) =>
isCacheApiAvailable
? async (project: gdProject, fileMetadata: FileMetadata): Promise<void> => {
const { profile } = authenticatedUser;
if (!profile) return;
const cloudProjectId = fileMetadata.fileIdentifier;
const cache = await caches.open(CLOUD_PROJECT_AUTOSAVE_CACHE_KEY);
const cacheKey = `${profile.id}/${cloudProjectId}`;
cache.put(
cacheKey,
new Response(
JSON.stringify({
project: serializeToJSON(project),
createdAt: Date.now(),
})
)
);
}
: undefined;

0 comments on commit 94070e8

Please sign in to comment.