Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
fix(frontend): fixed addFileSystem race
Browse files Browse the repository at this point in the history
With latest changes it is more important to load main script
UISourceCode on time. This patch forces loading of main script
ahead of time.
  • Loading branch information
alexkozy committed Jun 25, 2019
1 parent bb46e2b commit b9c7793
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
4 changes: 4 additions & 0 deletions front_end/ndb/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Ndb.FileSystem = class extends Persistence.PlatformFileSystem {
await this._fsService.startWatcher(this._embedderPath, this._excludePattern(), rpc.handle(this));
}

forceFileLoad(scriptName) {
return this._fsService.forceFileLoad(scriptName);
}

/**
* @override
* @return {!Array<string>}
Expand Down
26 changes: 16 additions & 10 deletions front_end/ndb/NdbMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ Ndb.NdbMain = class extends Common.Object {
const setting = Persistence.isolatedFileSystemManager.workspaceFolderExcludePatternSetting();
setting.set(Ndb.NdbMain._defaultExcludePattern().join('|'));
Ndb.nodeProcessManager = await Ndb.NodeProcessManager.create(SDK.targetManager);
this._addDefaultFileSystem();

const {cwd} = await Ndb.processInfo();
await Ndb.nodeProcessManager.addFileSystem(cwd);

await new Promise(resolve => SDK.initMainConnection(resolve));
// Create root Main target.
Expand All @@ -50,11 +52,6 @@ Ndb.NdbMain = class extends Common.Object {
this._repl();
}

async _addDefaultFileSystem() {
const info = await Ndb.processInfo();
await Ndb.nodeProcessManager.addFileSystem(info.cwd);
}

async _repl() {
const code = btoa(`console.log('Welcome to the ndb %cR%cE%cP%cL%c!',
'color:#8bc34a', 'color:#ffc107', 'color:#ff5722', 'color:#2196f3', 'color:inherit');
Expand Down Expand Up @@ -185,18 +182,27 @@ Ndb.NodeProcessManager = class extends Common.Object {
return this._processes.get(target.id()) || null;
}

async addFileSystem(cwd) {
/**
* @param {string} cwd
* @param {string=} mainFileName
* @return {!Promise}
*/
async addFileSystem(cwd, mainFileName) {
let promise = this._cwds.get(cwd);
if (!promise) {
async function innerAdd() {
const cwdUrl = Common.ParsedURL.platformPathToURL(cwd);
const fileSystemManager = Persistence.isolatedFileSystemManager;
fileSystemManager.addPlatformFileSystem(cwdUrl, await Ndb.FileSystem.create(fileSystemManager, cwd, cwdUrl));
const fs = await Ndb.FileSystem.create(fileSystemManager, cwd, cwdUrl, mainFileName);
fileSystemManager.addPlatformFileSystem(cwdUrl, fs);
return fs;
}
promise = innerAdd();
this._cwds.set(cwd, promise);
}
return promise;
if (mainFileName)
await (await promise).forceFileLoad(mainFileName);
await promise;
}

async detected(id) {
Expand All @@ -220,7 +226,7 @@ Ndb.NodeProcessManager = class extends Common.Object {
UI.context.setFlavor(SDK.ExecutionContext, executionContext);
}

await this.addFileSystem(info.cwd);
await this.addFileSystem(info.cwd, info.scriptName);
if (info.scriptName) {
const scriptURL = Common.ParsedURL.platformPathToURL(info.scriptName);
const uiSourceCode = Workspace.workspace.uiSourceCodeForURL(scriptURL);
Expand Down
13 changes: 12 additions & 1 deletion services/file_system.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
const fs = require('fs');

const { rpc, rpc_process } = require('carlo/rpc');
const chokidar = require('chokidar');

class FileSystemHandler {
constructor() {
require('../lib/process_utility.js')('file_system', () => this.dispose());
this._watcher = null;
this._embedderPath = '';
this._client = null;
}

startWatcher(embedderPath, exludePattern, client) {
startWatcher(embedderPath, exludePattern, client, mainFileName) {
this._embedderPath = embedderPath;
this._client = client;
this._watcher = chokidar.watch([embedderPath], {
ignored: new RegExp(exludePattern),
awaitWriteFinish: true,
Expand All @@ -27,6 +33,11 @@ class FileSystemHandler {
this._watcher.on('error', console.error);
}

forceFileLoad(fileName) {
if (fileName.startsWith(this._embedderPath) && fs.existsSync(fileName))
this._client.filesChanged([{type: 'add', name: fileName}]);
}

dispose() {
this._watcher.close();
Promise.resolve().then(() => process.exit(0));
Expand Down

0 comments on commit b9c7793

Please sign in to comment.