Commit c45de7bb authored by Nathan Bruer's avatar Nathan Bruer Committed by Commit Bot

[Devtools] createFile in projects promisified

This patch is for code health and to make it eassier for followup
patches. Simply makes createFile() in Workspace.Project return a promise
instead of a callback.

R=lushnikov
BUG=None

Change-Id: If9e0a8d1e24cb01e3a1cba8188d31d4e2b9f61c5
Reviewed-on: https://chromium-review.googlesource.com/720078
Commit-Queue: Blaise Bruer <allada@chromium.org>
Reviewed-by: default avatarPavel Feldman <pfeldman@chromium.org>
Reviewed-by: default avatarAndrey Lushnikov <lushnikov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#509539}
parent be12f0ba
......@@ -50,10 +50,7 @@
]);
function createSnippetPromise(content) {
var callback;
var promise = new Promise(fullfill => (callback = fullfill));
Snippets.scriptSnippetModel._project.createFile('', null, content, callback);
return promise;
return Snippets.scriptSnippetModel._project.createFile('', null, content);
}
function renameSourceCodePromise(newName, uiSourceCode) {
......
......@@ -83,7 +83,7 @@ function test() {
resetSnippetsSettings();
Snippets.scriptSnippetModel.project().createFile('', null, '', step2.bind(this));
Snippets.scriptSnippetModel.project().createFile('', null, '').then(step2.bind(this));
function step2(uiSourceCode) {
uiSourceCode1 = uiSourceCode;
......@@ -100,7 +100,7 @@ function test() {
function contentDumped2() {
TestRunner.addResult('Snippet1 created.');
Snippets.scriptSnippetModel.project().createFile('', null, '', step3.bind(this));
Snippets.scriptSnippetModel.project().createFile('', null, '').then(step3.bind(this));
}
}
......@@ -120,7 +120,7 @@ function test() {
function onContentDumped() {
Snippets.scriptSnippetModel.project().deleteFile(uiSourceCode1);
Snippets.scriptSnippetModel.project().deleteFile(uiSourceCode2);
Snippets.scriptSnippetModel.project().createFile('', null, '', step4.bind(this));
Snippets.scriptSnippetModel.project().createFile('', null, '').then(step4.bind(this));
}
}
......@@ -152,7 +152,7 @@ function test() {
var snippetScriptMapping =
Snippets.scriptSnippetModel.snippetScriptMapping(SDK.targetManager.models(SDK.DebuggerModel)[0]);
Snippets.scriptSnippetModel.project().createFile('', null, '', step2.bind(this));
Snippets.scriptSnippetModel.project().createFile('', null, '').then(step2.bind(this));
function step2(uiSourceCode) {
uiSourceCode1 = uiSourceCode;
......@@ -161,7 +161,7 @@ function test() {
content += '// This snippet does nothing.\n';
content += 'var i = 2+2;\n';
uiSourceCode1.setWorkingCopy(content);
Snippets.scriptSnippetModel.project().createFile('', null, '', step3.bind(this));
Snippets.scriptSnippetModel.project().createFile('', null, '').then(step3.bind(this));
}
function step3(uiSourceCode) {
......@@ -174,7 +174,7 @@ function test() {
content += '};\n';
content += 'doesNothing;\n';
uiSourceCode2.setWorkingCopy(content);
Snippets.scriptSnippetModel.project().createFile('', null, '', step4.bind(this));
Snippets.scriptSnippetModel.project().createFile('', null, '').then(step4.bind(this));
}
function step4(uiSourceCode) {
......@@ -217,7 +217,7 @@ function test() {
var snippetScriptMapping =
Snippets.scriptSnippetModel.snippetScriptMapping(SDK.targetManager.models(SDK.DebuggerModel)[0]);
Snippets.scriptSnippetModel.project().createFile('', null, '', step3.bind(this));
Snippets.scriptSnippetModel.project().createFile('', null, '').then(step3.bind(this));
function step3(uiSourceCode) {
var uiSourceCode1 = uiSourceCode;
......@@ -242,7 +242,7 @@ function test() {
context = this.executionContexts()[0];
resetSnippetsSettings();
Snippets.scriptSnippetModel.project().createFile('', null, '', step2.bind(this));
Snippets.scriptSnippetModel.project().createFile('', null, '').then(step2.bind(this));
}
function step2(uiSourceCode) {
......@@ -256,7 +256,7 @@ function test() {
function testDangerousNames(next) {
resetSnippetsSettings();
Snippets.scriptSnippetModel.project().createFile('', null, '', step2.bind(this));
Snippets.scriptSnippetModel.project().createFile('', null, '').then(step2.bind(this));
function step2(uiSourceCode) {
uiSourceCode.rename('toString');
......@@ -264,7 +264,7 @@ function test() {
}
function step3() {
Snippets.scriptSnippetModel.project().createFile('', null, '', step4.bind(this));
Snippets.scriptSnippetModel.project().createFile('', null, '').then(step4.bind(this));
}
function step4(uiSourceCode) {
......
......@@ -164,9 +164,9 @@ Bindings.ContentProviderBasedProject = class extends Workspace.ProjectStore {
* @param {string} path
* @param {?string} name
* @param {string} content
* @param {function(?Workspace.UISourceCode)} callback
* @return {!Promise<?Workspace.UISourceCode>}
*/
createFile(path, name, content, callback) {
createFile(path, name, content) {
}
/**
......
......@@ -501,35 +501,15 @@ Persistence.FileSystemWorkspaceBinding.FileSystem = class extends Workspace.Proj
* @param {string} path
* @param {?string} name
* @param {string} content
* @param {function(?Workspace.UISourceCode)} callback
*/
createFile(path, name, content, callback) {
this._fileSystem.createFile(path, name, innerCallback.bind(this));
var createFilePath;
/**
* @param {?string} filePath
* @this {Persistence.FileSystemWorkspaceBinding.FileSystem}
*/
function innerCallback(filePath) {
if (!filePath) {
callback(null);
return;
}
createFilePath = filePath;
if (!content) {
contentSet.call(this);
return;
}
this._fileSystem.setFileContent(filePath, content, contentSet.bind(this));
}
/**
* @this {Persistence.FileSystemWorkspaceBinding.FileSystem}
*/
function contentSet() {
callback(this._addFile(createFilePath));
}
* @return {!Promise<?Workspace.UISourceCode>}
*/
async createFile(path, name, content) {
var filePath = await new Promise(resolve => this._fileSystem.createFile(path, name, resolve));
if (!filePath)
return null;
if (content)
await new Promise(resolve => this._fileSystem.setFileContent(filePath, content, resolve));
return this._addFile(filePath);
}
/**
......
......@@ -577,10 +577,10 @@ Snippets.SnippetsProject = class extends Bindings.ContentProviderBasedProject {
* @param {string} url
* @param {?string} name
* @param {string} content
* @param {function(?Workspace.UISourceCode)} callback
* @return {!Promise<?Workspace.UISourceCode>}
*/
createFile(url, name, content, callback) {
callback(this._model.createScriptSnippet(content));
createFile(url, name, content) {
return /** @type {!Promise<?Workspace.UISourceCode>} */ (Promise.resolve(this._model.createScriptSnippet(content)));
}
/**
......
......@@ -761,40 +761,17 @@ Sources.NavigatorView = class extends UI.VBox {
* @param {string} path
* @param {!Workspace.UISourceCode=} uiSourceCodeToCopy
*/
create(project, path, uiSourceCodeToCopy) {
/**
* @this {Sources.NavigatorView}
* @param {?string} content
*/
function contentLoaded(content) {
createFile.call(this, content || '');
}
async create(project, path, uiSourceCodeToCopy) {
var content = '';
if (uiSourceCodeToCopy)
uiSourceCodeToCopy.requestContent().then(contentLoaded.bind(this));
else
createFile.call(this);
/**
* @this {Sources.NavigatorView}
* @param {string=} content
*/
function createFile(content) {
project.createFile(path, null, content || '', fileCreated.bind(this));
}
/**
* @this {Sources.NavigatorView}
* @param {?Workspace.UISourceCode} uiSourceCode
*/
function fileCreated(uiSourceCode) {
if (!uiSourceCode)
return;
this._sourceSelected(uiSourceCode, false);
var node = this.revealUISourceCode(uiSourceCode, true);
if (node)
this.rename(node, true);
}
content = (await uiSourceCodeToCopy.requestContent()) || '';
var uiSourceCode = await project.createFile(path, null, content);
if (!uiSourceCode)
return;
this._sourceSelected(uiSourceCode, false);
var node = this.revealUISourceCode(uiSourceCode, true);
if (node)
this.rename(node, true);
}
_groupingChanged() {
......
......@@ -148,9 +148,9 @@ Workspace.Project.prototype = {
* @param {string} path
* @param {?string} name
* @param {string} content
* @param {function(?Workspace.UISourceCode)} callback
* @return {!Promise<?Workspace.UISourceCode>}
*/
createFile(path, name, content, callback) {},
createFile(path, name, content) {},
/**
* @param {!Workspace.UISourceCode} uiSourceCode
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment