import React from 'react' ; import ReactDOM from 'react-dom' ; import { Type , NoArgsReturnVoidFunction , Message } from './types' ; import Toast from './toast' ; import Queue from './queue' ; import eventManager from './event' ; const queue = new Queue ( ) ; eventManager . subscribe ( 'lt#popmessage' , ( { id , type , content , duration , onClose } ) => { const container = document . createElement ( 'div' ) ; document . body . appendChild ( container ) ; ReactDOM . render ( < Toast id = { id } type = { type } content = { content } duration = { duration } onClose = { ( ) : void => { ReactDOM . unmountComponentAtNode ( container ) ; document . body . removeChild ( container ) ; onClose && onClose ( ) ; queue . shift ( ) ; if ( queue . length > 0 ) { eventManager . publish ( 'lt#popmessage' , queue . getFirstMessage ( ) ) ; } } } />, container); } ) ; function notice ( type , content , duration ? , onClose ? ) { queue . push ( { type , content , duration , onClose } ) ; if ( queue . length === 1 ) { eventManager . publish ( 'lt#popmessage' , queue . getFirstMessage ( ) ) ; return ; } if ( queue . length > 1 ) { const message = queue . getFirstMessage ( ) ; if ( message . type === 'loading' ) { eventManager . publish ( 'lt#exit' , message . id ) ; } } } const index = { info ( content , duration ? , onClose ? ) { notice ( 'info' , content , duration , onClose ) ; } , success ( content , duration ? , onClose ? ) { notice ( 'success' , content , duration , onClose ) ; } , fail ( content , duration ? , onClose ? ) { notice ( 'fail' , content , duration , onClose ) ; } , loading ( content , onClose ? ) { notice ( 'loading' , content , 0 , onClose ) ; } , hide ( ) { if ( queue . length > 0 ) { eventManager . publish ( 'lt#exit' , queue . getFirstMessage ( ) . id ) ; } } , } ; export default index ;