Skip to content

Commit

Permalink
Fix Safari bug with updating IndexedDB records that contain files (bl…
Browse files Browse the repository at this point in the history
…obs) (#1189)
  • Loading branch information
azangru authored Dec 17, 2024
1 parent f5f9b91 commit 2c1ce1a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
26 changes: 13 additions & 13 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ const isTargetWeb = api =>
api.caller((caller) => caller && caller.target === 'web');

module.exports = (api) => {
const targets = isTargetWeb(api) ? {
esmodules: true
} : {
node: 'current'
const babelPresetEnvOptions = {
// debug: true, // <-- Enable if you want to debug what babel is doing
useBuiltIns: 'usage',
corejs: 3.39,
modules: false
};
if (!isTargetWeb(api)) {
// change babel compile target for node
babelPresetEnvOptions.targets = { node: 'current' };
}

return {
presets: [
Expand All @@ -15,23 +20,18 @@ module.exports = (api) => {
}],
'@babel/typescript',
[
'@babel/env',
{
useBuiltIns: 'usage',
corejs: 3.26,
modules: false,
targets
}
'@babel/preset-env',
babelPresetEnvOptions
]
],
env: {
test: {
presets: [
[
'@babel/env',
'@babel/preset-env',
{
useBuiltIns: 'usage',
corejs: 3.26
corejs: 3.39
}
]
]
Expand Down
17 changes: 17 additions & 0 deletions src/content/app/tools/vep/services/vepStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ export const updateVepSubmission = async (
return;
}

// The if block below is a hack due to IndexedDB (mis)behaviour in Safari.
// Safari seems to dislike it when you store a record one of whose fields is a file (i.e. a blob),
// and when you then retrieve that record from the db, change one of the fields that is not the file field,
// and save it back to the db.
// In such cases, IndexedDB in Safari (as recent as Safari 18) fails with an error:
// 'UnknownError: Error preparing Blob/File data to be stored in object store'
//
// The if block below clones the stored file into a new one. This is silly additional CPU work and memory pressure.
// TODO: observe in newer Safaris if this remains a problem, and remove this hack if no longer is.
if (storedSubmission.inputFile) {
const fileClone = await storedSubmission.inputFile.arrayBuffer();
storedSubmission.inputFile = new File(
[fileClone],
storedSubmission.inputFile.name
);
}

const updatedSubmission = {
...storedSubmission,
...fragment
Expand Down
21 changes: 10 additions & 11 deletions src/content/app/tools/vep/state/vep-form/vepFormSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,24 +240,16 @@ export const onVepFormSubmission = createAsyncThunk(
const inputText = vepFormState.inputText;
const parameters = vepFormState.parameters;

const requestPayload = await prepareRequestPayload({
submissionId,
species,
inputText,
parameters
});

// "Detach" the submission from the VEP form by assigning it another temporary id.
// The id will be eventually finalized to a permanent one after the server response
const updatedSubmissionId = createInFlightSubmissionId();
requestPayload.submission_id = updatedSubmissionId;

await updateVepSubmission(submissionId, {
id: submissionId,
species: vepFormState.selectedSpecies,
submissionName: vepFormState.submissionName,
inputText: vepFormState.inputText,
parameters: vepFormState.parameters,
species,
inputText,
parameters,
submittedAt: Date.now(),
status: 'SUBMITTING'
});
Expand All @@ -271,6 +263,13 @@ export const onVepFormSubmission = createAsyncThunk(
addSubmission(updatedStoredSubmission as VepSubmissionWithoutInputFile)
);

const requestPayload = await prepareRequestPayload({
submissionId: updatedSubmissionId,
species,
inputText,
parameters
});

dispatch(vepFormSubmit.initiate(requestPayload, { track: false }));
}
);
Expand Down

0 comments on commit 2c1ce1a

Please sign in to comment.