Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't use worker timers in worker and add web worker tests #1755

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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