Skip to content

Commit

Permalink
refactor: move send logic to folder, update types (#16)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
melishev and semantic-release-bot committed Feb 10, 2024
1 parent 0c8b41b commit d1a34bc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/heartbeat.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { send } from './send'
import type { WSGOConfig } from './types'

const heartbeatMessage = 'ping'
const heartbeatInterval = 1000

let heartbeatTimeout: ReturnType<typeof setTimeout> | 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)
}

Expand All @@ -18,9 +19,9 @@ export function heartbeatStop(): void {
heartbeatTimeout = undefined
}

export function listenHeartbeat(ws: WebSocket, e: MessageEvent<any>): void {
export function listenHeartbeat(ws: WebSocket, _config: WSGOConfig, e: MessageEvent<any>): void {
if (e.data === heartbeatMessage) {
heartbeatStart(ws)
heartbeatStart(ws, _config)
// eslint-disable-next-line no-useless-return
return
}
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -15,7 +16,7 @@ export default function create(

open: () => void
close: () => void
send: (eventName: WSGOEventName, data?: Parameters<typeof send>[1]) => void
send: (eventName: WSGOEventName, data?: WSGOSendData) => void
subscribe: <T>(eventName: WSGOEventName, callback: WSGOSubscribeCallback<T>) => void
} {
let ws: WebSocket | undefined
Expand Down Expand Up @@ -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)
Expand All @@ -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) => {
Expand All @@ -97,7 +100,7 @@ function _listen(ws: WebSocket, subscriptions: WSGOSubscriptions, _config: WSGOC
}

ws.onmessage = (e: MessageEvent<any>): any => {
listenHeartbeat(ws, e)
listenHeartbeat(ws, _config, e)

let message

Expand Down
9 changes: 4 additions & 5 deletions src/send.ts → src/send/index.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/send/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type WSGOSendData = any
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default defineConfig({
test: {
environment: 'jsdom',
coverage: {
reporter: ['json-summary'],
reporter: ['text', 'json-summary'],
},
},

Expand Down

0 comments on commit d1a34bc

Please sign in to comment.