@@ -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 ) ;
@@ -175,8 +184,9 @@ export const useClient: UseClient = (
175184 */
176185 if ( queuedListeners . current [ clientId ] ) {
177186 Object . keys ( queuedListeners . current [ clientId ] ) . forEach ( ( listenerId ) => {
178- const listener = queuedListeners . current [ clientId ] [ listenerId ] ;
179- const unsubscribe = client . listen ( listener ) as ( ) => void ;
187+ const { listener, opts } =
188+ queuedListeners . current [ clientId ] [ listenerId ] ;
189+ const unsubscribe = client . listen ( listener , opts ) as ( ) => void ;
180190 unsubscribeClientListeners . current [ clientId ] [ listenerId ] =
181191 unsubscribe ;
182192 } ) ;
@@ -189,8 +199,8 @@ export const useClient: UseClient = (
189199 * Register global listeners
190200 */
191201 const globalListeners = Object . entries ( queuedListeners . current . global ) ;
192- globalListeners . forEach ( ( [ listenerId , listener ] ) => {
193- const unsubscribe = client . listen ( listener ) as ( ) => void ;
202+ globalListeners . forEach ( ( [ listenerId , { listener, opts } ] ) => {
203+ const unsubscribe = client . listen ( listener , opts ) as ( ) => void ;
194204 unsubscribeClientListeners . current [ clientId ] [ listenerId ] = unsubscribe ;
195205
196206 /**
@@ -394,13 +404,17 @@ export const useClient: UseClient = (
394404 }
395405 } ;
396406
397- const addListener = (
398- listener : ListenerFunction ,
399- clientId ?: string
407+ const addListener = < Type extends SandpackMessageType = SandpackMessageType > (
408+ listener : ListenerFunction < Type > ,
409+ clientId ?: string ,
410+ opts ?: ListenerOptions < Type >
400411 ) : UnsubscribeFunction => {
401412 if ( clientId ) {
402413 if ( clients . current [ clientId ] ) {
403- const unsubscribeListener = clients . current [ clientId ] . listen ( listener ) ;
414+ const unsubscribeListener = clients . current [ clientId ] . listen (
415+ listener ,
416+ opts
417+ ) ;
404418
405419 return unsubscribeListener ;
406420 } else {
@@ -415,7 +429,10 @@ export const useClient: UseClient = (
415429 unsubscribeClientListeners . current [ clientId ] =
416430 unsubscribeClientListeners . current [ clientId ] || { } ;
417431
418- queuedListeners . current [ clientId ] [ listenerId ] = listener ;
432+ queuedListeners . current [ clientId ] [ listenerId ] = {
433+ listener : listener as any as ListenerFunction ,
434+ opts : opts as any as ListenerOptions ,
435+ } ;
419436
420437 const unsubscribeListener = ( ) : void => {
421438 if ( queuedListeners . current [ clientId ] [ listenerId ] ) {
@@ -439,7 +456,10 @@ export const useClient: UseClient = (
439456 } else {
440457 // Push to the **global** queue
441458 const listenerId = generateRandomId ( ) ;
442- queuedListeners . current . global [ listenerId ] = listener ;
459+ queuedListeners . current . global [ listenerId ] = {
460+ listener : listener as any as ListenerFunction ,
461+ opts : opts as any as ListenerOptions ,
462+ } ;
443463
444464 // Add to the current clients
445465 const clientsList = Object . values ( clients . current ) ;
0 commit comments