@p2olab/pimad-core
Version:
PiMAd (Process-industry-Modular-Automation-description) High level access to automation of modular plants.
243 lines (242 loc) • 8.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Backbone = void 0;
const Utils_1 = require("../Utils");
class AResponse {
constructor() {
this.initialized = false;
this.message = '';
this.content = {};
}
initialize(message, content) {
if (!this.initialized) {
this.initialized = true;
this.message = message;
this.content = content;
return (this.message == message && JSON.stringify(this.content) == JSON.stringify(content));
}
else {
return false;
}
}
getMessage() {
return this.message;
}
getContent() {
return this.content;
}
}
class AResponseFactory {
}
class ErrorResponse extends AResponse {
}
class DummyResponse extends AResponse {
}
class SuccessResponse extends AResponse {
}
class WarningResponse extends AResponse {
}
/**
* This factory spawns an {@link ErrorResponse}-Objects.
*/
class ErrorResponseFactory extends AResponseFactory {
create() {
const response = new ErrorResponse();
Utils_1.logger.debug(this.constructor.name + ' creates a ' + response.constructor.name);
return response;
}
}
/**
* This factory spawns a {@link DummyResponse}-Objects.
*/
class DummyResponseFactory extends AResponseFactory {
create() {
const response = new DummyResponse();
Utils_1.logger.debug(this.constructor.name + ' creates a ' + response.constructor.name);
return response;
}
}
/**
* This factory spawns a {@link SuccessResponse}-Objects.
*/
class SuccessResponseFactory extends AResponseFactory {
create() {
const response = new SuccessResponse();
Utils_1.logger.debug(this.constructor.name + ' creates a ' + response.constructor.name);
return response;
}
}
/**
* This factory spawns {@link WarningResponse}-Objects.
*/
class WarningResponseFactory extends AResponseFactory {
create() {
const response = new WarningResponse();
Utils_1.logger.debug(this.constructor.name + ' creates a ' + response.constructor.name);
return response;
}
}
/**
* The namespace Backbone provides the classes in PiMAd-core with various elementary functionalities. The
* {@link PiMAdResponse} interface serves as an exchange format between individual classes or even between PiMAd and
* docking software.
*
<uml>
abstract class AResponse {
#content: object
#msg: string
-initialized: boolean = false
}
abstract class AResponseFactory
class PiMAdResponseVendor {
-dummyResponseFactory: PiMAdResponseFactory
-errorResponseFactory: PiMAdResponseFactory
-successResponseFactory: PiMAdResponseFactory
-warningResponseFactory: PiMAdResponseFactory
+buyDummyResponse(): PiMAdResponse
+buyErrorResponse(): PiMAdResponse
+buySuccessResponse(): PiMAdResponse
+buyWarningResponse(): PiMAdResponse
}
class PiMAdResponseHandler {
-responseVendor: ResponseVendor
+handleCallbackWithResponse(type: ResponseTypes, message: string, content: object, callback: (response: PiMAdResponse) => void): void
}
enum PiMAdResponseTypes {
DUMMY
ERROR
SUCCESS
WARNING
}
interface PiMAdResponse {
+getMessage(): string
+getContent(): object
+initialize(message: string, content: object): boolean
}
interface PiMAdResponseFactory {
+create(): PiMAdResponse
}
PiMAdResponse <|.. AResponse
PiMAdResponse <-- PiMAdResponseFactory
PiMAdResponse <-- PiMAdResponseVendor
PiMAdResponseFactory <|.. AResponseFactory
AResponse <|-- DummyResponse
AResponse <|-- ErrorResponse
AResponse <|-- SuccessResponse
AResponse <|-- WarningResponse
AResponseFactory <|-- DummyResponseFactory
AResponseFactory <|-- ErrorResponseFactory
AResponseFactory <|-- SuccessResponseFactory
AResponseFactory <|-- WarningResponseFactory
PiMAdResponseHandler "1" o-- "1" PiMAdResponseVendor
PiMAdResponseVendor "1" o-- "1..*" PiMAdResponseFactory
</uml>
*/
var Backbone;
(function (Backbone) {
/**
* This vendor sells various {@link PiMAdResponse}-Instances.
*/
class PiMAdResponseVendor {
constructor() {
this.errorResponseFactory = new ErrorResponseFactory();
this.dummyResponseFactory = new DummyResponseFactory();
this.successResponseFactory = new SuccessResponseFactory();
this.warningResponseFactory = new WarningResponseFactory();
}
/**
* Buy a {@link DummyResponse} as {@link PiMAdResponse}.
*/
buyDummyResponse() {
return this.dummyResponseFactory.create();
}
/**
* Buy an {@link ErrorResponse} as {@link PiMAdResponse}.
*/
buyErrorResponse() {
return this.errorResponseFactory.create();
}
/**
* Buy a {@link SuccessResponse} as {@link PiMAdResponse}.
*/
buySuccessResponse() {
return this.successResponseFactory.create();
}
/**
* Buy a {@link WarningResponse} as {@link PiMAdResponse}.
*/
buyWarningResponse() {
return this.warningResponseFactory.create();
}
}
Backbone.PiMAdResponseVendor = PiMAdResponseVendor;
/**
* This enum referencing to all implementations of {@link PiMAdResponse}.
*/
let PiMAdResponseTypes;
(function (PiMAdResponseTypes) {
/**
* Referencing a {@link DummyResponse}.
*/
PiMAdResponseTypes[PiMAdResponseTypes["DUMMY"] = 0] = "DUMMY";
/**
* Referencing an {@link ErrorResponse}.
*/
PiMAdResponseTypes[PiMAdResponseTypes["ERROR"] = 1] = "ERROR";
/**
* Referencing a {@link SuccessResponse}.
*/
PiMAdResponseTypes[PiMAdResponseTypes["SUCCESS"] = 2] = "SUCCESS";
/**
* Referencing a {@link WarningResponse}.
*/
PiMAdResponseTypes[PiMAdResponseTypes["WARNING"] = 3] = "WARNING";
})(PiMAdResponseTypes = Backbone.PiMAdResponseTypes || (Backbone.PiMAdResponseTypes = {}));
/**
* This class generalizes the handling of objects of the response interface and thus reduces the repeated
* occurrence of certain code fragments.
*/
class PiMAdResponseHandler {
constructor() {
this.responseVendor = new PiMAdResponseVendor();
}
/**
* This method creates the desired {@link PiMAdResponse}-Instance, initializes it with the given data and finally calls the
* callback function.
* @param type - The type of the response.
* @param message - The message of the response.
* @param content - The content of the response.
* @param callback - The callback function that expects a {@link PiMAdResponse}-Instance as input.
*/
handleCallbackWithResponse(type, message, content, callback) {
callback(this.handleResponse(type, message, content));
}
/**
* This method creates the desired {@link PiMAdResponse}-Instance, initializes it with the given data and
* returns the instance.
* @param type - The type of the response.
* @param message - The message of the response.
* @param content - The content of the response.
*/
handleResponse(type, message, content) {
let response;
switch (type) {
case Backbone.PiMAdResponseTypes.DUMMY:
response = this.responseVendor.buyDummyResponse();
break;
case PiMAdResponseTypes.ERROR:
response = this.responseVendor.buyErrorResponse();
break;
case PiMAdResponseTypes.SUCCESS:
response = this.responseVendor.buySuccessResponse();
break;
case PiMAdResponseTypes.WARNING:
response = this.responseVendor.buyWarningResponse();
break;
}
response.initialize(message, content);
return response;
}
}
Backbone.PiMAdResponseHandler = PiMAdResponseHandler;
})(Backbone = exports.Backbone || (exports.Backbone = {}));