From 2c601066a6e4eebb65df5f4bd175c4d9f500401f Mon Sep 17 00:00:00 2001 From: Aminejv Date: Fri, 24 Sep 2021 17:37:44 +0100 Subject: [PATCH] feat(web-workers): - add useWorker to interact with web workers declaratively - update next to 11.1.2 to support web-workers - update next config to ignore eslint errors during build --- common/hooks.js | 27 +++++++++++++++++++++++++++ next.config.js | 5 +++++ package.json | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/common/hooks.js b/common/hooks.js index 5e88093d..10a0538f 100644 --- a/common/hooks.js +++ b/common/hooks.js @@ -424,3 +424,30 @@ export const useLockScroll = ({ lock = true } = {}) => { return () => (document.body.style.overflow = "visible"); }, [lock]); }; + +export const useWorker = ({ onStart, onMessage, onError, path } = {}, dependencies = []) => { + const workerRef = React.useRef(); + + const onStartRef = React.useRef(); + onStartRef.current = onStart; + + const onMessageRef = React.useRef(); + onMessageRef.current = onMessage; + + const onErrorRef = React.useRef(); + onErrorRef.current = onError; + + React.useEffect(() => { + const worker = new Worker(new URL(path, import.meta.url)); + if (!worker) return; + + workerRef.current = worker; + worker.onmessage = onMessageRef.current; + worker.onerror = onErrorRef.current; + + onStartRef.current(worker); + return () => worker?.terminate(); + }, dependencies); + + return workerRef.current; +}; diff --git a/next.config.js b/next.config.js index 5b9bf30f..06b490dc 100644 --- a/next.config.js +++ b/next.config.js @@ -2,6 +2,11 @@ const withOffline = require("next-offline"); const nextConfig = { webpack5: true, + eslint: { + // Warning: This allows production builds to successfully complete even if + // your project has ESLint errors. + ignoreDuringBuilds: true, + }, }; module.exports = withOffline(nextConfig); diff --git a/package.json b/package.json index 4a2d4c89..b6c09ba8 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "moment": "^2.29.1", "morgan": "^1.10.0", "multihashing-async": "^2.1.2", - "next": "^10.0.7", + "next": "^11.1.2", "next-offline": "^5.0.5", "oauth": "^0.9.15", "pg": "^8.5.1",