import { action , makeAutoObservable } from 'mobx' ; import { WebRTCConnectionStatus } from '../types/WebRTC' ; interface IFileHashToMetadataMap { [ fileHash ] : File ; } interface IFilesProgress { [ fileHash ] : number ; } interface IFileHashToDataMap { [ fileHash ] : Array < ArrayBuffer > ; } class DataStore { peerConnectionStatus = false ; webRTCConnectionStatus : WebRTCConnectionStatus = WebRTCConnectionStatus . DISCONNECTED ; fileHashToMetadataMap : IFileHashToMetadataMap = { } ; fileHashToDataMap : IFileHashToDataMap = { } ; filesSendProgress : IFilesProgress = { } ; filesReceiveProgress : IFilesProgress = { } ; isSending = false ; constructor ( ) { makeAutoObservable ( this ) ; } @ action setFileHashToMetadataMap = ( fileHashToMetadataMap ) => { this . fileHashToMetadataMap = fileHashToMetadataMap ; } ; @ action setFileSendProgress = ( fileHash , progress ) => { this . filesSendProgress [ fileHash ] = progress ; } ; @ action setFileReceiveProgress = ( fileHash , progress ) => { this . filesReceiveProgress [ fileHash ] = progress ; } ; @ action addFileData = ( fileHash , buffer ) => { if ( ! this . fileHashToDataMap [ fileHash ] ) { this . fileHashToDataMap [ fileHash ] = [ ] ; } this . fileHashToDataMap [ fileHash ] . push ( buffer ) ; } ; @ action clearFileData = ( fileHash ) => { this . fileHashToDataMap [ fileHash ] = [ ] ; } ; @ action setPeerConnectionStatus = ( peerConnectionStatus ) => { this . peerConnectionStatus = peerConnectionStatus ; } ; @ action setWebRTCConnectionStatus = ( webRTCConnectionStatus ) => { this . webRTCConnectionStatus = webRTCConnectionStatus ; console . log ( 'Connection status: ' , webRTCConnectionStatus ) ; } ; @ action clearFilesData = ( ) => { this . fileHashToMetadataMap = { } ; this . fileHashToDataMap = { } ; this . filesReceiveProgress = { } ; this . filesSendProgress = { } ; } ; @ action setIsSending = ( isSending ) => { this . isSending = isSending ; } ; } export default new DataStore ( ) ;