WIP improve UX [snapshot task 2]
This commit is contained in:
1 parent
3ae47dde8e
commit
cd66321ea6
13 files changed
+703
-87
No files matched your search
+38
-7
@@ -27,6 +27,7 @@ export interface TransferOptions {
|
||||
onProgress?: (bytes: number, size: number) => void;
|
||||
onComplete?: (result: TransferResult) => void;
|
||||
onError?: (error: Error) => void;
|
||||
onClose?: () => void;
|
||||
timeout?: number;
|
||||
}
|
||||
type ControlType = 'ready' | 'meta' | 'accept' | 'ack' | 'end' | 'complete';
|
||||
@@ -48,7 +49,10 @@ export function validateMetadata(meta: unknown): FileMetadata {
|
||||
// One ordered, reliable channel; bounded receive queue and acknowledged send window.
|
||||
export class Transfer {
|
||||
readonly dc: TransferChannel;
|
||||
readonly file: File | null;
|
||||
file: File | null;
|
||||
readonly sending: boolean;
|
||||
peerSupportsRepeat = false;
|
||||
readonly onClose: () => void;
|
||||
readonly onState: NonNullable<TransferOptions['onState']>;
|
||||
readonly onMeta: NonNullable<TransferOptions['onMeta']>;
|
||||
readonly onProgress: NonNullable<TransferOptions['onProgress']>;
|
||||
@@ -77,9 +81,11 @@ export class Transfer {
|
||||
if (!this.meta) throw new Error('Missing file metadata.');
|
||||
return this.meta;
|
||||
}
|
||||
constructor(dc: TransferChannel, { file = null, onState = () => {}, onMeta = () => {}, onProgress = () => {}, onComplete = () => {}, onError = () => {}, timeout = 60000 }: TransferOptions = {}) {
|
||||
constructor(dc: TransferChannel, { file = null, onState = () => {}, onMeta = () => {}, onProgress = () => {}, onComplete = () => {}, onError = () => {}, onClose = () => {}, timeout = 60000 }: TransferOptions = {}) {
|
||||
this.dc = dc;
|
||||
this.file = file;
|
||||
this.sending = !!file;
|
||||
this.onClose = onClose;
|
||||
this.onState = onState;
|
||||
this.onMeta = onMeta;
|
||||
this.onProgress = onProgress;
|
||||
@@ -115,7 +121,7 @@ export class Transfer {
|
||||
this.timer = setTimeout(() => this.fail(new Error('Transfer timed out. Start a new connection.')), ms);
|
||||
}
|
||||
guard(fn: () => void) { try { fn(); } catch (err) { this.fail(err); } }
|
||||
control(type: ControlType, data: Partial<FileMetadata> & { bytes?: number } = {}) {
|
||||
control(type: ControlType, data: Partial<FileMetadata> & { bytes?: number; repeat?: boolean } = {}) {
|
||||
if (this.closed || this.dc.readyState !== 'open') throw new Error('Connection is not open.');
|
||||
const text = JSON.stringify({ v: 2, type, ...data });
|
||||
if (text.length > CONTROL_LIMIT) throw new Error('Control message is too large.');
|
||||
@@ -128,7 +134,7 @@ export class Transfer {
|
||||
}
|
||||
ready() {
|
||||
if (!this.verified || this.dc.readyState !== 'open' || this.closed) return;
|
||||
if (!this.readySent) { this.control('ready'); this.readySent = true; }
|
||||
if (!this.readySent) { this.control('ready', { repeat: true }); this.readySent = true; }
|
||||
if (this.file && this.peerReady && this.state === 'verification') {
|
||||
this.meta = validateMetadata({ name: this.file.name, size: this.file.size, mime: this.file.type || '' });
|
||||
this.setState('waiting-accept');
|
||||
@@ -167,11 +173,14 @@ export class Transfer {
|
||||
const msg: unknown = JSON.parse(data);
|
||||
if (!isRecord(msg) || msg.v !== 2 || typeof msg.type !== 'string') throw new Error('Unsupported transfer protocol.');
|
||||
if (msg.type === 'ready' && !this.peerReady && this.state === 'verification') {
|
||||
this.peerSupportsRepeat = msg.repeat === true;
|
||||
this.peerReady = true; this.ready(); return;
|
||||
}
|
||||
if (!this.verified || !this.peerReady || !this.readySent) throw new Error('File data arrived before verification.');
|
||||
if (msg.type === 'meta' && !this.file && this.state === 'verification') {
|
||||
this.meta = validateMetadata(msg);
|
||||
if (msg.type === 'meta' && !this.sending && (this.state === 'verification' || (this.state === 'done' && this.peerSupportsRepeat))) {
|
||||
const meta = validateMetadata(msg);
|
||||
this.resetFile();
|
||||
this.meta = meta;
|
||||
this.setState('choose-storage'); this.onMeta(this.metadata); this.touch(5 * 60 * 1000); return;
|
||||
}
|
||||
if (msg.type === 'accept' && this.file && this.state === 'waiting-accept') {
|
||||
@@ -191,15 +200,36 @@ export class Transfer {
|
||||
// Treat downloads as opaque files, regardless of the sender's MIME claim.
|
||||
const blob = this.writable ? null : new Blob(this.chunks, { type: 'application/octet-stream' });
|
||||
this.chunks = [];
|
||||
this.writable = null;
|
||||
this.control('complete', { bytes: this.received });
|
||||
this.setState('done'); clearTimeout(this.timer);
|
||||
this.onComplete({ blob, meta: this.metadata }); return;
|
||||
}
|
||||
if (msg.type === 'complete' && this.file && this.state === 'awaiting-receipt' && msg.bytes === this.file.size && this.acked === this.file.size) {
|
||||
this.setState('done'); clearTimeout(this.timer); this.onComplete({ meta: this.metadata }); this.close(); return;
|
||||
this.setState('done'); clearTimeout(this.timer); this.onComplete({ meta: this.metadata });
|
||||
if (!this.peerSupportsRepeat) this.close();
|
||||
return;
|
||||
}
|
||||
throw new Error('Unexpected transfer message.');
|
||||
}
|
||||
get canSendAnother() {
|
||||
return this.sending && this.state === 'done' && !this.closed && this.peerSupportsRepeat && this.dc.readyState === 'open';
|
||||
}
|
||||
private resetFile() {
|
||||
this.received = this.sent = this.acked = 0;
|
||||
this.chunks = [];
|
||||
this.writable = null;
|
||||
this.meta = null;
|
||||
}
|
||||
sendAnother(file: File) {
|
||||
if (!this.canSendAnother) throw new Error('Start a new connection to send another file.');
|
||||
const meta = validateMetadata({ name: file.name, size: file.size, mime: file.type || '' });
|
||||
this.resetFile();
|
||||
this.file = file;
|
||||
this.meta = meta;
|
||||
this.setState('waiting-accept');
|
||||
this.guard(() => { this.control('meta', meta); this.touch(5 * 60 * 1000); });
|
||||
}
|
||||
accept(writable: FileDestination | null = null) {
|
||||
if (this.closed || this.state !== 'choose-storage') throw new Error('File is no longer available.');
|
||||
if (!writable && this.metadata.size > MEMORY_LIMIT) throw new Error('Files over 64 MiB require direct-to-disk saving.');
|
||||
@@ -235,5 +265,6 @@ export class Transfer {
|
||||
if (writable && this.state !== 'done') Promise.resolve().then(() => writable.abort()).catch(() => {});
|
||||
this.dc.onmessage = this.dc.onopen = this.dc.onclose = this.dc.onerror = null;
|
||||
this.dc.close();
|
||||
this.onClose();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user