TestDirective.js 1023 Bytes
Newer Older
xuchaozou's avatar
xuchaozou committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import BaseDirective from "./BaseDirective";

class TestDirective extends BaseDirective {
    match () {
        const {text} = this
        if(/熊猫测试指令/.test(text)) {
            return true
        }
        return false
    }
    async excute ({
        sendMsg = function () {},
    }) {
        return new Promise((resolve , reject) => {
            this.resolve = resolve
            this.reject = reject
            this.testCloseSession({sendMsg})
        })
    }

    closeResponse () {
        if(this.timer) {
            clearInterval(this.timer)
            this.timer = null
        }
邹绪超's avatar
邹绪超 committed
26
        this.reject && this.reject()
xuchaozou's avatar
xuchaozou committed
27 28 29
    }

    testCloseSession({sendMsg}) {
xuchaozou's avatar
xuchaozou committed
30 31
        let count = 0;
        let random = parseInt(Math.random() * 50 + 1)
xuchaozou's avatar
xuchaozou committed
32
        this.timer = setInterval(() => {
xuchaozou's avatar
xuchaozou committed
33 34 35 36 37
            count++
            if(count % random == 0) {
                random =  parseInt(Math.random() * 50 + 1)
                sendMsg(count)
            } 
xuchaozou's avatar
xuchaozou committed
38 39 40 41 42
        }, 100)
    }
}

export default TestDirective