import { EVT } from './EVT' ; import { CommonResult , ResultMeta , Result } from './Store.d' ; class Store { constructor ( ) { this . bindEvents ( ) ; } public resultMeta : ResultMeta [ ] = [ ] ; public result : Result [ ] = [ ] ; public readonly defaultFileName = '{user}/{title}/{index}' ; public crawlCompleteTime : Date = new Date ( ) ; public readonly fileType = { image : [ 'jpg' , 'jpeg' , 'png' , 'gif' ] , music : [ 'wav' , 'mp3' , 'flac' ] , video : [ 'mp4' , 'mov' , 'avi' ] , compressed : [ 'zip' ] , ps : [ 'psd' , 'clip' ] , other : [ 'txt' , 'pdf' ] , } ; private bindEvents ( ) { const allowWorkTrue = [ EVT . events . crawlFinish , EVT . events . crawlEmpty , EVT . events . crawlError , EVT . events . downloadPause , EVT . events . downloadStop , ] ; allowWorkTrue . forEach ( ( type ) => { window . addEventListener ( type , ( ) => { this . states . allowWork = true ; } ) ; } ) ; const allowWorkFalse = [ EVT . events . crawlStart , EVT . events . downloadStart ] ; allowWorkFalse . forEach ( ( type ) => { window . addEventListener ( type , ( ) => { this . states . allowWork = false ; } ) ; } ) ; window . addEventListener ( EVT . events . crawlStart , ( ) => { this . resetResult ( ) ; } ) ; window . addEventListener ( EVT . events . downloadComplete , ( ) => { this . resetStates ( ) ; } ) ; } private getCommonData ( data ) { return { postId : data . postId , type : data . type , title : data . title , date : data . date , fee : data . fee , user : data . user , uid : data . uid , tags : data . tags , } ; } public addResult ( data ) { this . resultMeta . push ( data ) ; if ( data . links . text . length > 0 ) { console . log ( data . links . text ) ; const text = data . links . text . join ( '\r\n' ) ; const blob = new Blob ( [ text ] , { type : 'text/plain' , } ) ; data . links . url = URL . createObjectURL ( blob ) ; data . links . size = blob . size ; const result = Object . assign ( this . getCommonData ( data ) , data . links ) ; this . result . push ( result ) ; } const files = data . files ; for ( const fileData of files ) { const result = Object . assign ( this . getCommonData ( data ) , fileData ) ; this . result . push ( result ) ; } } public states = { allowWork : true , quickDownload : false , } ; public resetResult ( ) { this . resultMeta = [ ] ; this . result = [ ] ; } public resetStates ( ) { this . states . allowWork = true ; this . states . quickDownload = false ; } } const store = new Store ( ) ; export { store } ;