0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/frontend/services/url/Resource.js
Vikas Potluri 4ac88dce10
Refactored common lib import to use destructuring (#11835)
* refactored `core/frontend/apps` to destructure common imports
* refactored `core/frontend/services/{apps, redirects, routing}` to destructure common imports
* refactored `core/frontend/services/settings` to destructure common imports
* refactored remaining `core/frontend/services` to destructure common imports
* refactored `core/server/adapters` to destructure common imports
* refactored `core/server/data/{db, exporter, schema, validation}` to destructure common imports
* refactored `core/server/data/importer` to destructure common imports
* refactored `core/server/models/{base, plugins, relations}` to destructure common imports
* refactored remaining `core/server/models` to destructure common imports
* refactored `core/server/api/canary/utils/serializers/output` to destructure common imports
* refactored remaining `core/server/api/canary/utils` to destructure common imports
* refactored remaining `core/server/api/canary` to destructure common imports
* refactored `core/server/api/shared` to destructure common imports
* refactored `core/server/api/v2/utils` to destructure common imports
* refactored remaining `core/server/api/v2` to destructure common imports
* refactored `core/frontend/meta` to destructure common imports
* fixed some tests referencing `common.errors` instead of `@tryghost/errors`
   - Not all of them need to be updated; only updating the ones that are
causing failures
* fixed errors import being shadowed by local scope
2020-05-22 19:22:20 +01:00

99 lines
2.4 KiB
JavaScript

const EventEmitter = require('events').EventEmitter;
const {logging} = require('../../../server/lib/common');
const errors = require('@tryghost/errors');
/**
* Resource cache.
*/
class Resource extends EventEmitter {
constructor(type, obj) {
super();
this.data = {};
this.config = {
type: type,
reserved: false
};
Object.assign(this.data, obj);
}
/**
* @description Get the type of the resource e.g. posts, users ...
* @returns {String} type
*/
getType() {
return this.config.type;
}
/**
* @description Reserve a resource.
*
* This happens if a url generator's conditions matches a resource.
* We have to reserve resources, because otherwise resources can appear in multiple url structures.
*/
reserve() {
if (!this.config.reserved) {
this.config.reserved = true;
} else {
logging.error(new errors.InternalServerError({
message: 'Resource is already taken. This should not happen.',
code: 'URLSERVICE_RESERVE_RESOURCE'
}));
}
}
/**
* @description Release a resource.
*
* This happens if conditions of a url generator no longer matches a resource.
* e.g. change a post to a page.
*/
release() {
this.config.reserved = false;
}
/**
* @description Check whether a resource is reserved.
* @returns {boolean}
*/
isReserved() {
return this.config.reserved === true;
}
/**
* @description Update the resource cache.
*
* Emit update to subscribers - observer pattern.
* e.g. url generator will listen on it's own resource's.
*
* @param {Object} obj - raw resource data
*/
update(obj) {
Object.assign(this.data, obj);
if (!this.isReserved()) {
return;
}
this.emit('updated', this);
}
/**
* @description Remove a resource.
*
* The fn is only useful to emit the action/event right now.
*
* CASE: url generator needs to know if one of it's resources/url should be removed.
*/
remove() {
// CASE: do not emit, if it is not reserved, because nobody will listen on events.
if (!this.isReserved()) {
return;
}
this.emit('removed', this);
}
}
module.exports = Resource;