|
@@ -0,0 +1,70 @@
|
|
|
|
+// utils/queueManager.js
|
|
|
|
+
|
|
|
|
+class QueueManager {
|
|
|
|
+ constructor() {
|
|
|
|
+ this._tasks = [];
|
|
|
|
+ this._listenMap = {};
|
|
|
|
+ this._timer = null;
|
|
|
|
+ this._interval = 60;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static get instance() {
|
|
|
|
+ if (!QueueManager._instance) {
|
|
|
|
+ QueueManager._instance = new QueueManager();
|
|
|
|
+ }
|
|
|
|
+ return QueueManager._instance;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ addTask(task) {
|
|
|
|
+ console.log(`QueueManager======addTask======${task}`);
|
|
|
|
+ this._tasks.unshift(task);
|
|
|
|
+ // mqttAddDebugCmd(`指令进队列:${task.toString()}, 数量:${this._tasks.length}`);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ listenTask(callback) {
|
|
|
|
+ // console.log(`QueueManager======listenTask======${callback.name || 'anonymous'}`);
|
|
|
|
+ // this._listenMap[callback.name || 'anonymous'] = callback;
|
|
|
|
+
|
|
|
|
+ if (!this._timer) {
|
|
|
|
+ this._timer = setInterval(() => {
|
|
|
|
+ if (this._tasks.length === 0) {
|
|
|
|
+ // console.log('QueueManager======execute======list empty');
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const last = this._tasks.pop();
|
|
|
|
+ if (callback) {
|
|
|
|
+ callback(last);
|
|
|
|
+ }
|
|
|
|
+ // Object.keys(this._listenMap).forEach(key => {
|
|
|
|
+ // const value = this._listenMap[key];
|
|
|
|
+ // if (last.constructor.name === key || key === 'dynamic') {
|
|
|
|
+ // console.log(`QueueManager======listenTask======forEach===callback`);
|
|
|
|
+ // value(last);
|
|
|
|
+ // }
|
|
|
|
+ // });
|
|
|
|
+
|
|
|
|
+ // 不管有没有订阅,都移除掉
|
|
|
|
+ }, this._interval);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ clear() {
|
|
|
|
+ console.log('QueueManager======clear======');
|
|
|
|
+ if (this._tasks.length > 0) {
|
|
|
|
+ this._tasks.length = 0;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ dispose() {
|
|
|
|
+ console.log('QueueManager======dispose======');
|
|
|
|
+ this.clear();
|
|
|
|
+ if (this._timer) {
|
|
|
|
+ clearInterval(this._timer);
|
|
|
|
+ this._timer = null;
|
|
|
|
+ }
|
|
|
|
+ this._listenMap = {};
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+module.exports = QueueManager;
|