!7

set flag DynModule.

srecord ScriptItem(S desc, S code) {
  Double seconds;
  S status;
  
  *(S *desc, S *code, double *seconds) {}
}

sclass Scripter extends DynObjectTable<ScriptItem> {
  int iRunning;
  S status;
  
  start {
    javaEval_useDiskCache();
    
    setData(ll(
      ScriptItem("Say 1", [[infoBox("1")]], 4.0),
      ScriptItem("Say 2", [[infoBox("2")]], 4.0)
    ));
  }
  
  visualize {
    ret northAndCenter(
      withMargin(centerAndEastWithMargin(rightAlignLabel(dm_fieldLabel('status)), jbutton("Run", r runIt))),
      jSection("SCRIPT", super.visualize()));
  }
  
  void runIt {
    thread "Run Script" {
      setFields(iRunning := 0, status := "running");
      afterwards { setField(status := "done"); }
      while (iRunning < l(data)) {
        ScriptItem item = get(data, iRunning);
        long targetTime = sysNow()+toMS(toDouble(item.seconds));
        item.status = "running";
        updateTable();
        setField(iRunning := iRunning+1);
        evalJava(item.code);
        sleepUntilSys(targetTime);
        item.status = "done";
        updateTable();
      }
    }
  }
}