create-expo-cljs-app
Version:
Create a react native application with Expo and Shadow-CLJS!
87 lines (74 loc) • 2.2 kB
Flow
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
const Module = require('./Module');
const Package = require('./Package');
type GetClosestPackageFn = (filePath: string) => ?string;
class ModuleCache {
_getClosestPackage: GetClosestPackageFn;
_moduleCache: {
[filePath: string]: Module,
__proto__: null,
...
};
_packageCache: {
[filePath: string]: Package,
__proto__: null,
...
};
_packageModuleMap: WeakMap<Module, string>;
constructor(options: {getClosestPackage: GetClosestPackageFn, ...}) {
this._getClosestPackage = options.getClosestPackage;
this._moduleCache = Object.create(null);
this._packageCache = Object.create(null);
this._packageModuleMap = new WeakMap();
}
getModule(filePath: string): Module {
if (!this._moduleCache[filePath]) {
this._moduleCache[filePath] = new Module(filePath, this);
}
return this._moduleCache[filePath];
}
getPackage(filePath: string): Package {
if (!this._packageCache[filePath]) {
this._packageCache[filePath] = new Package({
file: filePath,
});
}
return this._packageCache[filePath];
}
getPackageForModule(module: Module): ?Package {
let packagePath = this._packageModuleMap.get(module);
if (packagePath) {
if (this._packageCache[packagePath]) {
return this._packageCache[packagePath];
} else {
this._packageModuleMap.delete(module);
}
}
packagePath = this._getClosestPackage(module.path);
if (!packagePath) {
return null;
}
this._packageModuleMap.set(module, packagePath);
return this.getPackage(packagePath);
}
processFileChange(type: string, filePath: string) {
if (this._moduleCache[filePath]) {
this._moduleCache[filePath].invalidate();
delete this._moduleCache[filePath];
}
if (this._packageCache[filePath]) {
this._packageCache[filePath].invalidate();
delete this._packageCache[filePath];
}
}
}
module.exports = ModuleCache;