Skip to content

Commit f6bb099

Browse files
committed
chore(schema): move non-serializable services to services
1 parent 59f3023 commit f6bb099

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';
@@ -33,7 +32,6 @@ export type SchemaAnalysisState = {
3332
analysisState: AnalysisState;
3433
errorMessage: string;
3534
schema: Schema | null;
36-
schemaAccessor: SchemaAccessor | null;
3735
resultId: string;
3836
};
3937

@@ -49,7 +47,6 @@ export type AnalysisStartedAction = {
4947

5048
export type AnalysisFinishedAction = {
5149
type: SchemaAnalysisActions.analysisFinished;
52-
schemaAccessor: SchemaAccessor | null;
5350
schema: Schema | null;
5451
};
5552

@@ -73,7 +70,6 @@ export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
7370
analysisState: ANALYSIS_STATE_ANALYZING,
7471
errorMessage: '',
7572
schema: null,
76-
schemaAccessor: null,
7773
};
7874
}
7975

@@ -89,7 +85,6 @@ export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
8985
? ANALYSIS_STATE_COMPLETE
9086
: ANALYSIS_STATE_INITIAL,
9187
schema: action.schema,
92-
schemaAccessor: action.schemaAccessor,
9388
resultId: resultId(),
9489
};
9590
}
@@ -180,7 +175,6 @@ const getInitialState = (): SchemaAnalysisState => ({
180175
analysisState: ANALYSIS_STATE_INITIAL,
181176
errorMessage: '',
182177
schema: null,
183-
schemaAccessor: null,
184178
resultId: resultId(),
185179
});
186180

@@ -222,9 +216,9 @@ export const geoLayersDeleted = (
222216
};
223217

224218
export const stopAnalysis = (): SchemaThunkAction<void> => {
225-
return (dispatch, getState, { abortControllerRef }) => {
226-
if (!abortControllerRef.current) return;
227-
abortControllerRef.current?.abort();
219+
return (dispatch, getState, { analysisAbortControllerRef }) => {
220+
if (!analysisAbortControllerRef.current) return;
221+
analysisAbortControllerRef.current?.abort();
228222
};
229223
};
230224

@@ -242,7 +236,8 @@ export const startAnalysis = (): SchemaThunkAction<
242236
dataService,
243237
logger,
244238
fieldStoreService,
245-
abortControllerRef,
239+
analysisAbortControllerRef,
240+
schemaAccessorRef,
246241
namespace,
247242
geoLayersRef,
248243
connectionInfoRef,
@@ -272,8 +267,8 @@ export const startAnalysis = (): SchemaThunkAction<
272267
maxTimeMS: capMaxTimeMSAtPreferenceLimit(preferences, query.maxTimeMS),
273268
};
274269

275-
abortControllerRef.current = new AbortController();
276-
const abortSignal = abortControllerRef.current.signal;
270+
analysisAbortControllerRef.current = new AbortController();
271+
const abortSignal = analysisAbortControllerRef.current.signal;
277272

278273
try {
279274
debug('analysis started');
@@ -289,6 +284,7 @@ export const startAnalysis = (): SchemaThunkAction<
289284
driverOptions,
290285
logger
291286
);
287+
schemaAccessorRef.current = schemaAccessor;
292288
let schema: Schema | null = null;
293289
if (schemaAccessor) {
294290
schema = await schemaAccessor.getInternalSchema();
@@ -305,7 +301,6 @@ export const startAnalysis = (): SchemaThunkAction<
305301
dispatch({
306302
type: SchemaAnalysisActions.analysisFinished,
307303
schema,
308-
schemaAccessor,
309304
});
310305

311306
// track schema analyzed
@@ -333,7 +328,7 @@ export const startAnalysis = (): SchemaThunkAction<
333328
error: err as Error,
334329
});
335330
} finally {
336-
abortControllerRef.current = undefined;
331+
analysisAbortControllerRef.current = undefined;
337332
}
338333
};
339334
};

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ 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';
12-
import type { SchemaAccessor } from '../modules/schema-analysis';
1313

