diff --git a/src/heartbeat.ts b/src/heartbeat.ts index a4129b9..fc92e6d 100644 --- a/src/heartbeat.ts +++ b/src/heartbeat.ts @@ -1,15 +1,16 @@ import { send } from './send' +import type { WSGOConfig } from './types' const heartbeatMessage = 'ping' const heartbeatInterval = 1000 let heartbeatTimeout: ReturnType | undefined -export function heartbeatStart(ws: WebSocket): void { +export function heartbeatStart(ws: WebSocket, _config: WSGOConfig): void { heartbeatStop() heartbeatTimeout = setTimeout(() => { - send(heartbeatMessage, heartbeatMessage, ws) + send(ws, _config, heartbeatMessage, heartbeatMessage) }, heartbeatInterval) } @@ -18,9 +19,9 @@ export function heartbeatStop(): void { heartbeatTimeout = undefined } -export function listenHeartbeat(ws: WebSocket, e: MessageEvent): void { +export function listenHeartbeat(ws: WebSocket, _config: WSGOConfig, e: MessageEvent): void { if (e.data === heartbeatMessage) { - heartbeatStart(ws) + heartbeatStart(ws, _config) // eslint-disable-next-line no-useless-return return } diff --git a/src/index.ts b/src/index.ts index 3685c94..7bfe126 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { type WSGOEventName, type WSGOConfig, type WSGOSubscriptions } from './types' import { send } from './send' +import type { WSGOSendData } from './send/types' import { subscribe } from './subscribe' import type { WSGOSubscribeCallback } from './subscribe/types' import { heartbeatStart, heartbeatStop, listenHeartbeat } from './heartbeat' @@ -15,7 +16,7 @@ export default function create( open: () => void close: () => void - send: (eventName: WSGOEventName, data?: Parameters[1]) => void + send: (eventName: WSGOEventName, data?: WSGOSendData) => void subscribe: (eventName: WSGOEventName, callback: WSGOSubscribeCallback) => void } { let ws: WebSocket | undefined @@ -59,7 +60,9 @@ export default function create( close(ws) }, send: (...args) => { - send(...args, ws, _config) + if (ws === undefined) return + + send(ws, _config, ...args) }, subscribe: (...args) => { subscribe(...args, subscriptions, _config) @@ -83,7 +86,7 @@ function _listen(ws: WebSocket, subscriptions: WSGOSubscriptions, _config: WSGOC ws.onopen = (ev) => { _config.onConnected?.(ws, ev) - heartbeatStart(ws) + heartbeatStart(ws, _config) } ws.onclose = (ev) => { @@ -97,7 +100,7 @@ function _listen(ws: WebSocket, subscriptions: WSGOSubscriptions, _config: WSGOC } ws.onmessage = (e: MessageEvent): any => { - listenHeartbeat(ws, e) + listenHeartbeat(ws, _config, e) let message diff --git a/src/send.ts b/src/send/index.ts similarity index 54% rename from src/send.ts rename to src/send/index.ts index 04b97f5..1dcee2b 100644 --- a/src/send.ts +++ b/src/send/index.ts @@ -1,10 +1,9 @@ -import { type WSGOConfig } from './types' +import type { WSGOSendData } from './types' +import type { WSGOEventName, WSGOConfig } from '../types' /** Method allows you to send an event to the server */ -export function send(eventName: string, data?: any, ws?: WebSocket, config?: WSGOConfig): void { - if (ws === undefined) return - - if (config?.debugging ?? false) { +export function send(ws: WebSocket, _config: WSGOConfig, eventName: WSGOEventName, data?: WSGOSendData): void { + if (_config.debugging) { // start debug logging const timeout = 100 console.group(eventName, data) diff --git a/src/send/types.ts b/src/send/types.ts new file mode 100644 index 0000000..09138be --- /dev/null +++ b/src/send/types.ts @@ -0,0 +1 @@ +export type WSGOSendData = any diff --git a/vite.config.ts b/vite.config.ts index 164fef5..462976a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -12,7 +12,7 @@ export default defineConfig({ test: { environment: 'jsdom', coverage: { - reporter: ['json-summary'], + reporter: ['text', 'json-summary'], }, },