Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ SSR-safe, zero-config Web Workers integration for [Nuxt](https://nuxt.com).
- 🔥 SSR-safe usage of Web Workers
- ✨ auto-imported, zero-configuration
- 💪 fully typed
- 🕒 Automatic shutdown of workers after 1 second of inactivity

## Quick Setup

Expand Down Expand Up @@ -58,14 +59,18 @@ const message = await hi()
> [!TIP]
> Even if your function is synchronous, it will always become async when it is called within a worker because communication with a worker will always be asynchronous.

### Worker Auto-Shutdown

The module automatically shuts down workers after 1 second of inactivity to free up resources. This behavior ensures that your application remains efficient and responsive, without unnecessary resource consumption by idle workers.

## Roadmap

- [x] 📖 basic documentation
- [x] 🧪 client-side test
- [ ] 📦 webpack support
- [ ] ⚠️ type-level + dev warning if non serialisable args are passed
- [ ] 🤝 automatic shared workers with `.shared.ts` suffix
- [ ] 💤 worker auto-shutdown
- [x] 💤 worker auto-shutdown

## 💻 Development

Expand Down
14 changes: 14 additions & 0 deletions src/plugins/unplugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const WorkerPlugin = (opts: WorkerPluginOptions) => createUnplugin(() =>
source += `
const counts = {}
const map = {}
const workerTimeouts = {}

function initWorker (worker, name) {
map[name] = {}
Expand All @@ -96,7 +97,20 @@ function initWorker (worker, name) {
} else {
resolve(e.data.result)
}
// Reset the inactivity timer upon receiving a message
clearTimeout(workerTimeouts[name]);
workerTimeouts[name] = setTimeout(() => {
worker.terminate();
delete workerTimeouts[name];
console.log(\`Worker \${name} terminated due to inactivity.\`);
}, 1000); // 1 second of inactivity
}
// Initialize the inactivity timer
workerTimeouts[name] = setTimeout(() => {
worker.terminate();
delete workerTimeouts[name];
console.log(\`Worker \${name} terminated due to inactivity.\`);
}, 1000); // 1 second of inactivity
return worker
}`
}
Expand Down
14 changes: 14 additions & 0 deletions test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ describe('nuxt-workers', () => {
expect(await page.getByText('Client-side message: Hello from worker!').textContent()).toBeDefined()
await page.close()
})

it('should automatically shut down the worker after 1 second of inactivity', async () => {
const page = await createPage()
const logs: string[] = []
page.on('console', (l) => {
logs.push(l.text())
})
await page.goto(url('/'))
await page.getByText('Load client side message').click()
// wait for more than 1 second to ensure worker shutdown
await new Promise(resolve => setTimeout(resolve, 1500))
expect(logs).toContainEqual(expect.stringContaining('Worker hi terminated due to inactivity.'))
await page.close()
})
})