Skip to content

Commit 7b18692

Browse files
committed
cleanup
1 parent 02b5520 commit 7b18692

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

packages/compass-schema/src/components/export-schema-modal.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { type ChangeEvent, useCallback, useMemo } from 'react';
1+
import React, { type ChangeEvent, useCallback } from 'react';
22
import { connect } from 'react-redux';
33
import {
44
Button,
@@ -81,7 +81,7 @@ const ExportSchemaModal: React.FunctionComponent<{
8181
resultId?: string;
8282
exportFormat: SchemaFormat;
8383
exportedSchema?: string;
84-
blobToDownload?: Blob;
84+
downloadUrl?: string;
8585
filename?: string;
8686
onCancelSchemaExport: () => void;
8787
onChangeSchemaExportFormat: (format: SchemaFormat) => Promise<void>;
@@ -94,7 +94,7 @@ const ExportSchemaModal: React.FunctionComponent<{
9494
isOpen,
9595
exportFormat,
9696
exportedSchema,
97-
blobToDownload,
97+
downloadUrl,
9898
filename,
9999
onCancelSchemaExport,
100100
onChangeSchemaExportFormat,
@@ -111,11 +111,6 @@ const ExportSchemaModal: React.FunctionComponent<{
111111
[onChangeSchemaExportFormat]
112112
);
113113

114-
const downloadUrl = useMemo<string | undefined>(() => {
115-
if (!blobToDownload) return;
116-
return window.URL.createObjectURL(blobToDownload);
117-
}, [blobToDownload]);
118-
119114
return (
120115
<Modal open={isOpen} setOpen={onClose}>
121116
<ModalHeader title="Export Schema" />
@@ -213,7 +208,7 @@ export default connect(
213208
exportFormat: state.schemaExport.exportFormat,
214209
isOpen: state.schemaExport.isOpen,
215210
exportedSchema: state.schemaExport.exportedSchema,
216-
blobToDownload: state.schemaExport.blobToDownload,
211+
downloadUrl: state.schemaExport.downloadUrl,
217212
filename: state.schemaExport.filename,
218213
}),
219214
{

packages/compass-schema/src/stores/schema-export-reducer.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type SchemaExportState = {
2727
exportFormat: SchemaFormat;
2828
errorMessage?: string;
2929
exportStatus: ExportStatus;
30-
blobToDownload?: Blob;
30+
downloadUrl?: string;
3131
filename?: string;
3232
};
3333

@@ -110,7 +110,7 @@ export type ChangeExportSchemaFormatCompletedAction = {
110110

111111
export type schemaDownloadReadyAction = {
112112
type: SchemaExportActions.schemaDownloadReady;
113-
blob: Blob;
113+
downloadUrl: string;
114114
filename: string;
115115
};
116116

@@ -127,6 +127,20 @@ export const cancelExportSchema = (): SchemaThunkAction<
127127
};
128128
};
129129

130+
export const cleanupObjectUrl = (): SchemaThunkAction<
131+
void,
132+
CancelExportSchemaAction
133+
> => {
134+
return (dispatch, getState) => {
135+
const {
136+
schemaExport: { downloadUrl },
137+
} = getState();
138+
if (downloadUrl) {
139+
URL.revokeObjectURL(downloadUrl);
140+
}
141+
};
142+
};
143+
130144
async function getSchemaByFormat({
131145
exportFormat,
132146
schemaAccessor,
@@ -228,20 +242,26 @@ export const trackSchemaExported = (): SchemaThunkAction<void> => {
228242

229243
const prepareDownload = (): SchemaThunkAction<void> => {
230244
return (dispatch, getState, { namespace }) => {
231-
const { exportedSchema, exportFormat } = getState().schemaExport;
245+
const {
246+
exportedSchema,
247+
exportFormat,
248+
downloadUrl: previousDownloadUrl,
249+
} = getState().schemaExport;
232250
if (!exportedSchema) return;
233251

234252
try {
253+
if (previousDownloadUrl) URL.revokeObjectURL(previousDownloadUrl);
235254
const blob = new Blob([exportedSchema], {
236255
type: 'application/json',
237256
});
257+
const downloadUrl = URL.createObjectURL(blob);
238258
const filename = `schema-${namespace.replace(
239259
'.',
240260
'-'
241261
)}-${exportFormat}.json`;
242262
dispatch({
243263
type: SchemaExportActions.schemaDownloadReady,
244-
blob,
264+
downloadUrl,
245265
filename,
246266
});
247267
} catch (error) {
@@ -463,7 +483,7 @@ export const schemaExportReducer: Reducer<SchemaExportState, Action> = (
463483
) {
464484
return {
465485
...state,
466-
blobToDownload: action.blob,
486+
downloadUrl: action.downloadUrl,
467487
filename: action.filename,
468488
};
469489
}

packages/compass-schema/src/stores/store.spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe('Schema Store', function () {
163163
errorMessage,
164164
exportedSchema,
165165
filename,
166-
blobToDownload,
166+
downloadUrl,
167167
} = store.getState().schemaExport;
168168
expect(exportStatus).to.equal('complete');
169169
expect(!!errorMessage).to.be.false;
@@ -177,9 +177,8 @@ describe('Schema Store', function () {
177177
name: { type: 'string' },
178178
});
179179
expect(filename).to.equal('schema-db-coll-standardJSON.json');
180-
expect(blobToDownload).to.be.a('Blob');
181-
expect(blobToDownload?.type).to.equal('application/json');
182-
expect(blobToDownload?.size).to.be.greaterThan(0);
180+
expect(downloadUrl).to.be.a('string');
181+
expect(/blob:(.*)/.test(downloadUrl || '')).to.be.true;
183182
});
184183

185184
it('runs schema export formatting with a new format', async function () {
@@ -191,7 +190,7 @@ describe('Schema Store', function () {
191190
errorMessage,
192191
exportedSchema,
193192
filename,
194-
blobToDownload,
193+
downloadUrl,
195194
} = store.getState().schemaExport;
196195
expect(exportStatus).to.equal('complete');
197196
expect(!!errorMessage).to.be.false;
@@ -204,9 +203,8 @@ describe('Schema Store', function () {
204203
name: { bsonType: 'string' },
205204
});
206205
expect(filename).to.equal('schema-db-coll-mongoDBJSON.json');
207-
expect(blobToDownload).to.be.a('Blob');
208-
expect(blobToDownload?.type).to.equal('application/json');
209-
expect(blobToDownload?.size).to.be.greaterThan(0);
206+
expect(downloadUrl).to.be.a('string');
207+
expect(/blob:(.*)/.test(downloadUrl || '')).to.be.true;
210208
});
211209
});
212210
});

packages/compass-schema/src/stores/store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from './schema-analysis-reducer';
2525
import {
2626
cancelExportSchema,
27+
cleanupObjectUrl,
2728
confirmedExportLegacySchemaToClipboard,
2829
openLegacyBanner,
2930
schemaExportReducer,
@@ -93,6 +94,7 @@ export function activateSchemaPlugin(
9394

9495
addCleanup(() => store.dispatch(cleanupAnalysis()));
9596
addCleanup(() => store.dispatch(cancelExportSchema()));
97+
addCleanup(() => store.dispatch(cleanupObjectUrl()));
9698

9799
return {
98100
store,

0 commit comments

Comments
 (0)