11
22import type * as ws from "ws"
3- import { Trash , ev } from "@e280/stz"
3+ import { disposer , ev } from "@e280/stz"
44
55import { Conduit } from "./conduit.js"
66import { JsonRpc } from "../../../core/json-rpc.js"
@@ -13,7 +13,9 @@ type Message = InfraMessage | RpcMessage
1313export class WebsocketConduit extends Conduit {
1414 socket : WebSocket | ws . WebSocket
1515 pingponger : Pingponger
16- #trash = new Trash ( )
16+
17+ /** clean up this conduit, detaching socket listeners. does not close the socket. */
18+ dispose = disposer ( )
1719
1820 constructor ( options : {
1921 socket : WebSocket | ws . WebSocket
@@ -26,13 +28,13 @@ export class WebsocketConduit extends Conduit {
2628 this . socket = options . socket
2729
2830 // sending rpc messages
29- this . #trash . add (
31+ this . dispose . schedule (
3032 this . sendRequest . sub ( this . #sendRpc) ,
3133 this . sendResponse . sub ( this . #sendRpc) ,
3234 )
3335
3436 // listening to socket events
35- this . #trash . add (
37+ this . dispose . schedule (
3638 ev ( this . socket , {
3739 error : error => {
3840 this . dispose ( )
@@ -55,7 +57,7 @@ export class WebsocketConduit extends Conduit {
5557 } )
5658
5759 // establish pingponger heartbeat
58- this . #trash . add (
60+ this . dispose . schedule (
5961 this . pingponger . heartbeat ( ( ) => {
6062 if ( this . socket . readyState === 1 )
6163 this . socket . close ( )
@@ -86,10 +88,5 @@ export class WebsocketConduit extends Conduit {
8688 throw new Error ( "message listener" )
8789 }
8890 }
89-
90- /** clean up this conduit, detaching socket listeners. does not close the socket. */
91- dispose ( ) {
92- this . #trash. dispose ( )
93- }
9491}
9592
0 commit comments