RN ModuleCache.js: @flow

Reviewed By: davidaurelio

Differential Revision: D4117759

fbshipit-source-id: 158f6e50cb9211f87acce17047ed3e959e6c271f
This commit is contained in:
Jean Lauliac 2016-11-02 13:40:47 -07:00 committed by Facebook Github Bot
parent 94929b717c
commit 1baa2ab893
2 changed files with 51 additions and 17 deletions

View File

@ -18,14 +18,16 @@ const isAbsolutePath = require('absolute-path');
const jsonStableStringify = require('json-stable-stringify');
const path = require('path');
type Extractor = (sourceCode: string) => {deps: {sync: Array<string>}};
import type ModuleCache from './ModuleCache';
export type Extractor = (sourceCode: string) => {deps: {sync: Array<string>}};
type TransformedCode = {
code?: string,
dependencies?: Array<string>,
dependencyOffsets?: Array<number>,
map?: string,
};
type TransformCode = (
export type TransformCode = (
module: Module,
sourceCode: string,
transformOptions: mixed,
@ -35,7 +37,7 @@ type TransformCode = (
dependencyOffsets?: Array<number>,
map?: string,
}>;
type Cache = {
export type Cache = {
get<T>(
filePath: string,
key: string,
@ -43,15 +45,16 @@ type Cache = {
): Promise<T>,
invalidate(filePath: string): void,
};
type Options = {cacheTransformResults?: boolean};
type ModuleCache = {
getPackageForModule(m: Module): {
getName(): Promise<string>,
root: string,
},
export type Options = {cacheTransformResults?: boolean};
export type FastFs = {
readFile: (filePath: string) => Promise<string>,
closest: (innerFilePath: string, fileName: string) => string,
on: (
event: 'change',
onChange: (type: string, filePath: string, root: string) => void,
) => FastFs,
};
type FastFs = {readFile: (filePath: string) => Promise<string>};
type DepGraphHelpers = {isNodeModulesDir: (filePath: string) => boolean};
export type DepGraphHelpers = {isNodeModulesDir: (filePath: string) => boolean};
class Module {

View File

@ -5,6 +5,8 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
@ -16,8 +18,29 @@ const Polyfill = require('./Polyfill');
const path = require('path');
import type {
Cache,
DepGraphHelpers,
FastFs,
Extractor,
TransformCode,
Options as ModuleOptions,
} from './Module';
class ModuleCache {
_moduleCache: {[filePath: string]: Module};
_packageCache: {[filePath: string]: Package};
_fastfs: FastFs;
_cache: Cache;
_extractRequires: Extractor;
_transformCode: TransformCode;
_depGraphHelpers: DepGraphHelpers;
_platforms: mixed;
_assetDependencies: mixed;
_moduleOptions: ModuleOptions;
_packageModuleMap: WeakMap<Module, string>;
constructor({
fastfs,
cache,
@ -26,7 +49,15 @@ class ModuleCache {
depGraphHelpers,
assetDependencies,
moduleOptions,
}, platforms) {
}: {
fastfs: FastFs,
cache: Cache,
extractRequires: Extractor,
transformCode: TransformCode,
depGraphHelpers: DepGraphHelpers,
assetDependencies: mixed,
moduleOptions: ModuleOptions,
}, platforms: mixed) {
this._moduleCache = Object.create(null);
this._packageCache = Object.create(null);
this._fastfs = fastfs;
@ -42,7 +73,7 @@ class ModuleCache {
fastfs.on('change', this._processFileChange.bind(this));
}
getModule(filePath) {
getModule(filePath: string) {
if (!this._moduleCache[filePath]) {
this._moduleCache[filePath] = new Module({
file: filePath,
@ -62,7 +93,7 @@ class ModuleCache {
return this._moduleCache;
}
getAssetModule(filePath) {
getAssetModule(filePath: string) {
if (!this._moduleCache[filePath]) {
this._moduleCache[filePath] = new AssetModule({
file: filePath,
@ -75,7 +106,7 @@ class ModuleCache {
return this._moduleCache[filePath];
}
getPackage(filePath) {
getPackage(filePath: string) {
if (!this._packageCache[filePath]) {
this._packageCache[filePath] = new Package({
file: filePath,
@ -86,7 +117,7 @@ class ModuleCache {
return this._packageCache[filePath];
}
getPackageForModule(module) {
getPackageForModule(module: Module): ?Package {
if (this._packageModuleMap.has(module)) {
const packagePath = this._packageModuleMap.get(module);
if (this._packageCache[packagePath]) {
@ -105,7 +136,7 @@ class ModuleCache {
return this.getPackage(packagePath);
}
createPolyfill({file}) {
createPolyfill({file}: {file: string}) {
return new Polyfill({
file,
cache: this._cache,