UNPKG

blazepress

Version:

A rapid web application development platform for Node.js

106 lines (55 loc) 1.44 kB
const EventEmitter = require( 'events' ).EventEmitter const ActiveSet = require( '../collections/ActiveSet' ) const lang = require( '../lang' ).ns( 'Service' ) class DefaultService extends EventEmitter { constructor() { let services = new ActiveSet super() this.services = services services.events.on( 'added', ( service ) => { service.parent = this service.emit( 'mounted', this ) this.emit( 'serviceMounted', service ) }) services.events.on( 'deleted', ( service ) => { service.parent = null service.emit( 'unmounted', this ) this.emit( 'serviceUnmounted', service ) }) } emit( ...args ) { DefaultService.log( this + ':', ...args ) EventEmitter.prototype.emit.call( this, ...args ) } toString() { return '[' + this.constructor.name + ']' } inspect() { return this.toString() } start() { if( this.isRunning ) { throw new Error( lang.get( 'error_no_start_reason_already_running', this ) ) } else { this.isRunning = true this.emit( 'started' ) } } stop() { if( this.isRunning ) { this.isRunning = false } else { throw new Error( lang.get( 'error_no_stop_reason_not_running', this ) ) } } log( ...args ) { Service.log( this, ...args ) } } DefaultService.nolog = function() {} DefaultService.log = ( function log( ...args ) { console.log.call( console, ...args ) }) module.exports = DefaultService