@johnhenry/browsermesh-sync 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/files.mjs +63 -2
package/package.json
CHANGED
package/src/files.mjs
CHANGED
|
@@ -308,6 +308,12 @@ export class MeshFileTransfer {
|
|
|
308
308
|
// Callbacks
|
|
309
309
|
#onOffer = null;
|
|
310
310
|
#onProgress = null;
|
|
311
|
+
|
|
312
|
+
/** @type {Set<Function>} multi-listener progress subscribers */
|
|
313
|
+
#progressListeners = new Set();
|
|
314
|
+
|
|
315
|
+
/** @type {Map<string, 'send'|'receive'>} transferId → local role */
|
|
316
|
+
#directions = new Map();
|
|
311
317
|
#onComplete = null;
|
|
312
318
|
#onSend = null;
|
|
313
319
|
|
|
@@ -323,6 +329,28 @@ export class MeshFileTransfer {
|
|
|
323
329
|
onComplete(cb) { this.#onComplete = cb; return this; }
|
|
324
330
|
onSend(cb) { this.#onSend = cb; return this; }
|
|
325
331
|
|
|
332
|
+
/**
|
|
333
|
+
* Subscribe to transfer progress. Unlike the single-slot `onProgress`, any
|
|
334
|
+
* number of listeners may subscribe and none displaces another.
|
|
335
|
+
*
|
|
336
|
+
* Listeners receive `{ transferId, direction, bytesDone, bytesTotal,
|
|
337
|
+
* percentComplete, transferRate, state }` where `direction` is `'send'`
|
|
338
|
+
* (this instance created the offer) or `'receive'` (it accepted one) and
|
|
339
|
+
* `state` is the transfer status. An event fires after every chunk
|
|
340
|
+
* (`'transferring'`) and once more on reaching `'completed'` or
|
|
341
|
+
* `'cancelled'`. Events are NOT throttled; throttle in the consumer.
|
|
342
|
+
* A throwing listener is isolated and does not affect other listeners or
|
|
343
|
+
* the transfer.
|
|
344
|
+
*
|
|
345
|
+
* @param {(event: object) => void} fn
|
|
346
|
+
* @returns {() => void} unsubscribe
|
|
347
|
+
*/
|
|
348
|
+
subscribeProgress(fn) {
|
|
349
|
+
if (typeof fn !== 'function') throw new TypeError('subscribeProgress requires a function');
|
|
350
|
+
this.#progressListeners.add(fn);
|
|
351
|
+
return () => { this.#progressListeners.delete(fn); };
|
|
352
|
+
}
|
|
353
|
+
|
|
326
354
|
/** Access the chunk store. */
|
|
327
355
|
get store() { return this.#store; }
|
|
328
356
|
|
|
@@ -343,6 +371,7 @@ export class MeshFileTransfer {
|
|
|
343
371
|
expires: opts.expires,
|
|
344
372
|
});
|
|
345
373
|
this.#offers.set(offer.transferId, offer);
|
|
374
|
+
this.#directions.set(offer.transferId, 'send');
|
|
346
375
|
const fileSizes = offer.files.map(f => f.size);
|
|
347
376
|
const state = new TransferState({
|
|
348
377
|
transferId: offer.transferId,
|
|
@@ -373,6 +402,7 @@ export class MeshFileTransfer {
|
|
|
373
402
|
throw new Error('Transfer offer has expired');
|
|
374
403
|
}
|
|
375
404
|
this.#offers.set(offer.transferId, offer);
|
|
405
|
+
this.#directions.set(offer.transferId, 'receive');
|
|
376
406
|
const fileSizes = offer.files.map(f => f.size);
|
|
377
407
|
const state = new TransferState({
|
|
378
408
|
transferId: offer.transferId,
|
|
@@ -403,6 +433,7 @@ export class MeshFileTransfer {
|
|
|
403
433
|
const state = this.#states.get(transferId);
|
|
404
434
|
if (state) {
|
|
405
435
|
state.status = 'cancelled';
|
|
436
|
+
this._notifyProgress(transferId, state);
|
|
406
437
|
}
|
|
407
438
|
|
|
408
439
|
this._emit({
|
|
@@ -442,7 +473,9 @@ export class MeshFileTransfer {
|
|
|
442
473
|
}
|
|
443
474
|
|
|
444
475
|
if (state.isComplete()) {
|
|
476
|
+
const wasDone = state.status === 'completed';
|
|
445
477
|
state.status = 'completed';
|
|
478
|
+
if (!wasDone) this._notifyProgress(transferId, state);
|
|
446
479
|
this._emit({
|
|
447
480
|
t: MESH_TYPE.FILE_COMPLETE,
|
|
448
481
|
p: { transferId },
|
|
@@ -477,7 +510,9 @@ export class MeshFileTransfer {
|
|
|
477
510
|
this._emitProgress(transferId, state);
|
|
478
511
|
|
|
479
512
|
if (state.isComplete()) {
|
|
513
|
+
const wasDone = state.status === 'completed';
|
|
480
514
|
state.status = 'completed';
|
|
515
|
+
if (!wasDone) this._notifyProgress(transferId, state);
|
|
481
516
|
this._emit({
|
|
482
517
|
t: MESH_TYPE.FILE_COMPLETE,
|
|
483
518
|
p: { transferId },
|
|
@@ -540,6 +575,7 @@ export class MeshFileTransfer {
|
|
|
540
575
|
const state = this.#states.get(transferId);
|
|
541
576
|
if (state && state.status !== 'completed' && state.status !== 'failed' && state.status !== 'cancelled') {
|
|
542
577
|
state.status = 'cancelled';
|
|
578
|
+
this._notifyProgress(transferId, state);
|
|
543
579
|
}
|
|
544
580
|
|
|
545
581
|
this._emit({
|
|
@@ -578,19 +614,27 @@ export class MeshFileTransfer {
|
|
|
578
614
|
}
|
|
579
615
|
case MESH_TYPE.FILE_REJECT: {
|
|
580
616
|
const state = this.#states.get(msg.p.transferId);
|
|
581
|
-
if (state
|
|
617
|
+
if (state && state.status !== 'cancelled') {
|
|
618
|
+
state.status = 'cancelled';
|
|
619
|
+
this._notifyProgress(msg.p.transferId, state);
|
|
620
|
+
}
|
|
582
621
|
this.#offers.delete(msg.p.transferId);
|
|
583
622
|
break;
|
|
584
623
|
}
|
|
585
624
|
case MESH_TYPE.FILE_COMPLETE: {
|
|
586
625
|
const state = this.#states.get(msg.p.transferId);
|
|
626
|
+
const wasDone = state?.status === 'completed';
|
|
587
627
|
if (state) state.status = 'completed';
|
|
628
|
+
if (state && !wasDone) this._notifyProgress(msg.p.transferId, state);
|
|
588
629
|
if (this.#onComplete) this.#onComplete(msg.p.transferId, state);
|
|
589
630
|
break;
|
|
590
631
|
}
|
|
591
632
|
case MESH_TYPE.FILE_CANCEL: {
|
|
592
633
|
const state = this.#states.get(msg.p.transferId);
|
|
593
|
-
if (state && state.status !== 'completed'
|
|
634
|
+
if (state && state.status !== 'completed' && state.status !== 'cancelled') {
|
|
635
|
+
state.status = 'cancelled';
|
|
636
|
+
this._notifyProgress(msg.p.transferId, state);
|
|
637
|
+
}
|
|
594
638
|
break;
|
|
595
639
|
}
|
|
596
640
|
}
|
|
@@ -602,7 +646,24 @@ export class MeshFileTransfer {
|
|
|
602
646
|
if (this.#onSend) this.#onSend(msg);
|
|
603
647
|
}
|
|
604
648
|
|
|
649
|
+
_notifyProgress(transferId, state) {
|
|
650
|
+
if (this.#progressListeners.size === 0) return;
|
|
651
|
+
const event = {
|
|
652
|
+
transferId,
|
|
653
|
+
direction: this.#directions.get(transferId) ?? 'receive',
|
|
654
|
+
bytesDone: state.bytesTransferred,
|
|
655
|
+
bytesTotal: state.totalSize,
|
|
656
|
+
percentComplete: state.percentComplete,
|
|
657
|
+
transferRate: state.transferRate,
|
|
658
|
+
state: state.status,
|
|
659
|
+
};
|
|
660
|
+
for (const fn of [...this.#progressListeners]) {
|
|
661
|
+
try { fn({ ...event }); } catch { /* listener errors never affect the transfer */ }
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
605
665
|
_emitProgress(transferId, state) {
|
|
666
|
+
this._notifyProgress(transferId, state);
|
|
606
667
|
if (this.#onProgress) {
|
|
607
668
|
this.#onProgress(transferId, {
|
|
608
669
|
bytesTransferred: state.bytesTransferred,
|