Skip to content

Commit

Permalink
adding worker thread messaging (try)
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed May 16, 2023
1 parent 95da39b commit 0a2a93a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/esmockCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const esmockCache = {
}

const esmockTreeIdSet = (key, keylong) => (
(typeof global.postMessageEsmk === 'function')
&& global.postMessageEsmk({ key, keylong }),
global.mockKeys[String(key)] = keylong)

const esmockTreeIdGet = key => (
Expand Down
10 changes: 9 additions & 1 deletion src/esmockLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import process from 'process'
import { isMainThread } from 'node:worker_threads'
import urlDummy from './esmockDummy.js'
import esmockErr from './esmockErr.js'

Expand All @@ -16,6 +17,13 @@ const withHashRe = /.*#-#/
const isesmRe = /isesm=true/
const isnotfoundRe = /isfound=false/

const globalPreload = isMainThread && (({ port }) => (
port.addEventListener('message', ev => (
global.mockKeys[ev.data.key] = ev.data.keylong)),
port.unref(),
'global.postMessageEsmk = d => port.postMessage(d)'
))

// new versions of node: when multiple loaders are used and context
// is passed to nextResolve, the process crashes in a recursive call
// see: /esmock/issues/#48
Expand Down Expand Up @@ -123,4 +131,4 @@ const load = async (url, context, nextLoad) => {
// node lt 16.12 require getSource, node gte 16.12 warn remove getSource
const getSource = isLT1612 && load

export {load, resolve, getSource, loaderIsVerified}
export {load, resolve, getSource, loaderIsVerified, globalPreload}

0 comments on commit 0a2a93a

Please sign in to comment.