import { Agent } from 'egg' ; import assert from 'assert' ; import { GloablEvents , BotLogger , loggerWrapper } from './Utils' ; /**
 * Hypertrons agent plugin base class
 * Provide basic functions:
 *   1. LoadToAgent static function
 *   2. agent hooks on start
 *   3. getConfig function to get config for certain repo
 */ export abstract class AgentPluginBase < TConfig > { param name param constructor param app static LoadToAgent < T extends AgentPluginBase < TConfig > , TConfig > ( name , constructor , agent ) { if ( ! Object . prototype . isPrototypeOf . call ( AgentPluginBase , constructor ) ) { throw new Error ( `Invalid plugin type for ${ name }` ) ; } agent . addSingleton ( name , ( config , agent ) => { const plugin = new constructor ( config , agent ) ; plugin . name = name ; plugin . logger . info ( 'Going to load' ) ; return plugin ; } ) ; } protected agent : Agent ; protected name : string ; protected config : TConfig ; public logger : BotLogger ; constructor ( config , agent ) { this . agent = agent ; this . config = config ; this . logger = loggerWrapper ( this . agent . logger , ( ) => `[${ this . name }-agent]` ) ; this . checkConfigFields ( ) . forEach ( f => this . checkConfig ( config , f ) ) ; this . agent . messenger . on ( GloablEvents . READY , ( ) => { this . logger . info ( 'Bot ready, start to run onReady.' ) ; this . onReady ( ) ; } ) ; this . agent . messenger . on ( GloablEvents . START , ( ) => { this . logger . info ( 'Bot start, start to run onStart.' ) ; this . onStart ( ) ; } ) ; this . agent . messenger . on ( GloablEvents . CLOSE , ( ) => { this . logger . info ( 'Bot close, start to run onClose.' ) ; this . onClose ( ) ; } ) ; } protected checkConfigFields ( ) { return [ ] ; } private checkConfig ( config , field ) { assert . notEqual ( config [ field ] , undefined ) ; } /**
     * When plugin ready
     */ public abstract async onReady ( ) ; /**
     * When server start
     */ public abstract async onStart ( ) ; /**
     * When app close
     */ public abstract async onClose ( ) ; }