libflitter/Unit.js

/**
 * @module libflitter/Unit
 */

const Service = require('./NamedService')

/**
 * Base class for all Flitter Units.
 * A Unit provides one piece of functionality in Flitter.
 * @extends {module:libflitter/NamedService~NamedService}
 */
class Unit extends Service {
    /**
     * Defines the services required by this unit.
     * The 'app' service is provided by default.
     * @returns {Array<string>}
     */
    static get services() {
        return [...super.services, 'app']
    }

    /**
     * Names of additional services provided by this unit.
     * @returns {Array<string>}
     */
    static get provides() {
        return []
    }

    static STATUS_STOPPED = 'stopped'
    static STATUS_STARTING = 'starting'
    static STATUS_RUNNING = 'running'
    static STATUS_STOPPING = 'stopping'
    static STATUS_ERROR = 'error'

    /**
     * Loads the unit.
     * This should attach the unit's functionality to the Flitter app. It is provided a context where its variables
     * and methods that need to be made available to the rest of the app should reside.
     * @param {module:libflitter/app/FlitterApp~FlitterApp} app - the Flitter app
     * @param {module:libflitter/Context~Context} context - the unit's context. This is where variables and methods provided by the unit should be bound.
     * @returns {Promise<void>}
     */
    async go(app){

    }

    /**
     * Get the name of the unit. Should be a lowercase, alphanum/dash/underscore string.
     * @returns {String}
     */
    name(){
        return this.constructor.name
    }

    /**
     * Get the directories provided by the unit. 
     * Should be an object such that key => value is the name of the directory => fully qualified path to the directory.
     * @returns {Object}
     */
    directories(){
        return {}
    }

    /**
     * Cleans up the unit's resources before Flitter closes.
     * @param {module:libflitter/app/FlitterApp~FlitterApp} app - the Flitter app
     * @returns {Promise<void>}
     */
    async cleanup(app){
        
    }

    /**
     * Get or set the unit's current status.
     * @param {string} [set] - if provided, set the status of the unit
     * @return {string} - current status of the unit
     */
    status(set){
        if ( !this._status ) this._status = this.constructor.STATUS_STOPPED
        
        if ( !set ) return this._status
        else this._status = set
    }
}

module.exports = exports = Unit