Skip to content
This repository
Browse code

Fixed race condition in watch mode

Reload renamed files
  • Loading branch information...
commit d0f349de82e5f22ebc51e5090a7555cb84df9dec 1 parent 49d852a
Jazz jazzzz authored

Showing 2 changed files with 31 additions and 24 deletions. Show diff stats Hide diff stats

  1. +21 17 lib/watch.js
  2. +10 7 lib/wrap.js
38 lib/watch.js
@@ -24,6 +24,25 @@ function reg (w, opts, body, file) {
24 24 }
25 25 };
26 26 var pending = null;
  27 + var bundle = function () {
  28 + if (pending) return;
  29 + pending = setTimeout(function () {
  30 + pending = null;
  31 + // modified
  32 + if (w[type][file]) {
  33 + w.reload(file);
  34 + }
  35 + else if (type === 'entries') {
  36 + w.addEntry(file);
  37 + }
  38 + else if (type === 'files') {
  39 + w.require(file);
  40 + }
  41 +
  42 + w._cache = null;
  43 + w.emit('bundle');
  44 + }, 100);
  45 + };
27 46
28 47 var watcher = function (event, filename) {
29 48 exists(file, function (ex) {
@@ -39,27 +58,12 @@ function reg (w, opts, body, file) {
39 58 w._cache = null;
40 59 }
41 60 else if (event === 'change') {
42   - if (pending) return;
43   - pending = setTimeout(function () {
44   - pending = null;
45   - // modified
46   - if (w[type][file]) {
47   - w.reload(file);
48   - }
49   - else if (type === 'entries') {
50   - w.addEntry(file);
51   - }
52   - else if (type === 'files') {
53   - w.require(file);
54   - }
55   -
56   - w._cache = null;
57   - w.emit('bundle');
58   - }, 100);
  61 + bundle();
59 62 }
60 63 else if (event === 'rename') {
61 64 w.watches[file].close();
62 65 process.nextTick(watch);
  66 + bundle();
63 67 }
64 68 });
65 69 };
17 lib/wrap.js
@@ -183,10 +183,11 @@ Wrap.prototype.addEntry = function (file_, opts) {
183 183 if (!opts) opts = {};
184 184 var file = path.resolve(opts.dirname || process.cwd(), file_);
185 185
186   - var body = opts.body || self.readFile(file);
  186 + var entry = this.entries[file] = {};
  187 + // entry must be set before readFile for the watch
  188 + var body = entry.body = opts.body || self.readFile(file);
187 189 if (body === undefined) return self;
188 190
189   - var entry = this.entries[file] = { body : body };
190 191 if (opts.target) entry.target = opts.target;
191 192
192 193 try {
@@ -481,14 +482,16 @@ Wrap.prototype.require = function (mfile, opts) {
481 482 };
482 483 }
483 484 }
484   -
485   - var body = opts.body || self.readFile(opts.file);
486   - if (body === undefined) return self;
487   -
  485 +
488 486 var entry = self.files[opts.file] = {
489   - body : body,
490 487 target : opts.target
491 488 };
  489 + // entry must be set before readFile for the watch
  490 + var body = entry.body = opts.body || self.readFile(opts.file);
  491 + if (body === undefined) {
  492 + delete self.files[opts.file];
  493 + return self;
  494 + }
492 495
493 496 try {
494 497 var required = self.detective.find(body);

0 comments on commit d0f349d

Please sign in to comment.
Something went wrong with that request. Please try again.