@u4/adbkit
Version:
A Typescript client for the Android Debug Bridge.
69 lines • 2.46 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = __importDefault(require("events"));
/**
* A simple EventEmitter, mainly for keeping track of the progress.
*/
class PushTransfer extends events_1.default {
constructor() {
super(...arguments);
this.stack = [];
this.stats = {
bytesTransferred: 0,
};
this.on = (event, listener) => super.on(event, listener);
this.off = (event, listener) => super.off(event, listener);
this.once = (event, listener) => super.once(event, listener);
this.emit = (event, ...args) => super.emit(event, ...args);
this.done = false;
}
/**
* Cancels the transfer by ending both the stream that is being pushed and the sync connection. This will most likely end up creating a broken file on your device. **Use at your own risk.** Also note that you must create a new sync connection if you wish to continue using the sync service.
* @returns The pushTransfer instance.
*/
cancel() {
return this.emit('cancel');
}
push(byteCount) {
return this.stack.push(byteCount);
}
pop() {
const byteCount = this.stack.pop();
if (byteCount) {
this.stats.bytesTransferred += byteCount;
}
return this.emit('progress', this.stats);
}
end() {
this.done = true;
return this.emit('end');
}
/**
* get end notification using Promise
*/
waitForEnd() {
if (!this.waitForEndPromise) {
if (this.done) {
this.waitForEndPromise = Promise.resolve();
}
else {
this.waitForEndPromise = new Promise((resolve, reject) => {
const unReg = () => {
this.off('end', onEnd);
this.off('error', onError);
};
const onError = (e) => { unReg(); reject(e); };
const onEnd = () => (unReg(), resolve());
this.on('end', onEnd);
this.on('error', onError);
});
}
}
return this.waitForEndPromise;
}
}
exports.default = PushTransfer;
//# sourceMappingURL=pushtransfer.js.map