UNPKG

webgme-engine

Version:

WebGME server and Client API without a GUI

313 lines (259 loc) 12.3 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: addon/AddOnBase.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: addon/AddOnBase.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/*globals define*/ /*eslint-env node*/ /** * This is the base class that add-ons should inherit from. * (Using the AddOnGenerator - the generated add-on will do that.) * @author pmeijer / https://github.com/pmeijer */ define([ 'addon/AddOnUpdateResult', 'common/storage/constants' ], function (AddOnUpdateResult, STORAGE_CONSTANTS) { 'use strict'; /** * BaseClass for AddOns which run on the server and act upon changes in a branch. * Use the AddOnGenerator to generate a new AddOn that implements this class. * @param {GmeLogger} logger * @param {GmeConfig} gmeConfig * @constructor * @alias AddOnBase */ function AddOnBase(logger, gmeConfig) { /** * @type {GmeConfig} */ this.gmeConfig = gmeConfig; /** * @type {GmeLogger} */ this.logger = logger; // Set at configure /** * @type {Core} */ this.core = null; /** * @type {Project} */ this.project = null; /** * @type {string} */ this.branchName = null; /** * @type {BlobClient} */ this.blobClient = null; this.initialized = false; /** * @type {AddOnUpdateResult} */ this.updateResult = null; this.currentlyWorking = false; this.lifespan = 0; this.logger.debug('ctor'); } /** * Configures the AddOn * @param {object} configuration * @param {function} callback */ AddOnBase.prototype.configure = function (configuration) { this.core = configuration.core; this.project = configuration.project; this.branchName = configuration.branchName; this.blobClient = configuration.blobClient; this.projectId = this.project.projectId; }; /** * Readable name of this AddOn that can contain spaces. * @returns {string} */ AddOnBase.prototype.getName = function () { throw new Error('implement this function in the derived class - getting type automatically is a bad idea,' + 'when the js scripts are minified names are useless.'); }; /** * Current version of this AddOn using semantic versioning. * @returns {string} */ AddOnBase.prototype.getVersion = function () { return '0.1.0'; }; /** * A detailed description of this AddOn and its purpose. It can be one or more sentences. * @returns {string} */ AddOnBase.prototype.getDescription = function () { return ''; }; /** * This is invoked after each commit to the branch. AddOns are allowed to make changes on updates, * but should not persist by themselves. The manager/monitor will persist after each AddOn has had its * way (ordered by the "usedAddOn" registry in the rootNode). * * Changes made by AddOns do not trigger a new update for other addOns. * @param {module:Core~Node} rootNode * @param {module:Storage~CommitObject} commitObj * @param {function(Error, AddOnUpdateResult)} callback */ AddOnBase.prototype.update = function (rootNode, commitObj, callback) { callback(new Error('The function is the main function of the addOn so it must be overwritten.')); }; /** * Called once when the AddOn is started for the first time. * @param {object} rootNode * @param {object} commitObj * @param {function(Error, AddOnUpdateResult)} callback */ AddOnBase.prototype.initialize = function (rootNode, commitObj, callback) { callback(new Error('The function is the main function of the addOn so it must be overwritten.')); }; /** * Called by the manager/monitor after each commit to the branch. * @param {object} rootNode * @param {object} commitObj * @param {function(Error, AddOnUpdateResult)} callback * @private */ AddOnBase.prototype._update = function (rootNode, commitObj, callback) { var self = this; this.updateResult = new AddOnUpdateResult(commitObj); this.currentlyWorking = true; this.lifespan += 1; this.update(rootNode, commitObj, function (err, result) { self.currentlyWorking = false; callback(err, result); }); }; /** * Called once by the manager/monitor when the AddOn is first started. * @param {object} rootNode * @param {object} commitObj * @param {function(Error, AddOnUpdateResult)} callback * @private */ AddOnBase.prototype._initialize = function (rootNode, commitObj, callback) { var self = this; this.initialized = true; this.updateResult = new AddOnUpdateResult(commitObj); this.currentlyWorking = true; this.initialize(rootNode, commitObj, function (err, result) { self.currentlyWorking = false; callback(err, result); }); }; /** * Creats or appends commit message for the current update-cycle. * @param {string} msg */ AddOnBase.prototype.addCommitMessage = function (msg) { this.updateResult.addCommitMessage(this, msg); }; /** * Adds a notification to all sockets connected to the branch room. The notification will be sent after * the update-callback has been invoked. * @param {string|object} message - Message string or object containing message. * @param {string} message.message - If object it must contain a message. * @param {string} [message.severity='info'] - Severity level ('success', 'info', 'warn', 'error') */ AddOnBase.prototype.addNotification = function (message) { var self = this, data = { type: STORAGE_CONSTANTS.ADD_ON_NOTIFICATION, notification: typeof message === 'string' ? {message: message} : message, projectId: self.projectId, branchName: self.branchName, commitHash: self.updateResult.commitObj._id, addOnName: self.getName(), addOnVersion: self.getVersion() }; self.updateResult.addNotification(self, data); }; // TODO: Query related /** * Structure of query parameters with names, descriptions, minimum, maximum values, default values and * type definitions. * @returns {object[]} */ AddOnBase.prototype.getQueryParamsStructure = function () { return []; }; /** * Queries are typically invoked by users from a client. The AddOn is not suppose to make any changes to * either the model's or the AddOn's state. (Since users can share a running instance of an AddOn). * @param {string} commitHash - State of the invoker. * @param {object} queryParams - Values based on the 'getQueryParametersStructure'. * @param {function} callback - resolves with PluginResult. */ AddOnBase.prototype.query = function (commitHash, queryParams, callback) { callback(new Error('The function is the main function of the addOn so it must be overwritten.')); }; AddOnBase.prototype._getStatus = function () { var status = this.getStatus(); status.currentlyWorking = this.currentlyWorking; status.lifespan = this.lifespan; return status; }; AddOnBase.prototype.getStatus = function () { return {}; }; /** * Overwrite this if you are sending requests to the webgme server. * Make sure to still update the blob-clients token. * * @param token */ AddOnBase.prototype.setToken = function (token) { if (this.blobClient) { this.blobClient.setToken(token); } }; /** * Returns the default values of the Query Parameters. * * @returns {object} */ AddOnBase.prototype.getDefaultQueryParams = function () { var paramStructure = this.getQueryParametersStructure(), defaultParams = {}, i; for (i = 0; i &lt; paramStructure.length; i += 1) { defaultParams[paramStructure[i].name] = paramStructure[i].value; } return defaultParams; }; return AddOnBase; });</code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="Server_GMEAuth.html">Server:GMEAuth</a></li><li><a href="Server_SafeStorage.html">Server:SafeStorage</a></li><li><a href="Server_UserProject.html">Server:UserProject</a></li><li><a href="module-Core.html">Core</a></li><li><a href="module-Storage.html">Storage</a></li><li><a href="module-crosscuts.html">crosscuts</a></li><li><a href="module-serialization.html">serialization</a></li></ul><h3>Externals</h3><ul><li><a href="external-Promise.html">Promise</a></li></ul><h3>Classes</h3><ul><li><a href="AddOnBase.html">AddOnBase</a></li><li><a href="AddOnUpdateResult.html">AddOnUpdateResult</a></li><li><a href="Artifact.html">Artifact</a></li><li><a href="BlobClient.html">BlobClient</a></li><li><a href="BlobMetadata.html">BlobMetadata</a></li><li><a href="BlobRunPluginClient.html">BlobRunPluginClient</a></li><li><a href="Client.html">Client</a></li><li><a href="Core.html">Core</a></li><li><a href="ExecutorClient.html">ExecutorClient</a></li><li><a href="GMENode.html">GMENode</a></li><li><a href="GmeLogger.html">GmeLogger</a></li><li><a href="InterPluginResult.html">InterPluginResult</a></li><li><a href="JobInfo.html">JobInfo</a></li><li><a href="OutputInfo.html">OutputInfo</a></li><li><a href="PluginBase.html">PluginBase</a></li><li><a href="PluginConfig.html">PluginConfig</a></li><li><a href="PluginMessage.html">PluginMessage</a></li><li><a href="PluginNodeDescription.html">PluginNodeDescription</a></li><li><a href="PluginResult.html">PluginResult</a></li><li><a href="Project.html">Project</a></li><li><a href="ProjectInterface.html">ProjectInterface</a></li><li><a href="Server_GMEAuth-GMEAuth.html">GMEAuth</a></li><li><a href="Server_SafeStorage-SafeStorage.html">SafeStorage</a></li><li><a href="Server_UserProject-UserProject.html">UserProject</a></li><li><a href="WebsocketRouter.html">WebsocketRouter</a></li><li><a href="WebsocketRouterUser.html">WebsocketRouterUser</a></li></ul><h3>Events</h3><ul><li><a href="Client.html#event:BRANCH_CHANGED">BRANCH_CHANGED</a></li><li><a href="Client.html#event:BRANCH_CLOSED">BRANCH_CLOSED</a></li><li><a href="Client.html#event:BRANCH_OPENED">BRANCH_OPENED</a></li><li><a href="Client.html#event:BRANCH_STATUS_CHANGED">BRANCH_STATUS_CHANGED</a></li><li><a href="Client.html#event:CONNECTED_USERS_CHANGED">CONNECTED_USERS_CHANGED</a></li><li><a href="Client.html#event:NETWORK_STATUS_CHANGED">NETWORK_STATUS_CHANGED</a></li><li><a href="Client.html#event:NOTIFICATION">NOTIFICATION</a></li><li><a href="Client.html#event:PLUGIN_FINISHED">PLUGIN_FINISHED</a></li><li><a href="Client.html#event:PLUGIN_INITIATED">PLUGIN_INITIATED</a></li><li><a href="Client.html#event:PLUGIN_NOTIFICATION">PLUGIN_NOTIFICATION</a></li><li><a href="Client.html#event:PROJECT_CLOSED">PROJECT_CLOSED</a></li><li><a href="Client.html#event:PROJECT_OPENED">PROJECT_OPENED</a></li></ul><h3><a href="global.html">Global</a></h3> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Jun 21 2024 09:43:40 GMT-0400 (Eastern Daylight Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>