@@ -12,8 +12,8 @@ import {
12
12
SUPER_CHAT_MESSAGE , type SuperChatHandler ,
13
13
WATCHED_CHANGE , type WatchedChangeHandler ,
14
14
} from '../parser'
15
- import type { Message } from '../types/app'
16
15
import type { KeepLiveTCP , KeepLiveWS , Message as WSMessage } from 'tiny-bilibili-ws'
16
+ import { normalizeDanmu , checkIsDuplicateDanmuMsg } from '../utils/message'
17
17
18
18
export type MsgHandler = Partial <
19
19
{
@@ -40,19 +40,6 @@ export type MsgHandler = Partial<
40
40
& WatchedChangeHandler
41
41
>
42
42
43
- const normalizeDanmu = < T > ( msgType : string , body : T ) : Message < T > => {
44
- const timestamp = Date . now ( )
45
- const randomText = Math . floor ( Math . random ( ) * 10000 ) . toString ( )
46
- // @ts -ignore
47
- const id = `${ timestamp } :${ msgType } :${ body . user ?. uid } :${ randomText } `
48
- return {
49
- id,
50
- timestamp,
51
- type : msgType ,
52
- body,
53
- }
54
- }
55
-
56
43
export const listenAll = ( instance : KeepLiveTCP | KeepLiveWS , roomId : number , handler ?: MsgHandler ) => {
57
44
if ( ! handler ) return
58
45
@@ -99,14 +86,14 @@ export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, ha
99
86
100
87
// DANMU_MSG
101
88
if ( handler [ DANMU_MSG . handlerName ] || handler [ DANMU_MSG_402220 . handlerName ] ) {
102
- instance . on ( DANMU_MSG . eventName , ( data : WSMessage < any > ) => {
89
+ const msgCallback = handler [ DANMU_MSG . handlerName ] !
90
+ const handleDanmuMsg = ( data : WSMessage < any > ) => {
103
91
const parsedData = DANMU_MSG . parser ( data . data , roomId )
104
- handler [ DANMU_MSG . handlerName ] ?.( normalizeDanmu ( DANMU_MSG . eventName , parsedData ) )
105
- } )
106
- instance . on ( DANMU_MSG_402220 . eventName , ( data : WSMessage < any > ) => {
107
- const parsedData = DANMU_MSG_402220 . parser ( data . data , roomId )
108
- handler [ DANMU_MSG_402220 . handlerName ] ?.( normalizeDanmu ( DANMU_MSG_402220 . eventName , parsedData ) )
109
- } )
92
+ if ( checkIsDuplicateDanmuMsg ( parsedData ) ) return
93
+ msgCallback ( normalizeDanmu ( DANMU_MSG . eventName , parsedData ) )
94
+ }
95
+ instance . on ( DANMU_MSG . eventName , handleDanmuMsg )
96
+ instance . on ( DANMU_MSG_402220 . eventName , handleDanmuMsg )
110
97
}
111
98
112
99
// GUARD_BUY
0 commit comments