33 lines
620 B
JavaScript
33 lines
620 B
JavaScript
|
import BLESDK from './ble'
|
||
|
|
||
|
class taskQueue {
|
||
|
list = []
|
||
|
isExecute = false
|
||
|
overtime = null
|
||
|
|
||
|
addTask(buffer) {
|
||
|
this.list.push(buffer)
|
||
|
if (!this.isExecute) {
|
||
|
this.isExecute = true
|
||
|
this.executeTask()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
executeTask() {
|
||
|
this.isExecute = !!this.list.length
|
||
|
if (!this.list.length) return
|
||
|
BLESDK.writeBleValue(new Uint8Array(['0x7b', ...this.list[0]]).buffer)
|
||
|
this.list.shift()
|
||
|
|
||
|
this.overtime = setTimeout(() => {
|
||
|
this.executeTask()
|
||
|
}, 1000)
|
||
|
}
|
||
|
stopOverTime() {
|
||
|
clearTimeout(this.overtime)
|
||
|
this.overtime = null
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default new taskQueue()
|