Skip to content

Commit 396c151

Browse files
authored
chore(schema): move non serializable services off of state to services (#6717)
1 parent a203c74 commit 396c151

File tree

6 files changed

+67
-83
lines changed

6 files changed

+67
-83
lines changed

packages/compass-schema/src/components/schema-toolbar/schema-toolbar.spec.tsx

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import React from 'react';
33
import { render, screen } from '@mongodb-js/testing-library-compass';
44
import { expect } from 'chai';
55
import sinon from 'sinon';
6-
import {
7-
createSandboxFromDefaultPreferences,
8-
type PreferencesAccess,
9-
} from 'compass-preferences-model';
10-
import { PreferencesProvider } from 'compass-preferences-model/provider';
6+
import type { AllPreferences } from 'compass-preferences-model';
117
import { SchemaToolbar } from './schema-toolbar';
128
import QueryBarPlugin from '@mongodb-js/compass-query-bar';
139
import {
@@ -35,33 +31,28 @@ const testErrorMessage =
3531
const exportSchemaTestId = 'open-schema-export-button';
3632

3733
describe('SchemaToolbar', function () {
38-
let defaultPreferences: PreferencesAccess;
39-
40-
before(async function () {
41-
defaultPreferences = await createSandboxFromDefaultPreferences();
42-
});
43-
4434
const renderSchemaToolbar = (
4535
props: Partial<ComponentProps<typeof SchemaToolbar>> = {},
46-
preferences: PreferencesAccess = defaultPreferences
36+
preferences: Partial<AllPreferences> = {}
4737
) => {
4838
const queryBarProps = {};
4939
render(
50-
<PreferencesProvider value={preferences}>
51-
<MockQueryBarPlugin {...(queryBarProps as any)}>
52-
<SchemaToolbar
53-
analysisState="complete"
54-
errorMessage={''}
55-
isOutdated={false}
56-
onAnalyzeSchemaClicked={() => {}}
57-
onResetClicked={() => {}}
58-
sampleSize={10}
59-
schemaResultId="123"
60-
onExportSchemaClicked={() => {}}
61-
{...props}
62-
/>
63-
</MockQueryBarPlugin>
64-
</PreferencesProvider>
40+
<MockQueryBarPlugin {...(queryBarProps as any)}>
41+
<SchemaToolbar
42+
analysisState="complete"
43+
errorMessage={''}
44+
isOutdated={false}
45+
onAnalyzeSchemaClicked={() => {}}
46+
onResetClicked={() => {}}
47+
sampleSize={10}
48+
schemaResultId="123"
49+
onExportSchemaClicked={() => {}}
50+
{...props}
51+
/>
52+
</MockQueryBarPlugin>,
53+
{
54+
preferences,
55+
}
6556
);
6657
};
6758

@@ -125,17 +116,14 @@ describe('SchemaToolbar', function () {
125116
});
126117

127118
describe('when rendered with the enableExportSchema feature flag true', function () {
128-
beforeEach(async function () {
129-
const preferences = await createSandboxFromDefaultPreferences();
130-
await preferences.savePreferences({
131-
enableExportSchema: true,
132-
});
133-
119+
beforeEach(function () {
134120
renderSchemaToolbar(
135121
{
136122
sampleSize: 100,
137123
},
138-
preferences
124+
{
125+
enableExportSchema: true,
126+
}
139127
);
140128
});
141129

packages/compass-schema/src/modules/schema-analysis.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ describe('schema-analysis', function () {
196196
);
197197
});
198198

199-
it('returns null if is cancelled', async function () {
199+
it('returns undefined if is cancelled', async function () {
200200
const dataService = {
201201
sample: () => Promise.reject(new Error('test error')),
202202
isCancelError: () => true,
@@ -214,7 +214,7 @@ describe('schema-analysis', function () {
214214
dummyLogger
215215
);
216216

217-
expect(result).to.equal(null);
217+
expect(result).to.equal(undefined);
218218
});
219219

220220
it('throws if sample throws', async function () {

packages/compass-schema/src/modules/schema-analysis.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { AggregateOptions, Filter, Document } from 'mongodb';
22
import { analyzeDocuments } from 'mongodb-schema';
33
import type {
44
Schema,
5+
SchemaAccessor,
56
ArraySchemaType,
67
DocumentSchemaType,
78
SchemaField,
@@ -35,8 +36,6 @@ function promoteMongoErrorCode(err?: Error & { code?: unknown }) {
3536
return err;
3637
}
3738

38-
export type SchemaAccessor = Awaited<ReturnType<typeof analyzeDocuments>>;
39-
4039
export const analyzeSchema = async (
4140
dataService: DataService,
4241
abortSignal: AbortSignal,
@@ -50,7 +49,7 @@ export const analyzeSchema = async (
5049
| undefined,
5150
aggregateOptions: AggregateOptions,
5251
{ log, mongoLogId, debug }: Logger
53-
): Promise<SchemaAccessor | null> => {
52+
): Promise<SchemaAccessor | undefined> => {
5453
try {
5554
log.info(mongoLogId(1001000089), 'Schema', 'Starting schema analysis', {
5655
ns,
@@ -82,7 +81,7 @@ export const analyzeSchema = async (
8281
});
8382
if (dataService.isCancelError(err)) {
8483
debug('caught background operation terminated error', err);
85-
return null;
84+
return;
8685
}
8786

8887
const error = promoteMongoErrorCode(err);

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { addLayer, generateGeoQuery } from '../modules/geo';
1414
import {
1515
analyzeSchema,
1616
calculateSchemaDepth,
17-
type SchemaAccessor,
1817
schemaContainsGeoData,
1918
} from '../modules/schema-analysis';
2019
import { capMaxTimeMSAtPreferenceLimit } from 'compass-preferences-model/provider';
@@ -32,7 +31,6 @@ export type SchemaAnalysisState = {
3231
analysisState: AnalysisState;
3332
errorMessage: string;
3433
schema: Schema | null;
35-
schemaAccessor: SchemaAccessor | null;
3634
resultId: string;
3735
};
3836

@@ -48,7 +46,6 @@ export type AnalysisStartedAction = {
4846

4947
export type AnalysisFinishedAction = {
5048
type: SchemaAnalysisActions.analysisFinished;
51-
schemaAccessor: SchemaAccessor | null;
5249
schema: Schema | null;
5350
};
5451

@@ -72,7 +69,6 @@ export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
7269
analysisState: ANALYSIS_STATE_ANALYZING,
7370
errorMessage: '',
7471
schema: null,
75-
schemaAccessor: null,
7672
};
7773
}
7874

@@ -88,7 +84,6 @@ export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
8884
? ANALYSIS_STATE_COMPLETE
8985
: ANALYSIS_STATE_INITIAL,
9086
schema: action.schema,
91-
schemaAccessor: action.schemaAccessor,
9287
resultId: resultId(),
9388
};
9489
}
@@ -129,7 +124,6 @@ const getInitialState = (): SchemaAnalysisState => ({
129124
analysisState: ANALYSIS_STATE_INITIAL,
130125
errorMessage: '',
131126
schema: null,
132-
schemaAccessor: null,
133127
resultId: resultId(),
134128
});
135129

@@ -171,9 +165,9 @@ export const geoLayersDeleted = (
171165
};
172166

173167
export const stopAnalysis = (): SchemaThunkAction<void> => {
174-
return (dispatch, getState, { abortControllerRef }) => {
175-
if (!abortControllerRef.current) return;
176-
abortControllerRef.current?.abort();
168+
return (dispatch, getState, { analysisAbortControllerRef }) => {
169+
if (!analysisAbortControllerRef.current) return;
170+
analysisAbortControllerRef.current?.abort();
177171
};
178172
};
179173

@@ -191,7 +185,8 @@ export const startAnalysis = (): SchemaThunkAction<
191185
dataService,
192186
logger,
193187
fieldStoreService,
194-
abortControllerRef,
188+
analysisAbortControllerRef,
189+
schemaAccessorRef,
195190
namespace,
196191
geoLayersRef,
197192
connectionInfoRef,
@@ -221,8 +216,8 @@ export const startAnalysis = (): SchemaThunkAction<
221216
maxTimeMS: capMaxTimeMSAtPreferenceLimit(preferences, query.maxTimeMS),
222217
};
223218

224-
abortControllerRef.current = new AbortController();
225-
const abortSignal = abortControllerRef.current.signal;
219+
analysisAbortControllerRef.current = new AbortController();
220+
const abortSignal = analysisAbortControllerRef.current.signal;
226221

227222
try {
228223
debug('analysis started');
@@ -238,6 +233,7 @@ export const startAnalysis = (): SchemaThunkAction<
238233
driverOptions,
239234
logger
240235
);
236+
schemaAccessorRef.current = schemaAccessor;
241237
let schema: Schema | null = null;
242238
if (schemaAccessor) {
243239
schema = await schemaAccessor.getInternalSchema();
@@ -254,7 +250,6 @@ export const startAnalysis = (): SchemaThunkAction<
254250
dispatch({
255251
type: SchemaAnalysisActions.analysisFinished,
256252
schema,
257-
schemaAccessor,
258253
});
259254

260255
// track schema analyzed
@@ -282,7 +277,7 @@ export const startAnalysis = (): SchemaThunkAction<
282277
error: err as Error,
283278
});
284279
} finally {
285-
abortControllerRef.current = undefined;
280+
analysisAbortControllerRef.current = undefined;
286281
}
287282
};
288283
};

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import type {
55
InternalSchema,
66
MongoDBJSONSchema,
77
ExpandedJSONSchema,
8+
SchemaAccessor,
89
} from 'mongodb-schema';
910

1011
import type { SchemaThunkAction } from './store';
1112
import { isAction } from '../utils';
1213
import {
1314
calculateSchemaDepth,
1415
schemaContainsGeoData,
15-
type SchemaAccessor,
1616
} from '../modules/schema-analysis';
1717
import { openToast } from '@mongodb-js/compass-components';
1818

@@ -23,7 +23,6 @@ export type SchemaFormat =
2323
| 'legacyJSON';
2424
export type ExportStatus = 'inprogress' | 'complete' | 'error';
2525
export type SchemaExportState = {
26-
abortController?: AbortController;
2726
isOpen: boolean;
2827
isLegacyBannerOpen: boolean;
2928
legacyBannerChoice?: 'legacy' | 'export';
@@ -81,11 +80,8 @@ export const closeExportSchema = (): SchemaThunkAction<
8180
void,
8281
CloseExportSchemaAction
8382
> => {
84-
return (dispatch, getState) => {
85-
const {
86-
schemaExport: { abortController },
87-
} = getState();
88-
abortController?.abort();
83+
return (dispatch, getState, { exportAbortControllerRef }) => {
84+
exportAbortControllerRef.current?.abort();
8985

9086
return dispatch({
9187
type: SchemaExportActions.closeExportSchema,
@@ -99,7 +95,6 @@ export type CancelExportSchemaAction = {
9995

10096
export type ChangeExportSchemaFormatStartedAction = {
10197
type: SchemaExportActions.changeExportSchemaFormatStarted;
102-
abortController: AbortController;
10398
exportFormat: SchemaFormat;
10499
};
105100

@@ -117,11 +112,8 @@ export const cancelExportSchema = (): SchemaThunkAction<
117112
void,
118113
CancelExportSchemaAction
119114
> => {
120-
return (dispatch, getState) => {
121-
const {
122-
schemaExport: { abortController },
123-
} = getState();
124-
abortController?.abort();
115+
return (dispatch, getState, { exportAbortControllerRef }) => {
116+
exportAbortControllerRef.current?.abort();
125117

126118
return dispatch({
127119
type: SchemaExportActions.cancelExportSchema,
@@ -169,19 +161,19 @@ export const changeExportSchemaFormat = (
169161
| ChangeExportSchemaFormatErroredAction
170162
| ChangeExportSchemaFormatCompletedAction
171163
> => {
172-
return async (dispatch, getState, { logger: { log } }) => {
173-
const {
174-
schemaExport: { abortController: existingAbortController },
175-
} = getState();
176-
164+
return async (
165+
dispatch,
166+
getState,
167+
{ logger: { log }, exportAbortControllerRef, schemaAccessorRef }
168+
) => {
177169
// If we're already in progress we abort their current operation.
178-
existingAbortController?.abort();
170+
exportAbortControllerRef.current?.abort();
179171

180-
const abortController = new AbortController();
172+
exportAbortControllerRef.current = new AbortController();
173+
const abortSignal = exportAbortControllerRef.current.signal;
181174

182175
dispatch({
183176
type: SchemaExportActions.changeExportSchemaFormatStarted,
184-
abortController,
185177
exportFormat,
186178
});
187179

@@ -196,18 +188,18 @@ export const changeExportSchemaFormat = (
196188
}
197189
);
198190

199-
const schemaAccessor = getState().schemaAnalysis.schemaAccessor;
191+
const schemaAccessor = schemaAccessorRef.current;
200192
if (!schemaAccessor) {
201193
throw new Error('No schema analysis available');
202194
}
203195

204196
exportedSchema = await getSchemaByFormat({
205197
schemaAccessor,
206198
exportFormat,
207-
signal: abortController.signal,
199+
signal: abortSignal,
208200
});
209201
} catch (err: any) {
210-
if (abortController.signal.aborted) {
202+
if (abortSignal.aborted) {
211203
return;
212204
}
213205
log.error(
@@ -226,7 +218,7 @@ export const changeExportSchemaFormat = (
226218
return;
227219
}
228220

229-
if (abortController.signal.aborted) {
221+
if (abortSignal.aborted) {
230222
return;
231223
}
232224

@@ -319,7 +311,6 @@ export const schemaExportReducer: Reducer<SchemaExportState, Action> = (
319311
) {
320312
return {
321313
...state,
322-
abortController: action.abortController,
323314
exportStatus: 'inprogress',
324315
exportFormat: action.exportFormat,
325316
};

0 commit comments

Comments
 (0)