Ghost/core/server/services/url/Resource.js
kirrg001 5f5f0021db 🔥 Drop Node v4 Support
no issue

- support ends today
- see https://github.com/nodejs/Release
- removed `use strict`
2018-05-01 14:06:18 +02:00

60 lines
1.2 KiB
JavaScript

const EventEmitter = require('events').EventEmitter,
common = require('../../lib/common');
class Resource extends EventEmitter {
constructor(type, obj) {
super();
this.data = {};
this.config = {
type: type,
reserved: false
};
Object.assign(this.data, obj);
}
getType() {
return this.config.type;
}
reserve() {
if (!this.config.reserved) {
this.config.reserved = true;
} else {
common.logging.error(new common.errors.InternalServerError({
message: 'Resource is already taken. This should not happen.',
code: 'URLSERVICE_RESERVE_RESOURCE'
}));
}
}
release() {
this.config.reserved = false;
}
isReserved() {
return this.config.reserved === true;
}
update(obj) {
this.data = obj;
if (!this.isReserved()) {
return;
}
this.emit('updated', this);
}
remove() {
if (!this.isReserved()) {
return;
}
this.emit('removed', this);
}
}
module.exports = Resource;