mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 20:01:01 +08:00
packager: attachHMRServer.js: Flow
Summary: The breakage fixed by changeset [1] could have been identified earlier if we had typing on `attachHMRServer`, so I spent some time on that. This has revealed in turn a few functions across the codebase that were incorrectly typed, and that are now fixed. [1] packager: attachHMRServer.js: fix callsite of Server#getModuleForPath() Reviewed By: davidaurelio Differential Revision: D4706241 fbshipit-source-id: fc4285245921ae45d5781a47d626fc0559dba998
This commit is contained in:
committed by
Facebook Github Bot
parent
18c239ee22
commit
7098c450d8
@@ -5,7 +5,10 @@
|
||||
* 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';
|
||||
|
||||
const querystring = require('querystring');
|
||||
@@ -13,15 +16,27 @@ const url = require('url');
|
||||
|
||||
const {getInverseDependencies} = require('../../../packager/src//node-haste');
|
||||
|
||||
import type HMRBundle from '../../../packager/src/Bundler/HMRBundle';
|
||||
import type Server from '../../../packager/src/Server';
|
||||
import type ResolutionResponse from '../../../packager/src/node-haste/DependencyGraph/ResolutionResponse';
|
||||
import type Module from '../../../packager/src/node-haste/Module';
|
||||
import type {Server as HTTPServer} from 'http';
|
||||
|
||||
const blacklist = [
|
||||
'Libraries/Utilities/HMRClient.js',
|
||||
];
|
||||
|
||||
type HMROptions = {
|
||||
httpServer: HTTPServer,
|
||||
path: string,
|
||||
packagerServer: Server,
|
||||
};
|
||||
|
||||
/**
|
||||
* Attaches a WebSocket based connection to the Packager to expose
|
||||
* Hot Module Replacement updates to the simulator.
|
||||
*/
|
||||
function attachHMRServer({httpServer, path, packagerServer}) {
|
||||
function attachHMRServer({httpServer, path, packagerServer}: HMROptions) {
|
||||
let client = null;
|
||||
|
||||
function disconnect() {
|
||||
@@ -33,18 +48,25 @@ function attachHMRServer({httpServer, path, packagerServer}) {
|
||||
// - The full list of dependencies.
|
||||
// - The shallow dependencies each file on the dependency list has
|
||||
// - Inverse shallow dependencies map
|
||||
function getDependencies(platform, bundleEntry) {
|
||||
function getDependencies(platform: string, bundleEntry: string): Promise<{
|
||||
dependenciesCache: Array<string>,
|
||||
dependenciesModulesCache: {[mixed]: Module},
|
||||
shallowDependencies: {[string]: Array<Module>},
|
||||
inverseDependenciesCache: mixed,
|
||||
resolutionResponse: ResolutionResponse,
|
||||
}> {
|
||||
return packagerServer.getDependencies({
|
||||
platform: platform,
|
||||
dev: true,
|
||||
hot: true,
|
||||
entryFile: bundleEntry,
|
||||
}).then(response => {
|
||||
const {getModuleId} = response;
|
||||
/* $FlowFixMe: getModuleId might be null */
|
||||
const {getModuleId}: {getModuleId: () => number} = response;
|
||||
|
||||
// for each dependency builds the object:
|
||||
// `{path: '/a/b/c.js', deps: ['modA', 'modB', ...]}`
|
||||
return Promise.all(Object.values(response.dependencies).map(dep => {
|
||||
return Promise.all(response.dependencies.map((dep: Module) => {
|
||||
return dep.getName().then(depName => {
|
||||
if (dep.isAsset() || dep.isJSON()) {
|
||||
return Promise.resolve({path: dep.path, deps: []});
|
||||
@@ -54,23 +76,23 @@ function attachHMRServer({httpServer, path, packagerServer}) {
|
||||
dev: true,
|
||||
hot: true,
|
||||
entryFile: dep.path
|
||||
})
|
||||
.then(deps => {
|
||||
return {
|
||||
path: dep.path,
|
||||
name: depName,
|
||||
deps,
|
||||
};
|
||||
});
|
||||
}).then(deps => {
|
||||
return {
|
||||
path: dep.path,
|
||||
name: depName,
|
||||
deps,
|
||||
};
|
||||
});
|
||||
});
|
||||
}))
|
||||
.then(deps => {
|
||||
.then((deps: Array<{path: string, name?: string, deps: Array<Module>}>) => {
|
||||
// list with all the dependencies' filenames the bundle entry has
|
||||
const dependenciesCache = response.dependencies.map(dep => dep.path);
|
||||
|
||||
// map from module name to path
|
||||
const moduleToFilenameCache = Object.create(null);
|
||||
deps.forEach(dep => {
|
||||
/* $FlowFixMe: `name` could be null, but `deps` would be as well. */
|
||||
moduleToFilenameCache[dep.name] = dep.path;
|
||||
});
|
||||
|
||||
@@ -114,6 +136,7 @@ function attachHMRServer({httpServer, path, packagerServer}) {
|
||||
});
|
||||
|
||||
wss.on('connection', ws => {
|
||||
/* $FlowFixMe: url might be null */
|
||||
const params = querystring.parse(url.parse(ws.upgradeReq.url).query);
|
||||
|
||||
getDependencies(params.platform, params.bundleEntry)
|
||||
@@ -195,11 +218,13 @@ function attachHMRServer({httpServer, path, packagerServer}) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const nonNullClient = client;
|
||||
|
||||
return packagerServer.getModuleForPath(filename).then(moduleToUpdate => {
|
||||
// build list of modules for which we'll send HMR updates
|
||||
const modulesToUpdate = [moduleToUpdate];
|
||||
Object.keys(depsModulesCache).forEach(module => {
|
||||
if (!client.dependenciesModulesCache[module]) {
|
||||
if (!nonNullClient.dependenciesModulesCache[module]) {
|
||||
modulesToUpdate.push(depsModulesCache[module]);
|
||||
}
|
||||
});
|
||||
@@ -215,10 +240,10 @@ function attachHMRServer({httpServer, path, packagerServer}) {
|
||||
modulesToUpdate.reverse();
|
||||
|
||||
// invalidate caches
|
||||
client.dependenciesCache = depsCache;
|
||||
client.dependenciesModulesCache = depsModulesCache;
|
||||
client.shallowDependencies = shallowDeps;
|
||||
client.inverseDependenciesCache = inverseDepsCache;
|
||||
nonNullClient.dependenciesCache = depsCache;
|
||||
nonNullClient.dependenciesModulesCache = depsModulesCache;
|
||||
nonNullClient.shallowDependencies = shallowDeps;
|
||||
nonNullClient.inverseDependenciesCache = inverseDepsCache;
|
||||
|
||||
return resolutionResponse.copy({
|
||||
dependencies: modulesToUpdate
|
||||
@@ -252,7 +277,7 @@ function attachHMRServer({httpServer, path, packagerServer}) {
|
||||
resolutionResponse,
|
||||
}, packagerHost, httpServerAddress.port);
|
||||
})
|
||||
.then(bundle => {
|
||||
.then((bundle: HMRBundle) => {
|
||||
if (!client || !bundle || bundle.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
@@ -299,6 +324,7 @@ function attachHMRServer({httpServer, path, packagerServer}) {
|
||||
});
|
||||
|
||||
promise.then(() => {
|
||||
/* $FlowFixMe: assume `client` non-null */
|
||||
client.ws.send(JSON.stringify({type: 'update-done'}));
|
||||
});
|
||||
});
|
||||
@@ -316,7 +342,7 @@ function attachHMRServer({httpServer, path, packagerServer}) {
|
||||
});
|
||||
}
|
||||
|
||||
function arrayEquals(arrayA, arrayB) {
|
||||
function arrayEquals<T>(arrayA: Array<T>, arrayB: Array<T>): boolean {
|
||||
arrayA = arrayA || [];
|
||||
arrayB = arrayB || [];
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user