mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 05:37:34 +03:00
Separated event listeners from resource fetching
refs https://github.com/TryGhost/Toolbox/issues/125 - The "fetchResources" method did way to many things extracted event listener setup logic to a separate method - This allows to call out these stages as needed if we retreive resources separately from a cache of some sort and don't need to wait for the database response
This commit is contained in:
parent
46c3b53979
commit
376e2f4cf3
@ -55,13 +55,12 @@ class Resources {
|
||||
}
|
||||
|
||||
const bridge = require('../../../bridge');
|
||||
this.resourcesAPIVersion = bridge.getFrontendApiVersion();
|
||||
this.resourcesConfig = require(`./configs/${this.resourcesAPIVersion}`);
|
||||
const resourcesAPIVersion = bridge.getFrontendApiVersion();
|
||||
this.resourcesConfig = require(`./configs/${resourcesAPIVersion}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Helper function to initialise data fetching. Each resource type needs to register resource/model
|
||||
* events to get notified about updates/deletions/inserts.
|
||||
* @description Helper function to initialize data fetching.
|
||||
*/
|
||||
fetchResources() {
|
||||
const ops = [];
|
||||
@ -75,6 +74,29 @@ class Resources {
|
||||
|
||||
// NOTE: We are querying knex directly, because the Bookshelf ORM overhead is too slow.
|
||||
ops.push(this._fetch(resourceConfig));
|
||||
});
|
||||
|
||||
return Promise.all(ops);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Each resource type needs to register resource/model events to get notified
|
||||
* about updates/deletions/inserts.
|
||||
*
|
||||
* For example for a "tag" resource type with following configuration:
|
||||
* events: {
|
||||
* add: 'tag.added',
|
||||
* update: ['tag.edited', 'tag.attached', 'tag.detached'],
|
||||
* remove: 'tag.deleted'
|
||||
* }
|
||||
* there would be:
|
||||
* 1 event listener connected to "_onResourceAdded" handler and it's 'tag.added' event
|
||||
* 3 event listeners connected to "_onResourceUpdated" handler and it's 'tag.edited', 'tag.attached', 'tag.detached' events
|
||||
* 1 event listener connected to "_onResourceRemoved" handler and it's 'tag.deleted' event
|
||||
*/
|
||||
initEvenListeners() {
|
||||
_.each(this.resourcesConfig, (resourceConfig) => {
|
||||
this.data[resourceConfig.type] = [];
|
||||
|
||||
this._listenOn(resourceConfig.events.add, (model) => {
|
||||
return this._onResourceAdded.bind(this)(resourceConfig.type, model);
|
||||
@ -96,8 +118,6 @@ class Resources {
|
||||
return this._onResourceRemoved.bind(this)(resourceConfig.type, model);
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.all(ops);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -288,6 +288,7 @@ class UrlService {
|
||||
*/
|
||||
init() {
|
||||
this.resources.fetchResources();
|
||||
this.resources.initEvenListeners();
|
||||
// CASE: all resources are fetched, start the queue
|
||||
this.queue.start({
|
||||
event: 'init',
|
||||
|
Loading…
Reference in New Issue
Block a user