亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于Vue實現(xiàn)本地消息隊列MQ的示例詳解

 更新時間:2024年02月27日 08:43:32   作者:大陽光男孩  
這篇文章為大家詳細主要介紹了如何基于Vue實現(xiàn)本地消息隊列MQ, 讓消息延遲消費或者做緩存,文中的示例代碼講解詳細,需要的可以參考一下

MQ功能實現(xiàn)的具體代碼(TsMQ.ts):

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的三方包安裝命令

npm install uuid

使用方式:

<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功能實現(xiàn)的具體代碼,提示:其中需要用到三方的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>

效果:

總結:我們可以看到我們實現(xiàn)了這個功能 ,其中可以用來作為緩存使用,同理可寫

到此這篇關于基于Vue實現(xiàn)本地消息隊列MQ的示例詳解的文章就介紹到這了,更多相關Vue本地消息隊列MQ內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue click.stop阻止點擊事件繼續(xù)傳播的方法

    vue click.stop阻止點擊事件繼續(xù)傳播的方法

    今天小編就為大家分享一篇vue click.stop阻止點擊事件繼續(xù)傳播的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • 詳解vue mint-ui源碼解析之loadmore組件

    詳解vue mint-ui源碼解析之loadmore組件

    本篇文章主要介紹了vue mint-ui源碼解析之loadmore組件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Vue中mixins的使用方法以及實際項目應用指南

    Vue中mixins的使用方法以及實際項目應用指南

    vue中提供了一種混合機制--mixins,用來更高效的實現(xiàn)組件內(nèi)容的復用,下面這篇文章主要給大家介紹了關于Vue中mixins的使用方法以及實際項目應用指南,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-03-03
  • vue 中swiper的使用教程

    vue 中swiper的使用教程

    本文通過實例代碼給大家介紹了vue 中swiper的使用,感興趣的朋友跟隨腳本之家小編一起學習吧
    2018-05-05
  • vue3中的hook簡單封裝

    vue3中的hook簡單封裝

    這篇文章主要介紹了vue3中的hook簡單封裝,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 前端之vue3使用WebSocket的詳細步驟

    前端之vue3使用WebSocket的詳細步驟

    websocket實現(xiàn)的全雙工通信,真真太香了,下面這篇文章主要給大家介紹了關于前端之vue3使用WebSocket的詳細步驟,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • vue對枚舉值轉(zhuǎn)換方式

    vue對枚舉值轉(zhuǎn)換方式

    這篇文章主要介紹了vue對枚舉值轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue3中關于i18n字符串轉(zhuǎn)義問題

    vue3中關于i18n字符串轉(zhuǎn)義問題

    這篇文章主要介紹了vue3中關于i18n字符串轉(zhuǎn)義問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Vue3使用Lottie實現(xiàn)一個簡單的加載動畫

    Vue3使用Lottie實現(xiàn)一個簡單的加載動畫

    Lottie 是一個流行的動畫庫,它允許我們使用 JSON 文件來渲染高質(zhì)量的動畫,本文將介紹一下如何在Vue3中集成 Lottie 動畫實現(xiàn)一個加載動畫效果,需要的可以了解下
    2025-02-02
  • vue實現(xiàn)GitHub的第三方授權方法示例

    vue實現(xiàn)GitHub的第三方授權方法示例

    本文主要介紹了vue實現(xiàn)GitHub的第三方授權,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11

最新評論