2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01:00

Fixed settings reinit to only emit changed settings events (#12184)

closes #12038

Previously we were emitting changed events for _all_ settings which would
cause any listeners for those to be triggered, this ensures that listeners are
only triggered if the corresponding setting, _did_ in fact change.
This commit is contained in:
Kukhyeon Heo 2020-09-23 22:35:03 +09:00 committed by GitHub
parent 171908cefa
commit 635d26469f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 116 additions and 2 deletions

View file

@ -12,11 +12,19 @@ module.exports = {
},
async reinit() {
const oldSettings = SettingsCache.getAll();
SettingsCache.shutdown();
const settingsCollection = await models.Settings.populateDefaults();
SettingsCache.init(settingsCollection);
const newSettings = SettingsCache.init(settingsCollection);
for (const model of settingsCollection.models) {
model.emitChange(model.attributes.key + '.' + 'edited', {});
const key = model.attributes.key;
// The type of setting is object. That's why we need to compare the value of the `value` property.
if (newSettings[key].value !== oldSettings[key].value) {
model.emitChange(key + '.' + 'edited', {});
}
}
},

View file

@ -0,0 +1,106 @@
const sinon = require('sinon');
const should = require('should');
const rewire = require('rewire');
const settings = rewire('../../../core/server/services/settings');
describe('UNIT: server settings', function () {
afterEach(function () {
sinon.restore();
});
describe('reinit', function () {
function setupSettings(oldSettings, newSettings) {
const spy = sinon.spy();
const models = {
Settings: {
populateDefaults: sinon.stub()
}
};
models.Settings.populateDefaults.resolves({
models: [{
attributes: {
key: 'db_hash'
},
emitChange: spy
}]
});
const cache = {
getAll: sinon.stub(),
init: sinon.stub(),
shutdown: sinon.stub()
};
cache.getAll.returns(oldSettings);
cache.init.returns(newSettings);
settings.__set__({
models,
SettingsCache: cache
});
return spy;
}
it('emit is not fired when the settings value is same', async function () {
const oldSettings = {
db_hash: {
id: '5f4e32961c5a161b10dbff99',
group: 'core',
key: 'db_hash',
value: '342c470a-d4e2-4aa1-9dd3-fb5f11d82db6',
type: 'string',
flags: null,
created_at: '2020-09-01T11:37:58.000Z',
created_by: '1',
updated_at: '2020-09-01T11:37:58.000Z',
updated_by: '1'
}
};
const newSettings = oldSettings;
const spy = setupSettings(oldSettings, newSettings);
await settings.reinit();
spy.calledWith('db_hash.edited', {}).should.not.be.true();
});
it('emit is fired when the settings value is changed', async function () {
const oldSettings = {
db_hash: {
id: '5f4e32961c5a161b10dbff99',
group: 'core',
key: 'db_hash',
value: '342c470a-d4e2-4aa1-9dd3-fb5f11d82db6',
type: 'string',
flags: null,
created_at: '2020-09-01T11:37:58.000Z',
created_by: '1',
updated_at: '2020-09-01T11:37:58.000Z',
updated_by: '1'
}
};
const newSettings = {
db_hash: {
id: '5f4e32961c5a161b10dbff99',
group: 'core',
key: 'db_hash',
value: '12345', // changed
type: 'string',
flags: null,
created_at: '2020-09-01T11:37:58.000Z',
created_by: '1',
updated_at: '2020-09-01T11:37:58.000Z',
updated_by: '1'
}
};
const spy = setupSettings(oldSettings, newSettings);
await settings.reinit();
spy.calledWith('db_hash.edited', {}).should.be.true();
});
});
});