Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async await #1358

Open
wants to merge 7 commits 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
60 changes: 30 additions & 30 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = (grunt) => {
options: {
undef: true, // check for usage of undefined constiables
indent: 2,
esversion: 6,
esversion: 8,
laxbreak: true,
'-W033': true, // ignore Missing semicolon
'-W041': true, // ignore Use '===' to compare with '0'
Expand Down Expand Up @@ -258,20 +258,19 @@ module.exports = (grunt) => {
).then(this.async());
});

const bumpDependency = (packageJson, packageName) => {
const bumpDependency = async (packageJson, packageName) => {
const dependencyType = packageJson['dependencies'][packageName]
? 'dependencies'
: 'devDependencies';
let currentVersion = packageJson[dependencyType][packageName];
if (currentVersion[0] == '~' || currentVersion[0] == '^')
currentVersion = currentVersion.slice(1);
return pkgVersions(packageName).then((versionSet) => {
const versions = Array.from(versionSet);
const latestVersion = semver.maxSatisfying(versions, '*');
if (semver.gt(latestVersion, currentVersion)) {
packageJson[dependencyType][packageName] = '~' + latestVersion;
}
});
const versionSet = await pkgVersions(packageName);
const versions = Array.from(versionSet);
const latestVersion = semver.maxSatisfying(versions, '*');
if (semver.gt(latestVersion, currentVersion)) {
packageJson[dependencyType][packageName] = '~' + latestVersion;
}
};

grunt.registerTask(
Expand Down Expand Up @@ -332,32 +331,33 @@ module.exports = (grunt) => {
.then((tests) => {
grunt.log.writeln('Running click tests in parallel... (this will take a while...)');
return Promise.all(
tests.map((file) => {
tests.map(async (file) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole clickParallel task still has a bunch of then. Why is only this part rewritten?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like promises which are not returned in a function are not going to be rewritten because that would change the return type to be a Promise instead of void.
This is fine in some places like here but we might want do change that in other places.

let output = '';
const outStream = (data) => (output += data);

grunt.log.writeln(cliColor.set(`Clicktest started! \t${file}`, 'blue'));
return new Promise((resolve, reject) => {
const child = childProcess.execFile(
'./node_modules/mocha/bin/mocha',
[path.join(__dirname, 'clicktests', file), '--timeout=35000', '-b'],
{ maxBuffer: 10 * 1024 * 1024 }
);
child.stdout.on('data', outStream);
child.stderr.on('data', outStream);
child.on('exit', (code) => {
if (code == 0) resolve(file);
else reject();
});
})
.then(() => {
grunt.log.writeln(cliColor.set(`'Clicktest success! \t${file}`, 'green'));
return { name: file, output: output, isSuccess: true };
})
.catch(() => {
grunt.log.writeln(cliColor.set(`'Clicktest fail! \t'${file}`, 'red'));
return { name: file, output: output, isSuccess: false };

try {
await new Promise((resolve, reject) => {
const child = childProcess.execFile(
'./node_modules/mocha/bin/mocha',
[path.join(__dirname, 'clicktests', file), '--timeout=35000', '-b'],
{ maxBuffer: 10 * 1024 * 1024 }
);
child.stdout.on('data', outStream);
child.stderr.on('data', outStream);
child.on('exit', (code) => {
if (code == 0) resolve(file);
else reject();
});
});

grunt.log.writeln(cliColor.set(`'Clicktest success! \t${file}`, 'green'));
return { name: file, output: output, isSuccess: true };
} catch (error) {
grunt.log.writeln(cliColor.set(`'Clicktest fail! \t'${file}`, 'red'));
return { name: file, output: output, isSuccess: false };
}
})
);
})
Expand Down
7 changes: 3 additions & 4 deletions components/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ class AppViewModel {
);
}
gitSetUserConfig(bugTracking) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example let's change that here.

this.server.getPromise('/userconfig').then((userConfig) => {
this.server.getPromise('/userconfig').then(async (userConfig) => {
userConfig.bugtracking = bugTracking;
return this.server.postPromise('/userconfig', userConfig).then(() => {
this.bugtrackingEnabled(bugTracking);
});
await this.server.postPromise('/userconfig', userConfig);
this.bugtrackingEnabled(bugTracking);
});
}
enableBugtracking() {
Expand Down
16 changes: 10 additions & 6 deletions components/branches/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,21 @@ class BranchesViewModel {
details: 'Deleting ' + details + ' branch cannot be undone with ungit.',
})
.show()
.closeThen((diag) => {
.closeThen(async (diag) => {
if (!diag.result()) return;
const url = `${branch.isRemote ? '/remote' : ''}/branches`;
return this.server
.delPromise(url, {

try {
await this.server.delPromise(url, {
path: this.graph.repoPath(),
remote: branch.isRemote ? branch.remote : null,
name: branch.refName,
})
.then(() => programEvents.dispatch({ event: 'working-tree-changed' }))
.catch((e) => this.server.unhandledRejection(e));
});

return programEvents.dispatch({ event: 'working-tree-changed' });
} catch (e) {
return this.server.unhandledRejection(e);
Copy link
Contributor

@Hirse Hirse May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As unhandledRejection doesn't seem to return anything, I would remove the return when it is called;

Suggested change
return this.server.unhandledRejection(e);
this.server.unhandledRejection(e);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it does not return anyting, the rewrite is just using the same returns as before.
So before

.catch((e) => this.server.unhandledRejection(e));

did also return undefined.
But I agree we should change that!

}
});
}
}
39 changes: 22 additions & 17 deletions components/graph/git-graph-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,16 @@ class Reset extends ActionBase {
details: 'Resetting to ref: ' + remoteRef.name + ' cannot be undone with ungit.',
})
.show()
.closeThen((diag) => {
.closeThen(async (diag) => {
if (!diag.result()) return;
return this.server
.postPromise('/reset', { path: this.graph.repoPath(), to: remoteRef.name, mode: 'hard' })
.then(() => {
context.node(remoteRef.node());
});

await this.server.postPromise('/reset', {
path: this.graph.repoPath(),
to: remoteRef.name,
mode: 'hard',
});

context.node(remoteRef.node());
}).closePromise;
}
}
Expand Down Expand Up @@ -316,17 +319,19 @@ class Uncommit extends ActionBase {
});
}

perform() {
return this.server
.postPromise('/reset', { path: this.graph.repoPath(), to: 'HEAD^', mode: 'mixed' })
.then(() => {
let targetNode = this.node.belowNode;
while (targetNode && !targetNode.ancestorOfHEAD()) {
targetNode = targetNode.belowNode;
}
this.graph.HEADref().node(targetNode ? targetNode : null);
this.graph.checkedOutRef().node(targetNode ? targetNode : null);
});
async perform() {
await this.server.postPromise('/reset', {
path: this.graph.repoPath(),
to: 'HEAD^',
mode: 'mixed',
});

let targetNode = this.node.belowNode;
while (targetNode && !targetNode.ancestorOfHEAD()) {
targetNode = targetNode.belowNode;
}
this.graph.HEADref().node(targetNode ? targetNode : null);
this.graph.checkedOutRef().node(targetNode ? targetNode : null);
}
}

Expand Down
117 changes: 56 additions & 61 deletions components/graph/git-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class RefViewModel extends Selectable {
this.isDragging(false);
}

moveTo(target, rewindWarnOverride) {
async moveTo(target, rewindWarnOverride) {
let promise;
if (this.isLocal) {
const toNode = this.graph.nodesById[target];
Expand Down Expand Up @@ -161,45 +161,44 @@ class RefViewModel extends Selectable {
});
}

return promise
.then((res) => {
if (!res) return;
const targetNode = this.graph.getNode(target);
if (this.graph.checkedOutBranch() == this.refName) {
this.graph.HEADref().node(targetNode);
}
this.node(targetNode);
})
.catch((e) => this.server.unhandledRejection(e));
try {
const res = await promise;
if (!res) return;
const targetNode = this.graph.getNode(target);
if (this.graph.checkedOutBranch() == this.refName) {
this.graph.HEADref().node(targetNode);
}
this.node(targetNode);
} catch (e) {
return this.server.unhandledRejection(e);
}
}

remove(isClientOnly) {
async remove(isClientOnly) {
let url = this.isTag ? '/tags' : '/branches';
if (this.isRemote) url = `/remote${url}`;

return (isClientOnly
? Promise.resolve()
: this.server.delPromise(url, {
try {
if (!isClientOnly)
await this.server.delPromise(url, {
path: this.graph.repoPath(),
remote: this.isRemote ? this.remote : null,
name: this.refName,
})
)
.then(() => {
if (this.node()) this.node().removeRef(this);
this.graph.refs.remove(this);
delete this.graph.refsByRefName[this.name];
})
.catch((e) => this.server.unhandledRejection(e))
.finally(() => {
if (!isClientOnly) {
if (url == '/remote/tags') {
programEvents.dispatch({ event: 'request-fetch-tags' });
} else {
programEvents.dispatch({ event: 'branch-updated' });
}
});

if (this.node()) this.node().removeRef(this);
this.graph.refs.remove(this);
delete this.graph.refsByRefName[this.name];
} catch (e) {
this.server.unhandledRejection(e);
} finally {
if (!isClientOnly) {
if (url == '/remote/tags') {
programEvents.dispatch({ event: 'request-fetch-tags' });
} else {
programEvents.dispatch({ event: 'branch-updated' });
}
});
}
}
}

getLocalRef() {
Expand Down Expand Up @@ -241,39 +240,35 @@ class RefViewModel extends Selectable {
.catch((e) => this.server.unhandledRejection(e));
}

checkout() {
async checkout() {
const isRemote = this.isRemoteBranch;
const isLocalCurrent = this.getLocalRef() && this.getLocalRef().current();

return Promise.resolve()
.then(() => {
if (isRemote && !isLocalCurrent) {
return this.server.postPromise('/branches', {
path: this.graph.repoPath(),
name: this.refName,
sha1: this.name,
force: true,
});
}
})
.then(() =>
this.server.postPromise('/checkout', { path: this.graph.repoPath(), name: this.refName })
)
.then(() => {
if (isRemote && isLocalCurrent) {
return this.server.postPromise('/reset', {
path: this.graph.repoPath(),
to: this.name,
mode: 'hard',
});
}
})
.then(() => {
this.graph.HEADref().node(this.node());
})
.catch((err) => {
if (err.errorCode != 'merge-failed') this.server.unhandledRejection(err);
try {
if (isRemote && !isLocalCurrent) {
return this.server.postPromise('/branches', {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually wrong, because postPromise does return a promise which isn't awaited and try catched here!

path: this.graph.repoPath(),
name: this.refName,
sha1: this.name,
force: true,
});
}
await this.server.postPromise('/checkout', {
path: this.graph.repoPath(),
name: this.refName,
});
if (isRemote && isLocalCurrent) {
return this.server.postPromise('/reset', {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually wrong, because postPromise does return a promise which isn't awaited and try catched here!

path: this.graph.repoPath(),
to: this.name,
mode: 'hard',
});
}

this.graph.HEADref().node(this.node());
} catch (err) {
if (err.errorCode != 'merge-failed') this.server.unhandledRejection(err);
}
}
}

Expand Down
Loading