Vue前端实现一个本地消息队列(MQ), 让消息延迟消费或者做缓存

MQ功能实现的具体代码(TsMQ.ts):

TypeScript 复制代码
import { v4 as uuidx } from 'uuid';

import emitter from '@/utils/mitt'

class Message {

    // 过期时间,0表示马上就消费
    exp: number;

    // 消费标识,避免重复消费
    tag : string;

    // 消息体
    body : any;

    constructor( exp: number ,  body : any ) {
        if (exp == 0) {
            this.exp = 0;
        }else {
            this.exp = Date.now().valueOf() + exp;
        }
        this.body = body;
        this.tag = uuidx();
    }

}


export default class TsMQ {

    static tsMQ : TsMQ;

    repository : Map<string, any[]>;

    /*
      获取单列的MQ对象
     */
    static getInstance() : TsMQ {
        if(this.tsMQ == null) {
            this.tsMQ = new TsMQ();
        }
        return this.tsMQ;
    }

    constructor() {
        this.repository = new Map<string,any[]>();
        // 把消息放入Mitt进行推送
        setInterval(()=> {
            Array.from(this.repository.keys()).forEach( key => {
               let poll = this.repository.get(key) as any[];
               if(poll.length > 0) {
                   poll.forEach( item => {
                       if (item.exp == 0 || item.exp <= Date.now().valueOf() - 100) {
                           emitter.emit(key,item.body);
                           let single : any[] = this.repository.get(key) as any[];
                           single = single.filter(dispose => {
                               return dispose.tag !== item.tag;
                           });
                           this.repository.set(key,single);
                       }
                   });
               }
            });
        },100)
    }



    /*
      * @describe 放消息入队列
      * @param queue : string 队列名称
      * @param exp : number 消息消费时间
      * @param  message : any 消息体
     */
    pushMessage( queue : string , exp : number,  message : any ) {
        if(this.repository.has(queue)) {
            let single : any[] = this.repository.get(queue) as any[];
            single.push(new Message(exp,message));
            this.repository.set(queue,single);
        }else {
            let temp = [];
            temp.push(new Message(exp,message));
            this.repository.set(queue,temp);
        }
    }


    /*
     * 直接获取消息,可以配合做本地缓存
     * 就要去掉constructor的轮询推送
     */
    takeMessage( queue : string ) : any {
        let single : any[] = this.repository.get(queue) as any[];
        if( single && single.length > 0) {
            let message = single.shift();
            this.repository.set(queue,single);
            return message;
        }else {
            return '队列没有消息!';
        }
    }

}
复制代码
提示:其中需要用到三方的uuid和mitt,然后要消息持久化可以用到pinia来让消息持久化,本案列没有采用持久化

uuid的三方包安装命令

cs 复制代码
npm install uuid

使用方式:

javascript 复制代码
<script setup lang="ts">
   import TsMQ from '@/utils/TsMQ'
   import emitter from '@/utils/mitt'
   let tsMQ : TsMQ = TsMQ.getInstance();

   //订阅消息
   emitter.on("HelloWord",e => {
     console.log(`收到消息:${JSON.stringify(e)}\n消息时间:${new Date().toLocaleString()}`)
   })

   //投送消息
   function tsMQs() {
       console.log(`M2投递时间:${new Date().toLocaleString()}`)
       tsMQ.pushMessage("HelloWord",1000 * 20,{ name : 'M2', second:20 });
       tsMQ.pushMessage("HelloWord",1000 * 3,{ name : 'M1', second:3 });
   // MQ功能实现的具体代码,提示:其中需要用到三方的uuid和mitt,然后要消息持久化可以用到pinia来让消息持久化,本案列没有采用持久化
   }

   function takeMQs() {
       console.log(tsMQ.takeMessage('1'));
   }


</script>

<template>
  <div id="app" style="display: flex;flex-direction: column;justify-content: center;height: 100%;width: 100%;margin: 0;padding: 0">
    <span @click="tsMQs">MQ投送</span>
    <span @click="takeMQs">MQ获取</span>
  </div>
</template>

<style scoped>

</style>

效果:

复制代码
总结:我们可以看到我们实现了这个功能 ,其中可以用来作为缓存使用,同理可写
相关推荐
世界哪有真情2 小时前
拿人类意识卡 AI?等于用 bug 验收正式产品
前端·人工智能·后端
鱼樱前端2 小时前
别再自己拼凑了!这款 Vue 3.5+ 严肃产品级组件库,把 AI 对话、工作流和复杂数据全包了!
javascript·vue.js·github
Listen·Rain2 小时前
Vue3:setup详解
前端·javascript·vue.js
白露与泡影2 小时前
Redis 缓存实战:雪崩、穿透、击穿一次讲透
数据库·redis·缓存
Patrick_Wilson3 小时前
修好 bug 只是开始:一次由监控需求反向重构日志结构的复盘
前端·监控·数据可视化
kyriewen4 小时前
面试官让我优化一个卡死的表格,我打开 Claude 五秒重写,他放下咖啡问我要不要来当组长
前端·javascript·面试
小粉粉hhh4 小时前
React(二)——dom、组件间通信、useEffect
前端·javascript·react.js
Csvn4 小时前
🤖 AI 生成前端代码的 5 个典型翻车现场及修复指南
前端
IT_陈寒5 小时前
SpringBoot自动装配坑了我一天,原来问题出在这
前端·人工智能·后端
索西引擎6 小时前
【React】key 属性:协调算法中的元素标识机制与最佳实践
前端·javascript·react.js