@berish/rfp
Version:
Binary secure transport organization protocol for peer communication using function fingerprints
78 lines • 3.31 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PeerScope = void 0;
const emitter_1 = require("@berish/emitter");
class PeerScope {
constructor(peer) {
this._peer = null;
this._blockerEmitter = new emitter_1.EventEmitter();
this._blockerChain = [];
this._peer = peer;
}
hasBlockers() {
return this._blockerChain.length > 0;
}
hasBeforeBlocker(chunk) {
return this.getBeforeBlocker(chunk).length > 0;
}
getBeforeBlocker(chunk) {
const index = this._blockerChain.indexOf(chunk);
if (index > 0)
return this._blockerChain.slice(0, index - 1);
return [];
}
addBlocker(chunk) {
return __awaiter(this, void 0, void 0, function* () {
this._peer.logger('peerScope')('addBlocker').info(chunk.chunkId);
this._blockerChain.push(chunk);
yield this._blockerEmitter.emitAsync('block', chunk);
});
}
removeBlocker(chunk) {
return __awaiter(this, void 0, void 0, function* () {
if (this._blockerChain.indexOf(chunk) !== -1) {
this._peer.logger('peerScope')('removeBlocker').info(chunk.chunkId);
this._blockerChain = this._blockerChain.filter((m) => m !== chunk);
yield this._blockerEmitter.emitAsync('unblock', chunk);
}
});
}
waitBeforeUnblock(chunk) {
return __awaiter(this, void 0, void 0, function* () {
let beforeChunks = this.getBeforeBlocker(chunk);
if (beforeChunks.length <= 0)
return void 0;
this._peer
.logger('peerScope')('waitBeforeUnblock')(chunk.chunkId)
.info(beforeChunks.map((m) => m.chunkId));
do {
const unblockedChunk = yield Promise.race([
this._blockerEmitter.waitEvent('clear').then(() => null),
this._blockerEmitter.waitEvent('unblock'),
]);
if (!unblockedChunk)
return void 0;
beforeChunks = beforeChunks.filter((m) => m !== unblockedChunk);
} while (beforeChunks.length > 0);
});
}
clear() {
return __awaiter(this, void 0, void 0, function* () {
this._peer.logger('peerScope').info(`clear`);
this._blockerChain = [];
yield this._blockerEmitter.emitAsync('clear', null);
this._blockerEmitter.offAll();
});
}
}
exports.PeerScope = PeerScope;
//# sourceMappingURL=peerScope.js.map