From 376e2f4cf337bfcc9c4d0ce5860de8ade17a3947 Mon Sep 17 00:00:00 2001 From: Naz Date: Fri, 12 Nov 2021 11:56:05 +0400 Subject: [PATCH] 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 --- core/server/services/url/Resources.js | 32 +++++++++++++++++++++----- core/server/services/url/UrlService.js | 1 + 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/core/server/services/url/Resources.js b/core/server/services/url/Resources.js index 8f11dc3edd..1ad172adbf 100644 --- a/core/server/services/url/Resources.js +++ b/core/server/services/url/Resources.js @@ -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); } /** diff --git a/core/server/services/url/UrlService.js b/core/server/services/url/UrlService.js index 18fd271f24..cb6f0a5632 100644 --- a/core/server/services/url/UrlService.js +++ b/core/server/services/url/UrlService.js @@ -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',