73 lines
3.0 KiB
JavaScript
73 lines
3.0 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const pinkie_1 = __importDefault(require("pinkie"));
|
|
const RequireAnalyzer = __importStar(require("./legacy/analysis/require_analyzer"));
|
|
const array_remove_1 = __importDefault(require("../utils/array-remove"));
|
|
class RequireReader {
|
|
constructor(descriptorsCache, hammerheadProcessScript) {
|
|
this.descriptorsCache = descriptorsCache || {};
|
|
this.readings = [];
|
|
this.waiters = {};
|
|
this.hammerheadProcessScript = hammerheadProcessScript;
|
|
}
|
|
async _analyzeRequire(require, filename, sourceIndex) {
|
|
this.readings.push(require);
|
|
return new pinkie_1.default(resolve => {
|
|
RequireAnalyzer.run(require, filename, sourceIndex, this.hammerheadProcessScript, (errs, descriptor) => {
|
|
this.descriptorsCache[require] = descriptor;
|
|
(0, array_remove_1.default)(this.readings, require);
|
|
resolve({ errs, descriptor });
|
|
if (this.waiters[require]) {
|
|
this.waiters[require].forEach(waiter => waiter(descriptor));
|
|
this.waiters[require] = null;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
async _waitForReading(require) {
|
|
return new pinkie_1.default(resolve => {
|
|
if (!this.waiters[require])
|
|
this.waiters[require] = [];
|
|
this.waiters[require].push(descriptor => resolve({ descriptor, fromCache: true }));
|
|
});
|
|
}
|
|
async read(require, filename, sourceIndex) {
|
|
if (this.descriptorsCache[require]) {
|
|
return {
|
|
descriptor: this.descriptorsCache[require],
|
|
fromCache: true
|
|
};
|
|
}
|
|
if (this.readings.indexOf(require) > -1)
|
|
return await this._waitForReading(require);
|
|
return this._analyzeRequire(require, filename, sourceIndex);
|
|
}
|
|
}
|
|
exports.default = RequireReader;
|