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

65 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 = 'offline';
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';
let chunkSize = conn.metadata.chunkSize;
let i = 0;
conn.on('data', (data) => {
if (this.status !== 'downloading') return;
this.file.addPacket(data);
i++;
if (this.file.isComplete()) {
this.setState({ status: 'done', progress: 1 });
this.file.download();
conn.close();
} else {
this.setState({ progress: this.file.getProgress() });
if (i % chunkSize === 0) conn.send('more');
}
});
conn.on('close', () => {
if (this.status !== 'downloading') return;
this.setState({ status: 'cancelled', progress: 0 });
this.file.clearPackets();
});
}
}, 'DownloadStore')