Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(desktop): share one message event #1684

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/views/connections/ConnectionsDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ import { ipcRenderer } from 'electron'
import { MqttClient, IConnackPacket, IPublishPacket, IClientPublishOptions, IDisconnectPacket, Packet } from 'mqtt'
import _ from 'lodash'
import { Subject, fromEvent } from 'rxjs'
import { bufferTime, map, filter, takeUntil } from 'rxjs/operators'
import { bufferTime, map, filter, takeUntil, shareReplay } from 'rxjs/operators'
import cbor from 'cbor'

import time from '@/utils/time'
Expand Down Expand Up @@ -1397,7 +1397,7 @@ export default class ConnectionsDetail extends Vue {

// Recevied message
private onMessageArrived(client: MqttClient, id: string) {
const unsubscribe$ = new Subject()
const unsubscribe$ = new Subject<void>()

if (client.listenerCount('close') <= 1) {
fromEvent(client, 'close').subscribe(() => {
Expand All @@ -1407,20 +1407,22 @@ export default class ConnectionsDetail extends Vue {
})
}

const messageSubject$ = fromEvent(client, 'message').pipe(takeUntil(unsubscribe$))

const processMessageSubject$ = messageSubject$.pipe(
const messageSubject$ = fromEvent(client, 'message').pipe(
takeUntil(unsubscribe$),
map(([topic, payload, packet]: [string, Buffer, IPublishPacket]) => {
return this.processReceivedMessage(topic, payload, packet)
}),
shareReplay(1),
)

const SYSMessageSubject$ = processMessageSubject$.pipe(
const SYSMessageSubject$ = messageSubject$.pipe(
filter((m: MessageModel) => this.showBytes && id === this.curConnectionId && m.topic.includes('$SYS')),
shareReplay(1),
)

const nonSYSMessageSubject$ = processMessageSubject$.pipe(
const nonSYSMessageSubject$ = messageSubject$.pipe(
filter((m: MessageModel) => !(this.showBytes && id === this.curConnectionId && m.topic.includes('$SYS'))),
shareReplay(1),
)

// Print message log
Expand All @@ -1430,12 +1432,16 @@ export default class ConnectionsDetail extends Vue {

// Render messages
nonSYSMessageSubject$.pipe(bufferTime(500)).subscribe((messages: MessageModel[]) => {
messages.length && this.renderMessage(id, messages)
if (messages.length) {
this.renderMessage(id, messages)
}
})

// Save messages
nonSYSMessageSubject$.pipe(bufferTime(1000)).subscribe((messages: MessageModel[]) => {
messages.length && this.saveMessage(id, messages)
if (messages.length) {
this.saveMessage(id, messages)
}
})

// Bytes statistics
Expand Down