Skip to content

Commit 45c0afe

Browse files
authored
chore(home, app-stores): remove unused data-service-* and instance-* globalAppRegistry events (#5495)
1 parent 86fd84a commit 45c0afe

File tree

6 files changed

+218
-242
lines changed

6 files changed

+218
-242
lines changed

package-lock.json

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-app-stores/src/stores/instance-store.spec.ts

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { EventEmitter } from 'events';
22
import AppRegistry, { createActivateHelpers } from 'hadron-app-registry';
33
import { createInstanceStore } from './instance-store';
4-
import { once } from 'events';
54
import sinon from 'sinon';
65
import { expect } from 'chai';
76
import { createNoopLoggerAndTelemetry } from '@mongodb-js/compass-logging/provider';
@@ -47,21 +46,28 @@ describe('InstanceStore [Store]', function () {
4746
let store: ReturnType<typeof createInstanceStore>;
4847
let instance: MongoDBInstance;
4948

50-
let emitSpy: any;
5149
let initialInstanceRefreshedPromise: Promise<unknown>;
5250
let sandbox: sinon.SinonSandbox;
5351

52+
function waitForInstanceRefresh(): Promise<void> {
53+
return new Promise((resolve) => {
54+
if (instance.refreshingStatus === 'ready') {
55+
resolve();
56+
}
57+
instance.on('change:refreshingStatus', () => {
58+
if (instance.refreshingStatus === 'ready') {
59+
resolve();
60+
}
61+
});
62+
});
63+
}
64+
5465
beforeEach(function () {
5566
globalAppRegistry = new AppRegistry();
5667
sandbox = sinon.createSandbox();
5768

58-
emitSpy = sandbox.spy(globalAppRegistry, 'emit');
5969
dataService = createDataService();
6070
const logger = createNoopLoggerAndTelemetry();
61-
initialInstanceRefreshedPromise = once(
62-
globalAppRegistry,
63-
'instance-refreshed'
64-
);
6571

6672
store = createInstanceStore(
6773
{
@@ -72,22 +78,15 @@ describe('InstanceStore [Store]', function () {
7278
createActivateHelpers()
7379
);
7480
instance = store.state.instance;
81+
82+
initialInstanceRefreshedPromise = waitForInstanceRefresh();
7583
});
7684

7785
afterEach(function () {
78-
emitSpy = null;
7986
sandbox.restore();
8087
store.deactivate();
8188
});
8289

83-
context('when data service connects', function () {
84-
it('emits instance-refreshed event', async function () {
85-
await initialInstanceRefreshedPromise;
86-
const events = emitSpy.args.map(([evtName]: any) => evtName);
87-
expect(events).to.eql(['instance-created', 'instance-refreshed']);
88-
});
89-
});
90-
9190
context('on refresh data', function () {
9291
beforeEach(async function () {
9392
sandbox
@@ -99,22 +98,12 @@ describe('InstanceStore [Store]', function () {
9998
'1.2.3'
10099
);
101100
globalAppRegistry.emit('refresh-data');
102-
await once(globalAppRegistry, 'instance-refreshed');
101+
await waitForInstanceRefresh();
103102
});
104103

105104
it('calls instance model fetch', function () {
106105
expect(instance).to.have.nested.property('build.version', '3.2.1');
107106
});
108-
109-
it('emits instance-changed event', function () {
110-
const events = emitSpy.args.map(([evtName]: any) => evtName);
111-
expect(events).to.eql([
112-
'instance-created',
113-
'instance-refreshed',
114-
'refresh-data',
115-
'instance-refreshed',
116-
]);
117-
});
118107
});
119108

120109
context('when instance ready', function () {

packages/compass-app-stores/src/stores/instance-store.ts

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ function getTopologyDescription(
3737

3838
export function createInstanceStore(
3939
{
40-
globalAppRegistry: appRegistry,
40+
globalAppRegistry,
4141
dataService,
42-
logger: { debug },
42+
logger: { log, mongoLogId },
4343
}: {
4444
dataService: DataService;
4545
logger: LoggerAndTelemetry;
@@ -57,19 +57,17 @@ export function createInstanceStore(
5757

5858
try {
5959
await instance.refresh({ dataService, ...refreshOptions });
60-
61-
appRegistry.emit('instance-refreshed', {
62-
instance,
63-
dataService,
64-
errorMessage: '',
65-
});
6660
} catch (err: any) {
67-
appRegistry.emit('instance-refreshed', {
68-
instance,
69-
dataService,
70-
errorMessage: err.message,
71-
});
72-
61+
log.warn(
62+
mongoLogId(1_001_000_295),
63+
'Instance Store',
64+
'Failed to refresh instance',
65+
{
66+
message: (err as Error).message,
67+
connectionId: dataService.id,
68+
isFirstRun,
69+
}
70+
);
7371
// The `instance.refresh` method is catching all expected errors: we treat
7472
// a lot of metadata as optional so failing to fetch it shouldn't throw.
7573
// In most cases if this failed on subsequent runs, user is probably
@@ -92,16 +90,28 @@ export function createInstanceStore(
9290
}
9391
}
9492

95-
// Event emitted when the Databases grid needs to be refreshed
96-
// We additionally refresh the list of collections as well
97-
// since there is the side navigation which could be in expanded mode
93+
// Event emitted when the Databases grid needs to be refreshed. We
94+
// additionally refresh the list of collections as well since there is the
95+
// side navigation which could be in expanded mode
9896
async function refreshDatabases() {
99-
await instance.fetchDatabases({ dataService, force: true });
100-
await Promise.allSettled(
101-
instance.databases.map((db) =>
102-
db.fetchCollections({ dataService, force: true })
103-
)
104-
);
97+
try {
98+
await instance.fetchDatabases({ dataService, force: true });
99+
await Promise.allSettled(
100+
instance.databases.map((db) =>
101+
db.fetchCollections({ dataService, force: true })
102+
)
103+
);
104+
} catch (err: any) {
105+
log.warn(
106+
mongoLogId(1_001_000_296),
107+
'Instance Store',
108+
'Failed to refresh databases',
109+
{
110+
message: (err as Error).message,
111+
connectionId: dataService.id,
112+
}
113+
);
114+
}
105115
}
106116

107117
async function fetchAllCollections() {
@@ -174,12 +184,8 @@ export function createInstanceStore(
174184

175185
addCleanup(() => {
176186
instance.removeAllListeners();
177-
appRegistry.emit('instance-destroyed', { instance: null });
178187
});
179188

180-
debug('instance-created');
181-
appRegistry.emit('instance-created', { instance });
182-
183189
void refreshInstance({
184190
fetchDatabases: true,
185191
fetchDbStats: true,
@@ -197,22 +203,22 @@ export function createInstanceStore(
197203

198204
on(dataService, 'topologyDescriptionChanged', onTopologyDescriptionChanged);
199205

200-
on(appRegistry, 'sidebar-expand-database', (dbName: string) => {
206+
on(globalAppRegistry, 'sidebar-expand-database', (dbName: string) => {
201207
void instance.databases.get(dbName)?.fetchCollections({ dataService });
202208
});
203209

204-
on(appRegistry, 'sidebar-filter-navigation-list', fetchAllCollections);
210+
on(globalAppRegistry, 'sidebar-filter-navigation-list', fetchAllCollections);
205211

206-
on(appRegistry, 'refresh-data', refreshInstance);
212+
on(globalAppRegistry, 'refresh-data', refreshInstance);
207213

208-
on(appRegistry, 'database-dropped', (dbName: string) => {
214+
on(globalAppRegistry, 'database-dropped', (dbName: string) => {
209215
const db = instance.databases.remove(dbName);
210216
if (db) {
211217
MongoDBInstance.removeAllListeners(db);
212218
}
213219
});
214220

215-
on(appRegistry, 'collection-dropped', (namespace: string) => {
221+
on(globalAppRegistry, 'collection-dropped', (namespace: string) => {
216222
const { database } = toNS(namespace);
217223
const db = instance.databases.get(database);
218224
const coll = db?.collections.get(namespace, '_id');
@@ -236,10 +242,10 @@ export function createInstanceStore(
236242
}
237243
});
238244

239-
on(appRegistry, 'refresh-databases', refreshDatabases);
245+
on(globalAppRegistry, 'refresh-databases', refreshDatabases);
240246

241247
on(
242-
appRegistry,
248+
globalAppRegistry,
243249
'collection-renamed',
244250
({ from, to }: { from: string; to: string }) => {
245251
const { database, collection } = toNS(from);
@@ -250,16 +256,20 @@ export function createInstanceStore(
250256
}
251257
);
252258

253-
on(appRegistry, 'document-deleted', refreshNamespaceStats);
254-
on(appRegistry, 'document-inserted', refreshNamespaceStats);
255-
on(appRegistry, 'import-finished', refreshNamespaceStats);
259+
on(globalAppRegistry, 'document-deleted', refreshNamespaceStats);
260+
on(globalAppRegistry, 'document-inserted', refreshNamespaceStats);
261+
on(globalAppRegistry, 'import-finished', refreshNamespaceStats);
256262

257-
on(appRegistry, 'collection-created', maybeAddAndRefreshCollectionModel);
263+
on(
264+
globalAppRegistry,
265+
'collection-created',
266+
maybeAddAndRefreshCollectionModel
267+
);
258268

259-
on(appRegistry, 'view-created', maybeAddAndRefreshCollectionModel);
269+
on(globalAppRegistry, 'view-created', maybeAddAndRefreshCollectionModel);
260270

261271
on(
262-
appRegistry,
272+
globalAppRegistry,
263273
'agg-pipeline-out-executed',
264274
// null means the out / merge stage destination wasn't a namespace in the
265275
// same cluster
@@ -271,7 +281,7 @@ export function createInstanceStore(
271281
}
272282
);
273283

274-
on(appRegistry, 'view-edited', (namespace: string) => {
284+
on(globalAppRegistry, 'view-edited', (namespace: string) => {
275285
const { database } = toNS(namespace);
276286
void instance.databases
277287
.get(database)

packages/compass-home/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"@mongodb-js/tsconfig-compass": "^1.0.3",
109109
"@mongodb-js/webpack-config-compass": "^1.3.3",
110110
"@testing-library/react": "^12.1.4",
111+
"@testing-library/user-event": "^13.5.0",
111112
"@types/chai": "^4.2.21",
112113
"chai": "^4.1.2",
113114
"depcheck": "^1.4.1",

0 commit comments

Comments
 (0)