1. Home
  2. Docs
  3. 使用手册
  4. 运维剧本
  5. 命令列表
  6. CustomCommand组件

CustomCommand组件

提供了一种自定义命令的参考例子,方便开发者参考开发。
其源码地址:https://github.com/aoyiduo/woterm/blob/main/private/playbooks/test/TestCustomCommand.qml

Timer {
   id: timer
}

function sleep(ms) {
   return new Promise(function(resolve, reject){
       timer.interval = ms
       timer.repeat = false
       timer.running = false

       let fnTriggered = function() {
           timer.triggered.disconnect(fnTriggered)
           Qt.callLater(resolve)
       }
       timer.triggered.connect(fnTriggered)
       timer.start()
    })
}

function assertEqual(x, y) {
    return new Promise(function(resolve, reject) {
        if(x === y) {
           Qt.callLater(resolve, "Yes, it's equal.")
        }else{
           Qt.callLater(reject, "No, it's not equal.")
        }
    });
}

asyncGenerator(function *(){
    for(let i = 0; i < 10; i++) {
        let a = Math.random() * 100;
        let b = Math.random() * 100;
        let x = Math.floor(a % 2)
        let y = Math.floor(b % 2)
        Playbook.log("loop i=", i, x, y)
        yield assertEqual(x, y).then(function(result){
            Playbook.log("assertResult.then", result)
        }).catch(function(err){
            Playbook.log("assertResult.catch", err)
        })
        yield sleep(1000)
    }
})()