You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
filepizza/client/stores/DownloadStore.js

64 lines
1.5 KiB
JavaScript

import DownloadActions from '../actions/DownloadActions'
import DownloadFile from '../DownloadFile'
import peer from '../peer'
import alt from '../alt'
import socket from '../socket'
export default alt.createStore(class DownloadStore {
constructor() {
this.bindActions(DownloadActions)
this.status = 'ready'
this.token = null
this.file = null
this.progress = 0
this.on('bootstrap', () => {
if (this.file && !(this.file instanceof DownloadFile)) {
this.file = new DownloadFile(this.file.name,
this.file.size,
this.file.type)
}
})
}
onRequestDownload() {
if (this.status !== 'ready') return
this.status = 'requesting'
socket.emit('download', {
peerID: peer.id,
token: this.token
})
}
onBeginDownload(conn) {
if (this.status !== 'requesting') return
this.status = 'downloading'
conn.on('data', (chunk) => {
if (this.status !== 'downloading') return
this.file.addChunk(chunk)
if (this.file.isComplete()) {
this.setState({ status: 'done', progress: 1 })
this.file.download()
conn.close()
} else {
this.setState({ progress: this.file.getProgress() })
conn.send('more')
}
})
conn.on('close', () => {
if (this.status !== 'downloading') return
this.setState({ status: 'cancelled', progress: 0 })
this.file.clearChunks()
})
}
}, 'DownloadStore')