1414
export type SchemaFormat =
1515
| 'standardJSON'
@@ -18,7 +18,6 @@ export type SchemaFormat =
1818
| 'legacyJSON';
1919
export type ExportStatus = 'inprogress' | 'complete' | 'error';
2020
export type SchemaExportState = {
21-
abortController?: AbortController;
2221
isOpen: boolean;
2322
exportedSchema?: string;
2423
exportFormat: SchemaFormat;
@@ -69,11 +68,8 @@ export const closeExportSchema = (): SchemaThunkAction<
6968
void,
7069
CloseExportSchemaAction
7170
> => {
72-
return (dispatch, getState) => {
73-
const {
74-
schemaExport: { abortController },
75-
} = getState();
76-
abortController?.abort();
71+
return (dispatch, getState, { exportAbortControllerRef }) => {
72+
exportAbortControllerRef.current?.abort();
7773

7874
return dispatch({
7975
type: SchemaExportActions.closeExportSchema,
@@ -87,7 +83,6 @@ export type CancelExportSchemaAction = {
8783

8884
export type ChangeExportSchemaFormatStartedAction = {
8985
type: SchemaExportActions.changeExportSchemaFormatStarted;
90-
abortController: AbortController;
9186
exportFormat: SchemaFormat;
9287
};
9388

@@ -105,11 +100,8 @@ export const cancelExportSchema = (): SchemaThunkAction<
105100
void,
106101
CancelExportSchemaAction
107102
> => {
108-
return (dispatch, getState) => {
109-
const {
110-
schemaExport: { abortController },
111-
} = getState();
112-
abortController?.abort();
103+
return (dispatch, getState, { exportAbortControllerRef }) => {
104+
exportAbortControllerRef.current?.abort();
113105

114106
return dispatch({
115107
type: SchemaExportActions.cancelExportSchema,
@@ -157,19 +149,19 @@ export const changeExportSchemaFormat = (
157149
| ChangeExportSchemaFormatErroredAction
158150
| ChangeExportSchemaFormatCompletedAction
159151
> => {
160-
return async (dispatch, getState, { logger: { log } }) => {
161-
const {
162-
schemaExport: { abortController: existingAbortController },
163-
} = getState();
164-
152+
return async (
153+
dispatch,
154+
getState,
155+
{ logger: { log }, exportAbortControllerRef, schemaAccessorRef }
156+
) => {
165157
// If we're already in progress we abort their current operation.
166-
existingAbortController?.abort();
158+
exportAbortControllerRef.current?.abort();
167159

168-
const abortController = new AbortController();
160+
exportAbortControllerRef.current = new AbortController();
161+
const abortSignal = exportAbortControllerRef.current.signal;
169162

170163
dispatch({
171164
type: SchemaExportActions.changeExportSchemaFormatStarted,
172-
abortController,
173165
exportFormat,
174166
});
175167

@@ -184,18 +176,18 @@ export const changeExportSchemaFormat = (
184176
}
185177
);
186178

187-
const schemaAccessor = getState().schemaAnalysis.schemaAccessor;
179+
const schemaAccessor = schemaAccessorRef.current;
188180
if (!schemaAccessor) {
189181
throw new Error('No schema analysis available');
190182
}
191183

192184
exportedSchema = await getSchemaByFormat({
193185
schemaAccessor,
194186
exportFormat,
195-
signal: abortController.signal,
187+
signal: abortSignal,
196188
});
197189
} catch (err: any) {
198-
if (abortController.signal.aborted) {
190+
if (abortSignal.aborted) {
199191
return;
200192
}
201193
log.error(
@@ -214,7 +206,7 @@ export const changeExportSchemaFormat = (
214206
return;
215207
}
216208

217-
if (abortController.signal.aborted) {
209+
if (abortSignal.aborted) {
218210
return;
219211
}
220212

@@ -271,7 +263,6 @@ export const schemaExportReducer: Reducer<SchemaExportState, Action> = (
271263
) {
272264
return {
273265
...state,
274-
abortController: action.abortController,
275266
exportStatus: 'inprogress',
276267
exportFormat: action.exportFormat,
277268
};

0 commit comments

Comments
 (0)