Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Make file watching more robust #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@paulcbetts/mime-types": "^2.1.10",
"btoa": "^1.1.2",
"debug": "^2.5.1",
"gaze": "^1.1.2",
"lru-cache": "^4.0.1",
"mkdirp": "^0.5.1",
"pify": "^2.3.0",
Expand Down
6 changes: 0 additions & 6 deletions src/custom-operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ function retryWithDelayOrError(errors, maxRetries) {
}

const newCoolOperators = {
guaranteedThrottle: function(time, scheduler = async) {
return this
.map((x) => Observable.timer(time, scheduler).map(() => x))
.switch();
},

retryAtIntervals: function(maxRetries = 3) {
return this.retryWhen((errors) => retryWithDelayOrError(errors, maxRetries));
},
Expand Down
6 changes: 2 additions & 4 deletions src/live-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ function enableLiveReloadNaive() {
.filter(x => !FileChangedCache.isInNodeModules(x.filePath));

let weShouldReload = filesWeCareAbout
.mergeMap(x => watchPath(x.filePath).map(() => x))
.guaranteedThrottle(1*1000);
.mergeMap(x => watchPath(x.filePath).map(() => x));

return weShouldReload
.switchMap(() => Observable.defer(() => Observable.fromPromise(reloadAllWindows()).timeout(5*1000).catch(() => Observable.empty())))
Expand All @@ -75,8 +74,7 @@ function enableReactHMR() {
.filter(x => !FileChangedCache.isInNodeModules(x.filePath));

let weShouldReload = filesWeCareAbout
.mergeMap(x => watchPath(x.filePath).map(() => x))
.guaranteedThrottle(1*1000);
.mergeMap(x => watchPath(x.filePath).map(() => x));

return weShouldReload
.switchMap(() => Observable.defer(() => Observable.fromPromise(triggerHMRInRenderers()).catch(() => Observable.empty())))
Expand Down
17 changes: 9 additions & 8 deletions src/pathwatcher-rx.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import {Observable} from 'rxjs/Observable';
import {Subscription} from 'rxjs/Subscription';
import {Gaze} from 'gaze';
import LRU from 'lru-cache';

import 'rxjs/add/operator/publish';
Expand All @@ -9,14 +9,15 @@ export function watchPathDirect(directory) {
return Observable.create((subj) => {
let dead = false;

const watcher = fs.watch(directory, {}, (eventType, fileName) => {
if (dead) return;
subj.next({eventType, fileName});
});

watcher.on('error', (e) => {
const watcher = new Gaze();
watcher.on('error', (err) => {
dead = true;
subj.error(e);
subj.error(err);
});
watcher.add(directory);
watcher.on('changed', (fileName) => {
if (dead) return;
subj.next({fileName, eventType: 'changed'});
});

return new Subscription(() => { if (!dead) { watcher.close(); } });
Expand Down