Skip to content

Commit 7299d2d

Browse files
committed
Add jsdoc to createAddHookMessageChannel
1 parent 3a510c4 commit 7299d2d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ function callHookFn (hookFn, namespace, name, baseDir) {
3434

3535
let sendModulesToLoader
3636

37+
/**
38+
* Creates a message channel with a port that can be used to add hooks to the
39+
* list of exclusively included modules.
40+
*
41+
* This can be used to only wrap modules that are Hook'ed, however modules need
42+
* to be hooked before they are imported.
43+
*
44+
* ```ts
45+
* import { register } from 'module'
46+
* import { Hook, createAddHookMessageChannel } from 'import-in-the-middle'
47+
*
48+
* const { registerOptions, waitForAllMessagesAcknowledged } = createAddHookMessageChannel()
49+
*
50+
* register('import-in-the-middle/hook.mjs', import.meta.url, registerOptions)
51+
*
52+
* Hook(['fs'], (exported, name, baseDir) => {
53+
* // Instrument the fs module
54+
* })
55+
*
56+
* // Ensure that the loader has acknowledged all the modules
57+
* // before we allow execution to continue
58+
* await waitForAllMessagesAcknowledged()
59+
* ```
60+
*/
3761
function createAddHookMessageChannel () {
3862
const { port1, port2 } = new MessageChannel()
3963
let pendingAckCount = 0

0 commit comments

Comments
 (0)