目录场景核心问题状态监听状态监听优势状态监听劣势重构实现思路依赖收集总结场景 在业务上现在有一个场景,当发生业务行为变化时,需要对各个模块的行为进行数据收集,数据用途可以用作回顾,
在业务上现在有一个场景,当发生业务行为变化时,需要对各个模块的行为进行数据收集,数据用途可以用作回顾,也可以是例如监控这样的场景。
说白了这个需求就是需要对各个模块状态变更进行记录,然后再格式化上传到服务端。
解题思路有两种一种是状态监听,第二主动收集。
快速实现利用状态管理和wacth的机制很快就知道不同模块的状态变更,然后就可以获取数据,再格式化数据,发送给服务端
function useA(){
wacth(new,old){
if(start){
if(new.type =='need')
const a = {
a:new.a
}
const aa = {
aa:new.aa
}
upload(a)
upload(aa)
}
}
}
// 多处数据散落
function useB(){
// 重复监听
wacth(new,old){
// 全局判断
if(start){
// 不同状态判断
if(new.type =='need')
const b = {
b:new.b
}
//重复数据格式
const aa = {
b:new.aa
}
upload(b)
upload(aa)
}
}
}
class Dep {
private subs: any = []
// 添加观察者
public addSub(sub: any) {
if (sub && sub.update) {
this.subs.push(sub)
}
}
// 发送通知
public notify(content: any) {
this.subs.forEach((sub: any) => {
sub.update(content)
})
}
}
class Watcher {
private cb!: (arg: any) => void
constructor(cb: (arg: any) => void) {
// 回调函数负责更新
this.cb = cb
}
// 当数据发生变化的时候更新
update(content: any) {
this.cb(content)
}
}
class Channel {
// 管道存储数组
private queue: any = []
// 管道大小
private limitSize = 1
// 管道名称
public name: string
constructor(name: string, limitSize = 1) {
this.name = name
// 最小尺寸是1
limitSize = limitSize >= 1 ? limitSize : 1
this.limitSize = limitSize
}
push(item: any) {
// 如果超出限制尺寸移除第一个
if (this.limitSize == this.queue.length) {
this.queue.shift()
}
this.queue.push(item)
}
getLast() {
if (this.queue.length > 0) {
return this.queue[this.queue.length - 1]
} else {
throw new Error('no item return')
}
}
getLastIndex(index: number) {
try {
return this.queue[this.queue.length - index - 1]
} catch (error) {
throw new Error('no item return')
}
}
isEmpty() {
return this.queue.length == 0
}
}
export class Collection {
// 依赖收集对象
private dep = new Dep()
// 各个数据频道分类
private dataQueue = ['A', 'B', 'C']
// 频道集合
private channelMap = new Map()
// 上传队列
private MQ!: LiveCollectionMQ
// 策略模式:数据类型不同对应不同的处理机制
private strategies = {
A: () => {
// 可以在不同的管道中获取相对应的数据进行不同逻辑的处理
},
B: () => {
},
C: () => {
},
} as Record<NotifyType, any>
constructor() {
this.init()
}
private init() {
// 初始化watcher
this.intWatcher()
// 初始化频道
this.initChannel()
// 初始化数据
this.initData()
// 初始化队列
this.initMQ()
}
private intWatcher() {
this.dep.addSub(
new Watcher((type: NotifyType) => {
const handlerBack = this.getHandler(type)
handlerBack()
}),
)
}
private initChannel() {
this.dataQueue.forEach(item => {
this.channelMap.set(item, new Channel(item, 3))
})
}
private initData() {
}
private initMQ() {
}
public getMQ() {
return this.MQ
}
private getChannel(name: NotifyType) {
if (this.channelMap.get(name)) {
return this.channelMap.get(name)
} else {
throw new Error('no channel')
}
}
public notify(type: NotifyType, mes: any) {
// 设置管道缓存
this.setChannel(type, mes)
// 全局统一判断状态判断是否要分发数据
if (state.value.type) {
this.dep.notify(type)
}
}
private setChannel(name: NotifyType, mes: any) {
const channel = this.getChannel(name)
channel.push(mes)
}
private getHandler(name: NotifyType) {
return this.strategies[name]
}
public getChannelLast(name: NotifyType) {
try {
const channel = this.getChannel(name)
return channel.getLast()
} catch (error) {
throw new Error(error)
}
}
public getChannelItemByLastIndex(name: NotifyType, index: number) {
try {
const channel = this.getChannel(name)
return channel.getLastIndex(index)
} catch (error) {
throw new Error(error)
}
}
public generateA() {
}
public generateB() {
}
public generateC() {
}
}
export const CollectionHelper = new Collection()
以上就是用vue设计一个数据采集器的详细内容,更多关于vue 设计数据采集器的资料请关注编程网其它相关文章!
--结束END--
本文标题: 用vue设计一个数据采集器
本文链接: https://www.lsjlt.com/news/124482.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0