mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Refactored SettingsCache to get events through DI
- requiring lib/common/events makes the settings cache tightly coupled to the server
- moving this up to settings index means the cache itself can be moved to a shared component/moved out of Ghost
- the index then becomes the settings manager
- questionable whether the event listeners & updater part of this shouldn't be part of a manager, independent of the actual cache 🤔
This commit is contained in:
parent
d33baf9ba4
commit
3ea6df819c
3 changed files with 10 additions and 8 deletions
|
@ -3,7 +3,6 @@
|
|||
// circular dependency bugs.
|
||||
const debug = require('@tryghost/debug')('settings:cache');
|
||||
const _ = require('lodash');
|
||||
const events = require('../../lib/common/events');
|
||||
const publicSettings = require('./public');
|
||||
|
||||
// Local function, only ever used for initialising
|
||||
|
@ -110,12 +109,13 @@ module.exports = {
|
|||
*
|
||||
* Optionally takes a collection of settings & can populate the cache with these.
|
||||
*
|
||||
* @param {EventEmitter} events
|
||||
* @param {Bookshelf.Collection<Settings>} [settingsCollection]
|
||||
* @return {object}
|
||||
*/
|
||||
init(settingsCollection) {
|
||||
init(events, settingsCollection) {
|
||||
// First, reset the cache and listeners
|
||||
this.reset();
|
||||
this.reset(events);
|
||||
|
||||
// // if we have been passed a collection of settings, use this to populate the cache
|
||||
if (settingsCollection && settingsCollection.models) {
|
||||
|
@ -132,8 +132,9 @@ module.exports = {
|
|||
|
||||
/**
|
||||
* Reset both the cache and the listeners, must be called during init
|
||||
* @param {EventEmitter} events
|
||||
*/
|
||||
reset() {
|
||||
reset(events) {
|
||||
settingsCache = {};
|
||||
|
||||
events.removeListener('settings.edited', updateSettingFromModel);
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
* Settings Lib
|
||||
* A collection of utilities for handling settings including a cache
|
||||
*/
|
||||
const events = require('../../lib/common/events');
|
||||
const models = require('../../models');
|
||||
const SettingsCache = require('./cache');
|
||||
|
||||
|
@ -27,14 +28,14 @@ module.exports = {
|
|||
*/
|
||||
async init() {
|
||||
const settingsCollection = await models.Settings.populateDefaults();
|
||||
SettingsCache.init(settingsCollection);
|
||||
SettingsCache.init(events, settingsCollection);
|
||||
},
|
||||
|
||||
/**
|
||||
* Shutdown the cache, used in force boot during testing
|
||||
*/
|
||||
shutdown() {
|
||||
SettingsCache.reset();
|
||||
SettingsCache.reset(events);
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -70,7 +70,7 @@ describe('UNIT: settings cache', function () {
|
|||
}]
|
||||
};
|
||||
|
||||
cache.init(settingsCollection);
|
||||
cache.init(events, settingsCollection);
|
||||
cache.get('key1').should.equal('init value');
|
||||
|
||||
// check handler only called once on settings.edit
|
||||
|
@ -83,7 +83,7 @@ describe('UNIT: settings cache', function () {
|
|||
cache.get('key1').should.equal('first edit');
|
||||
|
||||
// init does a reset by default
|
||||
cache.init(settingsCollection);
|
||||
cache.init(events, settingsCollection);
|
||||
setSpy.callCount.should.equal(3);
|
||||
cache.get('key1').should.equal('init value');
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue