Skip to content

Commit a4bd8a4

Browse files
committed
Write fewer event handlers in the FFI
All of the event handlers had the same format, and really were doing the same thing. There's a reason that `EventEmitter#on` is a thing. We can use that to our advantage and write less JS!
1 parent a25e815 commit a4bd8a4

File tree

2 files changed

+31
-80
lines changed

2 files changed

+31
-80
lines changed

src/Node/Net.js

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,12 @@ exports.localPortImpl = function (socket) {
7676
return socket.localPort;
7777
};
7878

79-
exports.onCloseServerImpl = function (server, callback) {
80-
server.on("close", callback);
79+
exports.onServerImpl = function (event, server, callback) {
80+
server.on(event, callback);
8181
};
8282

83-
exports.onCloseSocketImpl = function (socket, callback) {
84-
socket.on("close", callback);
85-
};
86-
87-
exports.onConnectImpl = function (socket, callback) {
88-
socket.on("connect", callback);
89-
};
90-
91-
exports.onConnectionImpl = function (server, callback) {
92-
server.on("connection", callback);
83+
exports.onSocketImpl = function (event, socket, callback) {
84+
socket.on(event, callback);
9385
};
9486

9587
exports.onDataImpl = function (socket, callbackBuffer, callbackString) {
@@ -102,38 +94,6 @@ exports.onDataImpl = function (socket, callbackBuffer, callbackString) {
10294
});
10395
};
10496

105-
exports.onDrainImpl = function (socket, callback) {
106-
socket.on("drain", callback);
107-
};
108-
109-
exports.onEndImpl = function (socket, callback) {
110-
socket.on("end", callback);
111-
};
112-
113-
exports.onErrorServerImpl = function (server, callback) {
114-
server.on("error", callback);
115-
};
116-
117-
exports.onErrorSocketImpl = function (socket, callback) {
118-
socket.on("error", callback);
119-
};
120-
121-
exports.onListeningImpl = function (server, callback) {
122-
server.on("listening", callback);
123-
};
124-
125-
exports.onLookupImpl = function (socket, callback) {
126-
socket.on("lookup", callback);
127-
};
128-
129-
exports.onReadyImpl = function (socket, callback) {
130-
socket.on("ready", callback);
131-
};
132-
133-
exports.onTimeoutImpl = function (socket, callback) {
134-
socket.on("timeout", callback);
135-
};
136-
13797
exports.pauseImpl = function (socket) {
13898
socket.pause();
13999
};

src/Node/Net.purs

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -464,44 +464,42 @@ localPort socket = do
464464
port <- runEffectFn1 localPortImpl socket
465465
pure (toMaybe port)
466466

467-
foreign import onCloseServerImpl :: EffectFn2 Server (Effect Unit) Unit
468-
469467
-- | Attaches the callback as a listener to the `'close'` event.
470468
-- |
471469
-- | `'close'` is emitted when a close occurs.
472470
-- | Will not be emitted until all connections have ended.
473471
onCloseServer :: Server -> Effect Unit -> Effect Unit
474-
onCloseServer server callback = runEffectFn2 onCloseServerImpl server callback
475-
476-
foreign import onCloseSocketImpl :: EffectFn2 Socket (EffectFn1 Boolean Unit) Unit
472+
onCloseServer server callback =
473+
runEffectFn3 onServerImpl "close" server callback
477474

478475
-- | Attaches the callback as a listener to the `'close'` event.
479476
-- | The `Boolean` represents whether an error happened during transmission.
480477
-- |
481478
-- | `'close'` is emitted when an close occurs.
482479
onCloseSocket :: Socket -> (Boolean -> Effect Unit) -> Effect Unit
483480
onCloseSocket socket callback =
484-
runEffectFn2
485-
onCloseSocketImpl
481+
runEffectFn3
482+
onSocketImpl
483+
"close"
486484
socket
487485
(mkEffectFn1 \hadError -> callback hadError)
488486

489-
foreign import onConnectImpl :: EffectFn2 Socket (Effect Unit) Unit
490-
491487
-- | Attaches the callback as a listener to the `'connect'` event.
492488
-- |
493489
-- | `'connect'` is emitted when a new connection is successfully establed.
494490
onConnect :: Socket -> Effect Unit -> Effect Unit
495-
onConnect socket callback = runEffectFn2 onConnectImpl socket callback
496-
497-
foreign import onConnectionImpl :: EffectFn2 Server (EffectFn1 Socket Unit) Unit
491+
onConnect socket callback = runEffectFn3 onSocketImpl "connect" socket callback
498492

499493
-- | Attaches the callback as a listener to the `'connection'` event.
500494
-- |
501495
-- | `'connection'` is emitted when a new connection is made.
502496
onConnection :: Server -> (Socket -> Effect Unit) -> Effect Unit
503497
onConnection server callback =
504-
runEffectFn2 onConnectionImpl server (mkEffectFn1 \socket -> callback socket)
498+
runEffectFn3
499+
onServerImpl
500+
"connection"
501+
server
502+
(mkEffectFn1 \socket -> callback socket)
505503

506504
foreign import onDataImpl :: EffectFn3 Socket (EffectFn1 Buffer Unit) (EffectFn1 String Unit) Unit
507505

@@ -517,79 +515,72 @@ onData socket callback =
517515
(mkEffectFn1 \buffer -> callback $ Left buffer)
518516
(mkEffectFn1 \str -> callback $ Right str)
519517

520-
foreign import onDrainImpl :: EffectFn2 Socket (Effect Unit) Boolean
521-
522518
-- | Attaches the callback as a listener to the `'drain'` event.
523519
-- |
524520
-- | `'drain'` is emitted when the write buffer is empty.
525521
onDrain :: Socket -> Effect Unit -> Effect Boolean
526-
onDrain socket callback = runEffectFn2 onDrainImpl socket callback
527-
528-
foreign import onEndImpl :: EffectFn2 Socket (Effect Unit) Unit
522+
onDrain socket callback = runEffectFn3 onSocketImpl "drain" socket callback
529523

530524
-- | Attaches the callback as a listener to the `'end'` event.
531525
-- |
532526
-- | `'end'` is emitted when the other end of the `Socket` sends a `FIN` packet.
533527
onEnd :: Socket -> Effect Unit -> Effect Unit
534-
onEnd socket callback = runEffectFn2 onEndImpl socket callback
535-
536-
foreign import onErrorServerImpl :: EffectFn2 Server (EffectFn1 Error Unit) Unit
528+
onEnd socket callback = runEffectFn3 onSocketImpl "end" socket callback
537529

538530
-- | Attaches the callback as a listener to the `'error'` event.
539531
-- |
540532
-- | `'error'` is emitted when an error occurs.
541533
onErrorServer :: Server -> (Error -> Effect Unit) -> Effect Unit
542534
onErrorServer server callback =
543-
runEffectFn2 onErrorServerImpl server (mkEffectFn1 \error -> callback error)
544-
545-
foreign import onErrorSocketImpl :: EffectFn2 Socket (EffectFn1 Error Unit) Unit
535+
runEffectFn3
536+
onServerImpl
537+
"error"
538+
server
539+
(mkEffectFn1 \error -> callback error)
546540

547541
-- | Attaches the callback as a listener to the `'error'` event.
548542
-- |
549543
-- | `'error'` is emitted when an error occurs.
550544
-- | `'close'` is emitted directly after this event.
551545
onErrorSocket :: Socket -> (Error -> Effect Unit) -> Effect Unit
552546
onErrorSocket socket callback =
553-
runEffectFn2 onErrorSocketImpl socket (mkEffectFn1 \error -> callback error)
554-
555-
foreign import onListeningImpl :: EffectFn2 Server (Effect Unit) Unit
547+
runEffectFn3 onSocketImpl "error" socket (mkEffectFn1 \err -> callback err)
556548

557549
-- | Attaches the callback as a listener to the `'listening'` event.
558550
-- |
559551
-- | `'listening'` is emitted when the `Server` has been bound.
560552
onListening :: Server -> Effect Unit -> Effect Unit
561-
onListening server callback = runEffectFn2 onListeningImpl server callback
562-
563-
foreign import onLookupImpl :: EffectFn2 Socket (EffectFn4 (Nullable Error) (Nullable String) (Nullable (Nullable Int)) (Nullable String) Unit) Unit
553+
onListening server callback =
554+
runEffectFn3 onServerImpl "listening" server callback
564555

565556
-- | Attaches the callback as a listener to the `'lookup'` event.
566557
-- |
567558
-- | `'lookup'` is emitted after resolving the hostname but before connecting.
568559
onLookup :: Socket -> (Either Error Lookup -> Effect Unit) -> Effect Unit
569560
onLookup socket callback =
570-
runEffectFn2 onLookupImpl socket $ mkEffectFn4 \err' address'' family' host' ->
561+
runEffectFn3 onSocketImpl "lookup" socket $ mkEffectFn4 \err' address'' family' host' ->
571562
case toMaybe err', toMaybe address'', toMaybe family', toMaybe host' of
572563
Just err, _, _, _ -> callback (Left err)
573564
Nothing, Just address', Just family, Just host ->
574565
callback (Right { address: address', family: toMaybe family, host })
575566
_, _, _, _ -> mempty
576567

577-
foreign import onReadyImpl :: EffectFn2 Socket (Effect Unit) Unit
578-
579568
-- | Attaches the callback as a listener to the `'ready'` event.
580569
-- |
581570
-- | `'ready'` is emitted when the `Socket` is ready to be used.
582571
onReady :: Socket -> Effect Unit -> Effect Unit
583-
onReady socket callback = runEffectFn2 onReadyImpl socket callback
572+
onReady socket callback = runEffectFn3 onSocketImpl "ready" socket callback
573+
574+
foreign import onServerImpl :: forall f. EffectFn3 String Server (f Unit) Unit
584575

585-
foreign import onTimeoutImpl :: EffectFn2 Socket (Effect Unit) Unit
576+
foreign import onSocketImpl :: forall a f. EffectFn3 String Socket (f Unit) a
586577

587578
-- | Attaches the callback as a listener to the `'timeout'` event.
588579
-- |
589580
-- | `'timeout'` is emitted if the `Socket` times out from inactivity.
590581
-- | The `Socket` is still open and should be manually closed.
591582
onTimeout :: Socket -> Effect Unit -> Effect Unit
592-
onTimeout socket callback = runEffectFn2 onTimeoutImpl socket callback
583+
onTimeout socket callback = runEffectFn3 onSocketImpl "timeout" socket callback
593584

594585
foreign import pauseImpl :: EffectFn1 Socket Unit
595586

0 commit comments

Comments
 (0)