Files
react-native/packager/react-packager/src/DependencyResolver/ModuleCache.js
Christoph Pojer 5b4244d755 Sync new haste features from upstream
Reviewed By: amasad

Differential Revision: D2644383

fb-gh-sync-id: 5225e6afb8e5b835865473b2880a77235e6bd6eb
2015-11-16 22:50:30 -08:00

93 lines
2.2 KiB
JavaScript

'use strict';
const AssetModule = require('./AssetModule');
const Package = require('./Package');
const Module = require('./Module');
const path = require('path');
class ModuleCache {
constructor(fastfs, cache, extractRequires) {
this._moduleCache = Object.create(null);
this._packageCache = Object.create(null);
this._fastfs = fastfs;
this._cache = cache;
this._extractRequires = extractRequires;
fastfs.on('change', this._processFileChange.bind(this));
}
getModule(filePath) {
filePath = path.resolve(filePath);
if (!this._moduleCache[filePath]) {
this._moduleCache[filePath] = new Module(
filePath,
this._fastfs,
this,
this._cache,
this._extractRequires
);
}
return this._moduleCache[filePath];
}
getAssetModule(filePath) {
filePath = path.resolve(filePath);
if (!this._moduleCache[filePath]) {
this._moduleCache[filePath] = new AssetModule(
filePath,
this._fastfs,
this,
this._cache,
);
}
return this._moduleCache[filePath];
}
getPackage(filePath) {
filePath = path.resolve(filePath);
if (!this._packageCache[filePath]) {
this._packageCache[filePath] = new Package(
filePath,
this._fastfs,
this._cache,
);
}
return this._packageCache[filePath];
}
getPackageForModule(module) {
// TODO(amasad): use ES6 Map.
if (module.__package) {
if (this._packageCache[module.__package]) {
return this._packageCache[module.__package];
} else {
delete module.__package;
}
}
const packagePath = this._fastfs.closest(module.path, 'package.json');
if (!packagePath) {
return null;
}
module.__package = packagePath;
return this.getPackage(packagePath);
}
_processFileChange(type, filePath, root) {
const absPath = path.join(root, filePath);
if (this._moduleCache[absPath]) {
this._moduleCache[absPath].invalidate();
delete this._moduleCache[absPath];
}
if (this._packageCache[absPath]) {
this._packageCache[absPath].invalidate();
delete this._packageCache[absPath];
}
}
}
module.exports = ModuleCache;