@@ -6,6 +6,8 @@ import type {
66 SandpackMessage ,
77 UnsubscribeFunction ,
88 SandpackClient ,
9+ SandpackMessageType ,
10+ ListenerOptions ,
911} from "@codesandbox/sandpack-client" ;
1012import {
1113 loadSandpackClient ,
@@ -49,9 +51,10 @@ export interface UseClientOperations {
4951 clientPropsOverride ?: ClientPropsOverride
5052 ) => Promise < void > ;
5153 registerReactDevTools : ( value : ReactDevToolsMode ) => void ;
52- addListener : (
54+ addListener : < Type extends SandpackMessageType = SandpackMessageType > (
5355 listener : ListenerFunction ,
54- clientId ?: string
56+ clientId ?: string ,
57+ opts ?: ListenerOptions < Type >
5558 ) => UnsubscribeFunction ;
5659 dispatchMessage : ( message : SandpackMessage , clientId ?: string ) => void ;
5760 lazyAnchorRef : React . RefObject < HTMLDivElement > ;
@@ -61,7 +64,10 @@ export interface UseClientOperations {
6164 Record < string , Record < string , UnsubscribeFunction > >
6265 > ;
6366 queuedListenersRef : React . MutableRefObject <
64- Record < string , Record < string , ListenerFunction > >
67+ Record <
68+ string ,
69+ Record < string , { listener : ListenerFunction ; opts ?: ListenerOptions } >
70+ >
6571 > ;
6672}
6773
@@ -106,7 +112,10 @@ export const useClient: UseClient = (
106112 > ( { } ) ;
107113 const unsubscribe = useRef < ( ) => void | undefined > ( ) ;
108114 const queuedListeners = useRef <
109- Record < string , Record < string , ListenerFunction > >
115+ Record <
116+ string ,
117+ Record < string , { listener : ListenerFunction ; opts ?: ListenerOptions } >
118+ >
110119 > ( { global : { } } ) ;
111120 const debounceHook = useRef < number | undefined > ( ) ;
112121 const loadingScreenRegisteredRef = useRef < boolean > ( true ) ;
@@ -185,8 +194,9 @@ export const useClient: UseClient = (
185194 */
186195 if ( queuedListeners . current [ clientId ] ) {
187196 Object . keys ( queuedListeners . current [ clientId ] ) . forEach ( ( listenerId ) => {
188- const listener = queuedListeners . current [ clientId ] [ listenerId ] ;
189- const unsubscribe = client . listen ( listener ) as ( ) => void ;
197+ const { listener, opts } =
198+ queuedListeners . current [ clientId ] [ listenerId ] ;
199+ const unsubscribe = client . listen ( listener , opts ) as ( ) => void ;
190200 unsubscribeClientListeners . current [ clientId ] [ listenerId ] =
191201 unsubscribe ;
192202 } ) ;
@@ -199,8 +209,8 @@ export const useClient: UseClient = (
199209 * Register global listeners
200210 */
201211 const globalListeners = Object . entries ( queuedListeners . current . global ) ;
202- globalListeners . forEach ( ( [ listenerId , listener ] ) => {
203- const unsubscribe = client . listen ( listener ) as ( ) => void ;
212+ globalListeners . forEach ( ( [ listenerId , { listener, opts } ] ) => {
213+ const unsubscribe = client . listen ( listener , opts ) as ( ) => void ;
204214 unsubscribeClientListeners . current [ clientId ] [ listenerId ] = unsubscribe ;
205215
206216 /**
@@ -392,13 +402,17 @@ export const useClient: UseClient = (
392402 }
393403 } ;
394404
395- const addListener = (
396- listener : ListenerFunction ,
397- clientId ?: string
405+ const addListener = < Type extends SandpackMessageType = SandpackMessageType > (
406+ listener : ListenerFunction < Type > ,
407+ clientId ?: string ,
408+ opts ?: ListenerOptions < Type >
398409 ) : UnsubscribeFunction => {
399410 if ( clientId ) {
400411 if ( clients . current [ clientId ] ) {
401- const unsubscribeListener = clients . current [ clientId ] . listen ( listener ) ;
412+ const unsubscribeListener = clients . current [ clientId ] . listen (
413+ listener ,
414+ opts
415+ ) ;
402416
403417 return unsubscribeListener ;
404418 } else {
@@ -413,7 +427,10 @@ export const useClient: UseClient = (
413427 unsubscribeClientListeners . current [ clientId ] =
414428 unsubscribeClientListeners . current [ clientId ] || { } ;
415429
416- queuedListeners . current [ clientId ] [ listenerId ] = listener ;
430+ queuedListeners . current [ clientId ] [ listenerId ] = {
431+ listener : listener as any as ListenerFunction ,
432+ opts : opts as any as ListenerOptions ,
433+ } ;
417434
418435 const unsubscribeListener = ( ) : void => {
419436 if ( queuedListeners . current [ clientId ] [ listenerId ] ) {
@@ -437,7 +454,10 @@ export const useClient: UseClient = (
437454 } else {
438455 // Push to the **global** queue
439456 const listenerId = generateRandomId ( ) ;
440- queuedListeners . current . global [ listenerId ] = listener ;
457+ queuedListeners . current . global [ listenerId ] = {
458+ listener : listener as any as ListenerFunction ,
459+ opts : opts as any as ListenerOptions ,
460+ } ;
441461
442462 // Add to the current clients
443463 const clientsList = Object . values ( clients . current ) ;
0 commit comments