add `'change'` event to `HasteMap`

Reviewed By: bestander

Differential Revision: D3641360

fbshipit-source-id: 7941d0e954ad3e2aba1f16d797da280f6095aa05
This commit is contained in:
David Aurelio 2016-07-29 11:00:13 -07:00 committed by Facebook Github Bot 7
parent f34bb8fb94
commit eeda397983
1 changed files with 31 additions and 10 deletions

View File

@ -7,6 +7,9 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
*/ */
'use strict'; 'use strict';
const EventEmitter = require('events');
const path = require('../fastpath'); const path = require('../fastpath');
const getPlatformExtension = require('../lib/getPlatformExtension'); const getPlatformExtension = require('../lib/getPlatformExtension');
@ -14,7 +17,7 @@ const GENERIC_PLATFORM = 'generic';
const NATIVE_PLATFORM = 'native'; const NATIVE_PLATFORM = 'native';
const PACKAGE_JSON = path.sep + 'package.json'; const PACKAGE_JSON = path.sep + 'package.json';
class HasteMap { class HasteMap extends EventEmitter {
constructor({ constructor({
extensions, extensions,
fastfs, fastfs,
@ -23,6 +26,7 @@ class HasteMap {
helpers, helpers,
platforms, platforms,
}) { }) {
super();
this._extensions = extensions; this._extensions = extensions;
this._fastfs = fastfs; this._fastfs = fastfs;
this._moduleCache = moduleCache; this._moduleCache = moduleCache;
@ -50,6 +54,7 @@ class HasteMap {
processFileChange(type, absPath) { processFileChange(type, absPath) {
return Promise.resolve().then(() => { return Promise.resolve().then(() => {
/*eslint no-labels: 0 */ /*eslint no-labels: 0 */
let invalidated;
if (type === 'delete' || type === 'change') { if (type === 'delete' || type === 'change') {
loop: for (const name in this._map) { loop: for (const name in this._map) {
const modulesMap = this._map[name]; const modulesMap = this._map[name];
@ -57,21 +62,25 @@ class HasteMap {
const module = modulesMap[platform]; const module = modulesMap[platform];
if (module.path === absPath) { if (module.path === absPath) {
delete modulesMap[platform]; delete modulesMap[platform];
invalidated = name;
break loop; break loop;
} }
} }
} }
if (type === 'delete') { if (type === 'delete') {
if (invalidated) {
this.emit('change');
}
return null; return null;
} }
} }
if (this._extensions.indexOf(this._helpers.extname(absPath)) !== -1) { if (type !== 'delete' && this._extensions.indexOf(this._helpers.extname(absPath)) !== -1) {
if (path.basename(absPath) === 'package.json') { if (path.basename(absPath) === 'package.json') {
return this._processHastePackage(absPath); return this._processHastePackage(absPath, invalidated);
} else { } else {
return this._processHasteModule(absPath); return this._processHasteModule(absPath, invalidated);
} }
} }
}); });
@ -98,20 +107,32 @@ class HasteMap {
return module; return module;
} }
_processHasteModule(file) { _processHasteModule(file, previousName) {
const module = this._moduleCache.getModule(file); const module = this._moduleCache.getModule(file);
return module.isHaste().then( return module.isHaste().then(
isHaste => isHaste && module.getName() isHaste => isHaste && module.getName()
.then(name => this._updateHasteMap(name, module)) .then(name => {
const result = this._updateHasteMap(name, module);
if (previousName && name !== previousName) {
this.emit('change');
}
return result;
})
); );
} }
_processHastePackage(file) { _processHastePackage(file, previousName) {
file = path.resolve(file); file = path.resolve(file);
const p = this._moduleCache.getPackage(file); const p = this._moduleCache.getPackage(file);
return p.isHaste() return p.isHaste()
.then(isHaste => isHaste && p.getName() .then(isHaste => isHaste && p.getName()
.then(name => this._updateHasteMap(name, p))) .then(name => {
const result = this._updateHasteMap(name, p);
if (previousName && name !== previousName) {
this.emit('change');
}
return result;
}))
.catch(e => { .catch(e => {
if (e instanceof SyntaxError) { if (e instanceof SyntaxError) {
// Malformed package.json. // Malformed package.json.
@ -135,8 +156,8 @@ class HasteMap {
`@providesModule naming collision:\n` + `@providesModule naming collision:\n` +
` Duplicate module name: ${name}\n` + ` Duplicate module name: ${name}\n` +
` Paths: ${mod.path} collides with ${existingModule.path}\n\n` + ` Paths: ${mod.path} collides with ${existingModule.path}\n\n` +
`This error is caused by a @providesModule declaration ` + 'This error is caused by a @providesModule declaration ' +
`with the same name across two different files.` 'with the same name across two different files.'
); );
} }