Skip to content

Commit 0a2a93a

Browse files
committedMay 16, 2023
adding worker thread messaging (try)
1 parent 95da39b commit 0a2a93a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed
 

‎src/esmockCache.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const esmockCache = {
77
}
88

99
const esmockTreeIdSet = (key, keylong) => (
10+
(typeof global.postMessageEsmk === 'function')
11+
&& global.postMessageEsmk({ key, keylong }),
1012
global.mockKeys[String(key)] = keylong)
1113

1214
const esmockTreeIdGet = key => (

‎src/esmockLoader.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import process from 'process'
2+
import { isMainThread } from 'node:worker_threads'
23
import urlDummy from './esmockDummy.js'
34
import esmockErr from './esmockErr.js'
45

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

20+
const globalPreload = isMainThread && (({ port }) => (
21+
port.addEventListener('message', ev => (
22+
global.mockKeys[ev.data.key] = ev.data.keylong)),
23+
port.unref(),
24+
'global.postMessageEsmk = d => port.postMessage(d)'
25+
))
26+
1927
// new versions of node: when multiple loaders are used and context
2028
// is passed to nextResolve, the process crashes in a recursive call
2129
// see: /esmock/issues/#48
@@ -123,4 +131,4 @@ const load = async (url, context, nextLoad) => {
123131
// node lt 16.12 require getSource, node gte 16.12 warn remove getSource
124132
const getSource = isLT1612 && load
125133

126-
export {load, resolve, getSource, loaderIsVerified}
134+
export {load, resolve, getSource, loaderIsVerified, globalPreload}

0 commit comments

Comments
 (0)
Please sign in to comment.