bit-bin
Version:
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b
225 lines (180 loc) • 6.05 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getWorkspaceGraph = getWorkspaceGraph;
exports.Network = void 0;
function _bluebird() {
const data = require("bluebird");
_bluebird = function () {
return data;
};
return data;
}
function _rxjs() {
const data = require("rxjs");
_rxjs = function () {
return data;
};
return data;
}
function _operators() {
const data = require("rxjs/operators");
_operators = function () {
return data;
};
return data;
}
function _events() {
const data = require("events");
_events = function () {
return data;
};
return data;
}
function _scopeGraph() {
const data = _interopRequireDefault(require("../../../scope/graph/scope-graph"));
_scopeGraph = function () {
return data;
};
return data;
}
function _subGraph() {
const data = require("./sub-graph");
_subGraph = function () {
return data;
};
return data;
}
function _sortGraphByLevels() {
const data = require("../util/sort-graph-by-levels");
_sortGraphByLevels = function () {
return data;
};
return data;
}
class Network {
constructor(workspace, seeders, getFlow, getGraph = getWorkspaceGraph, postFlow, emitter = new (_events().EventEmitter)()) {
this.workspace = workspace;
this.seeders = seeders;
this.getFlow = getFlow;
this.getGraph = getGraph;
this.postFlow = postFlow;
this.emitter = emitter;
}
execute(options) {
var _this = this;
return (0, _bluebird().coroutine)(function* () {
const networkStream = new (_rxjs().ReplaySubject)();
const startTime = new Date();
networkStream.next({
type: 'network:start',
startTime
});
const graph = yield _this.createGraph(options);
const createCapsuleStartTime = new Date();
networkStream.next({
type: 'network:capsules:start',
startTime: createCapsuleStartTime
});
const visitedCache = yield createCapsuleVisitCache(graph, _this.workspace);
networkStream.next({
type: 'network:capsules:end',
startTime: createCapsuleStartTime,
duration: new Date().getTime() - createCapsuleStartTime.getTime()
});
_this.emitter.emit('workspaceLoaded', Object.keys(visitedCache).length);
_this.traverse(graph, networkStream, visitedCache, startTime);
return networkStream;
})();
}
onWorkspaceLoaded(cb) {
this.emitter.on('workspaceLoaded', cb);
}
traverse(graph, stream, visitedCache, startTime) {
const sorted = (0, _sortGraphByLevels().toposortByLevels)(graph);
const getFlow = this.getFlow.bind(this);
const postFlow = this.postFlow ? this.postFlow.bind(this) : null;
(0, _rxjs().from)(sorted).pipe((0, _operators().mergeMap)(level => (0, _rxjs().from)(level.map(flowId => getFlow(visitedCache[flowId].capsule).then(flow => {
visitedCache[flowId].visited = true;
return flow.execute(visitedCache[flowId].capsule);
})))), (0, _operators().concatAll)(), (0, _operators().map)(flowStream => {
stream.next(flowStream);
return flowStream;
}), (0, _operators().mergeAll)(), (0, _operators().filter)(data => data.type === 'flow:result'), (0, _operators().tap)(data => visitedCache[data.id.toString()].result = data), (0, _operators().tap)(data => handlePostFlow(postFlow, visitedCache[data.id.toString()]))).subscribe({
complete() {
endNetwork(stream, startTime, visitedCache);
},
error(err) {
handleNetworkError(err.id, graph, visitedCache, err);
}
});
}
createGraph(options) {
var _this2 = this;
return (0, _bluebird().coroutine)(function* () {
const fullGraph = yield _this2.getGraph(_this2.workspace.consumer);
if (_this2.seeders.length === 0) {
return fullGraph;
}
const components = yield _this2.workspace.getMany(_this2.seeders.map(seed => seed._legacy));
const subGraph = (0, _subGraph().createSubGraph)(components, options, fullGraph);
return subGraph;
})();
}
}
exports.Network = Network;
function handlePostFlow(postFlow, cacheValue) {
if (postFlow) {
postFlow(cacheValue.capsule).catch(() => {});
}
}
function endNetwork(network, startTime, visitedCache) {
const endMessage = {
type: 'network:result',
startTime,
duration: new Date().getTime() - startTime.getTime(),
value: Object.entries(visitedCache).reduce((accum, [key, val]) => {
accum[key] = {
result: val.result,
visited: val.visited
};
return accum;
}, {}),
// eslint-disable-next-line no-bitwise
code: !!~Object.entries(visitedCache).findIndex( // eslint-disable-next-line @typescript-eslint/no-unused-vars
([_k, value]) => !value.visited || value.result instanceof Error)
};
network.next(endMessage);
network.complete();
}
function handleNetworkError(seed, graph, visitedCache, err) {
const dependents = (0, _subGraph().getNeighborsByDirection)(seed, graph);
dependents.map(dependent => {
visitedCache[dependent].result = new Error(`Error due to ${seed}`);
return dependent;
}).forEach(dependent => graph.removeNode(dependent));
visitedCache[seed].result = err;
graph.removeNode(seed);
}
function createCapsuleVisitCache(_x, _x2) {
return _createCapsuleVisitCache.apply(this, arguments);
}
function _createCapsuleVisitCache() {
_createCapsuleVisitCache = (0, _bluebird().coroutine)(function* (graph, workspace) {
const capsules = yield workspace.loadCapsules(graph.nodes());
return capsules.reduce((accum, curr) => {
accum[curr.component.id.toString()] = {
visited: false,
capsule: curr,
result: null
};
return accum;
}, {});
});
return _createCapsuleVisitCache.apply(this, arguments);
}
function getWorkspaceGraph(consumer) {
return _scopeGraph().default.buildGraphFromWorkspace(consumer, false, true);
}
;