 : irc-send ( irc-message -- ) irc> out-messages>> mailbox-put ;
 : listener> ( name -- listener/f ) irc> listeners>> at ;
 
-: maybe-mailbox-get ( mailbox quot: ( irc-message -- ) -- )
-    [ dup mailbox-empty? [ drop 0.1 sleep ] ] dip '[ mailbox-get @ ] if ; inline
 
 GENERIC: to-listener ( message obj -- )
 

    [ (reader-loop) ] [ handle-disconnect ] recover t ;

: writer-loop ( -- ? )
    irc> out-messages>> [ handle-outgoing-irc ] maybe-mailbox-get t ;

! ======================================
! Processing loops
! ======================================

: in-multiplexer-loop ( -- ? )
    irc> in-messages>> [ handle-incoming-irc ] maybe-mailbox-get t ;

: strings>privmsg ( name string -- privmsg )
    privmsg new [ (>>trailing) ] keep [ (>>name) ] keep ;


: listener-loop ( name -- ? )
    dup listener> [
        out-messages>> mailbox-get
        maybe-annotate-with-name
        irc> out-messages>> mailbox-put
        t
    ] [ drop f ] if* ;

: spawn-irc-loop ( quot: ( -- ? ) name -- )
