Interface IManagedProgramInstance

All Superinterfaces:
IProgramInstance

public interface IManagedProgramInstance extends IProgramInstance
The intention for this interface is to provide a lifecycle API for a user-defined base class of a program. The use-case is as follows:
 MyProgram.gsp
 -------------

 extends MyBase

 doSomething()


 MyBase.gs
 ---------

 package abc

 class MyBase implements IManagedProgramInstance {
   override function beforeExecution() : boolean {
     // determine if the program should execute
   }
   override function afterExecution( t: Throwable ) {
     // handle the Throwable, do what you need to cleanup
   }
 }
 
Internally the Gosu compiler generates the evaluate() method like so:
   function evaluate( map: IExternalSymbolMap ) : Object {
     var $failure : Throwable
     if( this.beforeExecution() ) {
       try {
         [method-body] // returns result
       }
       catch( $catchFailure: Throwable ) {
         $failure = $catchFailure
       }
       finally {
         this.afterExecution( $failure )
       }
     }
     return null // only get here if exception not rethrown in afterExecution()
   }
 
  • Method Details

    • beforeExecution

      boolean beforeExecution()
      Called before IProgramInstance#evaluate().
      Returns:
      false if the program should not be executed i.e., don't call IProgramInstance#evaluate(), true if the program should execute normally.
    • afterExecution

      void afterExecution(Throwable error)
      Called after IProgramInstance#evaluate().
      Parameters:
      error - The exception or error if IProgramInstance#evaluate() terminates abnormally, otherwise null.