web-queue
一个同时支持浏览器和Node.js环境的队列库,使用TypeScript开发。
安装
npm install web-queue
yarn add web-queue
使用方法
基本用法
import { Queue } from 'web-queue';
const queue = new Queue<number>();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
console.log(queue.size());
console.log(queue.peek());
console.log(queue.dequeue());
console.log(queue.isEmpty());
queue.clear();
console.log(queue.isEmpty());
转换为数组
const queue = new Queue<string>();
queue.enqueue('a');
queue.enqueue('b');
queue.enqueue('c');
const array = queue.toArray();
console.log(array);
使用主题队列(TopicQueue)
TopicQueue 使用 BroadcastChannel API 实现基于主题的消息传递,适用于浏览器环境。
import { TopicQueue } from 'web-queue';
const topic = new TopicQueue<string>('myChannel');
const unsubscribe = topic.subscribe(message => {
console.log('收到消息:', message);
});
topic.enqueue('Hello World');
unsubscribe();
topic.close();
在多个标签页/窗口之间通信
const topicA = new TopicQueue<string>('sharedChannel');
topicA.enqueue('来自标签页 A 的消息');
const topicB = new TopicQueue<string>('sharedChannel');
topicB.subscribe(message => {
console.log('标签页 B 收到:', message);
});
API
Queue
enqueue(item: T): void - 将元素添加到队列末尾
dequeue(): T | undefined - 移除并返回队首元素
peek(): T | undefined - 返回队首元素但不移除
size(): number - 返回队列中的元素数量
isEmpty(): boolean - 检查队列是否为空
clear(): void - 清空队列
toArray(): T[] - 将队列转换为数组
TopicQueue
TopicQueue 继承自 Queue,并添加了以下方法:
constructor(topicName?: string) - 创建一个基于主题的队列
subscribe(callback: (item: T) => void): () => void - 订阅主题消息,返回取消订阅的函数
close(): void - 关闭广播通道
开发
npm install
npm run dev
npm run build
npm test
许可证
Apache License Version 2.0