Skip to content

Commit d1a34bc

Browse files
refactor: move send logic to folder, update types (#16)
* feat: add additional data to received message, move debugger to decorator * test: update subscribe test * test: update all tests * test: fix bug when transform data on mock server * refactor: move subscribe logic to folder * refactor: move send logic to folder, update types * refactor: move send logic to folder * test: add aditional coverage reporter * chore(release): 1.3.0 [skip ci] # [1.3.0](v1.2.0...v1.3.0) (2024-02-10) ### Features * add additional data to received message, move debugger to decorator ([#15](#15)) ([14b7a8c](14b7a8c)) --------- Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
1 parent 0c8b41b commit d1a34bc

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

src/heartbeat.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { send } from './send'
2+
import type { WSGOConfig } from './types'
23

34
const heartbeatMessage = 'ping'
45
const heartbeatInterval = 1000
56

67
let heartbeatTimeout: ReturnType<typeof setTimeout> | undefined
78

8-
export function heartbeatStart(ws: WebSocket): void {
9+
export function heartbeatStart(ws: WebSocket, _config: WSGOConfig): void {
910
heartbeatStop()
1011

1112
heartbeatTimeout = setTimeout(() => {
12-
send(heartbeatMessage, heartbeatMessage, ws)
13+
send(ws, _config, heartbeatMessage, heartbeatMessage)
1314
}, heartbeatInterval)
1415
}
1516

@@ -18,9 +19,9 @@ export function heartbeatStop(): void {
1819
heartbeatTimeout = undefined
1920
}
2021

21-
export function listenHeartbeat(ws: WebSocket, e: MessageEvent<any>): void {
22+
export function listenHeartbeat(ws: WebSocket, _config: WSGOConfig, e: MessageEvent<any>): void {
2223
if (e.data === heartbeatMessage) {
23-
heartbeatStart(ws)
24+
heartbeatStart(ws, _config)
2425
// eslint-disable-next-line no-useless-return
2526
return
2627
}

src/index.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type WSGOEventName, type WSGOConfig, type WSGOSubscriptions } from './types'
22

33
import { send } from './send'
4+
import type { WSGOSendData } from './send/types'
45
import { subscribe } from './subscribe'
56
import type { WSGOSubscribeCallback } from './subscribe/types'
67
import { heartbeatStart, heartbeatStop, listenHeartbeat } from './heartbeat'
@@ -15,7 +16,7 @@ export default function create(
1516

1617
open: () => void
1718
close: () => void
18-
send: (eventName: WSGOEventName, data?: Parameters<typeof send>[1]) => void
19+
send: (eventName: WSGOEventName, data?: WSGOSendData) => void
1920
subscribe: <T>(eventName: WSGOEventName, callback: WSGOSubscribeCallback<T>) => void
2021
} {
2122
let ws: WebSocket | undefined
@@ -59,7 +60,9 @@ export default function create(
5960
close(ws)
6061
},
6162
send: (...args) => {
62-
send(...args, ws, _config)
63+
if (ws === undefined) return
64+
65+
send(ws, _config, ...args)
6366
},
6467
subscribe: (...args) => {
6568
subscribe(...args, subscriptions, _config)
@@ -83,7 +86,7 @@ function _listen(ws: WebSocket, subscriptions: WSGOSubscriptions, _config: WSGOC
8386
ws.onopen = (ev) => {
8487
_config.onConnected?.(ws, ev)
8588

86-
heartbeatStart(ws)
89+
heartbeatStart(ws, _config)
8790
}
8891

8992
ws.onclose = (ev) => {
@@ -97,7 +100,7 @@ function _listen(ws: WebSocket, subscriptions: WSGOSubscriptions, _config: WSGOC
97100
}
98101

99102
ws.onmessage = (e: MessageEvent<any>): any => {
100-
listenHeartbeat(ws, e)
103+
listenHeartbeat(ws, _config, e)
101104

102105
let message
103106

src/send.ts src/send/index.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { type WSGOConfig } from './types'
1+
import type { WSGOSendData } from './types'
2+
import type { WSGOEventName, WSGOConfig } from '../types'
23

34
/** Method allows you to send an event to the server */
4-
export function send(eventName: string, data?: any, ws?: WebSocket, config?: WSGOConfig): void {
5-
if (ws === undefined) return
6-
7-
if (config?.debugging ?? false) {
5+
export function send(ws: WebSocket, _config: WSGOConfig, eventName: WSGOEventName, data?: WSGOSendData): void {
6+
if (_config.debugging) {
87
// start debug logging
98
const timeout = 100
109
console.group(eventName, data)

src/send/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type WSGOSendData = any

vite.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default defineConfig({
1212
test: {
1313
environment: 'jsdom',
1414
coverage: {
15-
reporter: ['json-summary'],
15+
reporter: ['text', 'json-summary'],
1616
},
1717
},
1818

0 commit comments

Comments
 (0)