Skip to content

Commit

Permalink
fix: don't use worker timers in worker and add web worker tests (#1755)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando authored Dec 5, 2023
1 parent f696f57 commit 38fb6ae
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/lib/PingTimer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clearTimeout as clearT, setTimeout as setT } from 'worker-timers'
import isBrowser from './is-browser'
import isBrowser, { isWebWorker } from './is-browser'

export default class PingTimer {
private keepalive: number
Expand Down
2 changes: 2 additions & 0 deletions src/lib/is-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ const isReactNativeEnv = () =>
const isBrowser =
isStandardBrowserEnv() || isWebWorkerEnv() || isReactNativeEnv()

export const isWebWorker = isWebWorkerEnv()

export default isBrowser
34 changes: 32 additions & 2 deletions test/browser/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@ import mqtt from '../../'; // this will resolve to mqtt/dist/mqtt.esm.js
const mqtt2 = window.mqtt

// get browser name
const browser = navigator.userAgent.toLowerCase().replace(/ /g, '_').replace(/\//g, '_')
const userAgent = navigator.userAgent.toLowerCase().replace(/ /g, '_').replace(/\//g, '_')

let browser = 'unknown'

console.log('userAgent:', userAgent)

if (userAgent.includes('chrome')) {
browser = 'chrome'
} else if (userAgent.includes('firefox')) {
browser = 'firefox'
} else if (userAgent.includes('safari')) {
browser = 'safari'
}

const browserTopic = `test/${browser}`

console.log('browser:', browser)

function run(proto, port, cb) {

Expand Down Expand Up @@ -71,5 +84,22 @@ it('should work with non-ESM version', () => {


run('ws', window.wsPort, () => {
run('wss', window.wssPort)
run('wss', window.wssPort, () => {
describe('MQTT.js browser test with web worker', () => {
it('should work with web worker', async () => {
const worker = new Worker('test/browser/worker.js')
const ready = new Promise((resolve, reject) => {
worker.onmessage = (e) => {
if (e.data === 'worker ready') {
resolve()
} else {
reject(e.data)
}
}
})
await ready
})
})
})
})

8 changes: 8 additions & 0 deletions test/browser/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// test mqttjs in worker

importScripts('/dist/mqtt.js');

/** @type { import('../../src').MqttClient }*/
const MQTT = mqtt;

postMessage(typeof MQTT?.connect === 'function' ? 'worker ready' : 'worker error');
2 changes: 2 additions & 0 deletions web-test-runner.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export default {
`<html>
<body>
<script src="dist/mqtt.js"></script>
<!-- web worker code -->
<link rel="modulepreload" href="test/browser/worker.js">
<script>
window.wsPort = ${wsPort};
window.wssPort = ${wssPort};
Expand Down

0 comments on commit 38fb6ae

Please sign in to comment.