Handle promises properly

With API V3 we have some relationships (like lastBuild on branch)
specified as asynchronuous, so sometimes we may deal with promises. This
commit fixes the situation by handling both plain records and promises.
This commit is contained in:
Piotr Sarnacki 2016-03-02 12:42:39 +01:00
parent 7035b3f763
commit 59747f8424
6 changed files with 54 additions and 7 deletions

View File

@ -1,7 +1,5 @@
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['repo-main-tools']
});

View File

@ -1,6 +1,7 @@
import Ember from 'ember';
import { githubRepo, statusImage } from 'travis/utils/urls';
import config from 'travis/config/environment';
import eventually from 'travis/utils/eventually';
export default Ember.Controller.extend({
@ -122,9 +123,12 @@ export default Ember.Controller.extend({
},
_lastBuildDidChange() {
var build;
build = this.get('repo.lastBuild');
return this.set('build', build);
let lastBuild = this.get('repo.lastBuild');
if(lastBuild) {
eventually(lastBuild, (build) => {
this.set('build', build);
});
}
},
stopObservingLastBuild() {

View File

@ -1,5 +1,6 @@
import Ember from 'ember';
import Config from 'travis/config/environment';
import eventually from 'travis/utils/eventually';
export default Ember.Service.extend({
records: [],
@ -28,7 +29,11 @@ export default Ember.Service.extend({
records.filter((record) => {
return this.get('allowFinishedBuilds') || !record.get('isFinished');
}).forEach((record) => {
record.updateTimes();
eventually(record, function(resolvedRecord) {
if(resolvedRecord) {
resolvedRecord.updateTimes();
}
});
});
this.set('records', []);

9
app/utils/eventually.js Normal file
View File

@ -0,0 +1,9 @@
export default function(anObjectOrAPromise, callback) {
if(anObjectOrAPromise.then) {
anObjectOrAPromise.then(function(result) {
callback(result);
});
} else {
callback(anObjectOrAPromise);
}
}

View File

@ -1,4 +1,5 @@
import Ember from 'ember';
import eventually from 'travis/utils/eventually';
export default Ember.Mixin.create({
restarting: false,
@ -31,7 +32,12 @@ export default Ember.Mixin.create({
onFinished = () => {
this.set('restarting', false);
};
return this.get('item').restart().then(onFinished, onFinished);
let restart = function(record) {
record.restart().then(onFinished, onFinished);
};
eventually(this.get('item'), (item) => {
item.restart();
});
},
cancel: function() {
var type;

View File

@ -0,0 +1,25 @@
import Ember from 'ember';
import eventually from 'travis/utils/eventually';
module("eventually");
test("eventually runs a callback with passed item right away if it's not a promise", function() {
stop();
expect(1);
eventually({ foo: 'bar' }, function(result) {
equal(result.foo, 'bar');
start();
});
});
test("eventually runs a callback when promise resolves if a passed object is a promise", function() {
stop();
expect(1);
let promise = { then: function(callback) { callback({ foo: 'bar'}); } };
eventually(promise, function(result) {
equal(result.foo, 'bar');
start();
});
});