Skip to content

Commit 33a8a56

Browse files
committed
expose only shouldFetchDbAndCollStats from instanceModel
1 parent e94e840 commit 33a8a56

File tree

5 files changed

+60
-19
lines changed

5 files changed

+60
-19
lines changed

packages/collection-model/lib/model.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,16 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
254254
return;
255255
}
256256

257-
const instanceModel = getParentByType(this, 'Instance');
258-
const { enableDbAndCollStats } = instanceModel.preferences.getPreferences();
257+
const shouldFetchDbAndCollStats = getParentByType(
258+
this,
259+
'Instance'
260+
).shouldFetchDbAndCollStats;
259261

260262
try {
261263
const newStatus = this.status === 'initial' ? 'fetching' : 'refreshing';
262264
this.set({ status: newStatus });
263265
const [collStats, collectionInfo] = await Promise.all([
264-
enableDbAndCollStats
266+
shouldFetchDbAndCollStats
265267
? dataService.collectionStats(this.database, this.name)
266268
: null,
267269
fetchInfo ? dataService.collectionInfo(this.database, this.name) : null,

packages/database-model/lib/model.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,16 @@ const DatabaseModel = AmpersandModel.extend(
139139
* @returns {Promise<void>}
140140
*/
141141
async fetch({ dataService, force = false }) {
142-
const instanceModel = getParentByType(this, 'Instance');
143-
const { enableDbAndCollStats } =
144-
instanceModel.preferences.getPreferences();
142+
const shouldFetchDbAndCollStats = getParentByType(
143+
this,
144+
'Instance'
145+
).shouldFetchDbAndCollStats;
145146

146147
if (!shouldFetch(this.status, force)) {
147148
return;
148149
}
149150

150-
if (!enableDbAndCollStats) {
151+
if (!shouldFetchDbAndCollStats) {
151152
this.set({ status: 'ready' });
152153
return;
153154
}

packages/databases-collections-list/src/index.spec.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,15 @@ describe('databases and collections list', function () {
266266
});
267267

268268
expect(screen.queryByText(/Storage size/)).not.to.exist;
269-
// expect(screen.queryByText('1.50 kB')).not.to.exist;
270-
// expect(screen.queryByText(/Documents/)).not.to.exist;
271-
// expect(screen.queryByText('10')).not.to.exist;
272-
// expect(screen.queryByText(/Avg. document size/)).not.to.exist;
273-
// expect(screen.queryByText('150.00 B')).not.to.exist;
274-
// expect(screen.queryByText(/Indexes/)).not.to.exist;
275-
// expect(screen.queryByText('15')).not.to.exist;
276-
// expect(screen.queryByText(/Total index size/)).not.to.exist;
277-
// expect(screen.queryByText('16.00 B')).not.to.exist;
269+
expect(screen.queryByText('1.50 kB')).not.to.exist;
270+
expect(screen.queryByText(/Documents/)).not.to.exist;
271+
expect(screen.queryByText('10')).not.to.exist;
272+
expect(screen.queryByText(/Avg. document size/)).not.to.exist;
273+
expect(screen.queryByText('150.00 B')).not.to.exist;
274+
expect(screen.queryByText(/Indexes/)).not.to.exist;
275+
expect(screen.queryByText('15')).not.to.exist;
276+
expect(screen.queryByText(/Total index size/)).not.to.exist;
277+
expect(screen.queryByText('16.00 B')).not.to.exist;
278278
});
279279
});
280280
});

packages/instance-model/lib/model.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,23 @@ const InstanceModel = AmpersandModel.extend(
135135
isSearchIndexesSupported: 'boolean',
136136
atlasVersion: { type: 'string', default: '' },
137137
csfleMode: { type: 'string', default: 'unavailable' },
138+
shouldFetchDbAndCollStats: { type: 'boolean', default: false },
138139
},
139140
initialize: function ({ preferences, ...props }) {
140-
this.preferences = preferences;
141+
// Initialize the property directly from preferences
142+
this.set({
143+
shouldFetchDbAndCollStats:
144+
preferences.getPreferences().enableDbAndCollStats,
145+
});
146+
147+
// Listen to preference changes using the preferences API
148+
this._preferenceUnsubscribe = preferences.onPreferenceValueChanged(
149+
'enableDbAndCollStats',
150+
(value) => {
151+
this.set({ shouldFetchDbAndCollStats: value });
152+
}
153+
);
154+
141155
AmpersandModel.prototype.initialize.call(this, props);
142156
},
143157
derived: {
@@ -387,6 +401,10 @@ const InstanceModel = AmpersandModel.extend(
387401
},
388402

389403
removeAllListeners() {
404+
// Clean up preference listeners
405+
if (this._preferenceUnsubscribe) {
406+
this._preferenceUnsubscribe();
407+
}
390408
InstanceModel.removeAllListeners(this);
391409
},
392410

packages/instance-model/test/index.test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
'use strict';
22
const { expect } = require('chai');
33
const { MongoDBInstance } = require('../');
4+
const {
5+
createSandboxFromDefaultPreferences,
6+
} = require('compass-preferences-model');
47

58
describe('mongodb-instance-model', function () {
9+
let preferences;
10+
11+
beforeEach(async function () {
12+
preferences = await createSandboxFromDefaultPreferences();
13+
});
14+
615
it('should be in initial state when created', function () {
7-
const instance = new MongoDBInstance({ _id: 'abc' });
16+
const instance = new MongoDBInstance({ _id: 'abc', preferences });
817
expect(instance).to.have.property('status', 'initial');
918
expect(instance.build.toJSON()).to.be.an('object').that.is.empty;
1019
expect(instance.host.toJSON()).to.be.an('object').that.is.empty;
1120
});
1221

22+
it('should answer shouldFetchDbAndCollStats based on preferences', async function () {
23+
const instance = new MongoDBInstance({ _id: 'abc', preferences });
24+
25+
await preferences.savePreferences({ enableDbAndCollStats: true });
26+
expect(instance.shouldFetchDbAndCollStats).to.equal(true);
27+
28+
await preferences.savePreferences({ enableDbAndCollStats: false });
29+
expect(instance.shouldFetchDbAndCollStats).to.equal(false);
30+
});
31+
1332
context('with mocked dataService', function () {
1433
const dataService = {
1534
instance() {
@@ -24,7 +43,7 @@ describe('mongodb-instance-model', function () {
2443
};
2544

2645
it('should fetch and populate instance info when fetch called', async function () {
27-
const instance = new MongoDBInstance({ _id: 'abc' });
46+
const instance = new MongoDBInstance({ _id: 'abc', preferences });
2847

2948
await instance.fetch({ dataService });
3049

@@ -61,6 +80,7 @@ describe('mongodb-instance-model', function () {
6180
_id: 'foo',
6281
hostname: 'foo.com',
6382
port: 1234,
83+
preferences,
6484
topologyDescription: getTopologyDescription(),
6585
});
6686

0 commit comments

Comments
 (0)