Replace $.Deferred with ES2015 promises

This commit is contained in:
JC Brand 2017-07-11 17:21:13 +02:00
parent 16d62bf9e4
commit 6af029048d
3 changed files with 40 additions and 31 deletions

View File

@ -430,8 +430,13 @@
'jid': 'another@conference.shakespeare.lit' 'jid': 'another@conference.shakespeare.lit'
}).c('nick').t('JC').up().up(); }).c('nick').t('JC').up().up();
_converse.connection._dataRecv(test_utils.createRequest(stanza)); _converse.connection._dataRecv(test_utils.createRequest(stanza));
test_utils.waitUntil(function () {
return $('#chatrooms dl.bookmarks dd').length;
}, 300).then(function () {
expect($('#chatrooms dl.bookmarks dd').length).toBe(3); expect($('#chatrooms dl.bookmarks dd').length).toBe(3);
done(); done();
});
})); }));
it("remembers the toggle state of the bookmarks list", mock.initConverseWithPromises( it("remembers the toggle state of the bookmarks list", mock.initConverseWithPromises(
@ -472,6 +477,10 @@
'nick': '' 'nick': ''
}); });
test_utils.openControlBox().openRoomsPanel(_converse); test_utils.openControlBox().openRoomsPanel(_converse);
test_utils.waitUntil(function () {
return $('#chatrooms dl.bookmarks dd:visible').length;
}, 300).then(function () {
expect($('#chatrooms dl.bookmarks dd:visible').length).toBe(1); expect($('#chatrooms dl.bookmarks dd:visible').length).toBe(1);
expect(_converse.bookmarksview.list_model.get('toggle-state')).toBe(_converse.OPENED); expect(_converse.bookmarksview.list_model.get('toggle-state')).toBe(_converse.OPENED);
$('#chatrooms .bookmarks-toggle').click(); $('#chatrooms .bookmarks-toggle').click();
@ -481,6 +490,7 @@
expect($('#chatrooms dl.bookmarks dd:visible').length).toBe(1); expect($('#chatrooms dl.bookmarks dd:visible').length).toBe(1);
expect(_converse.bookmarksview.list_model.get('toggle-state')).toBe(_converse.OPENED); expect(_converse.bookmarksview.list_model.get('toggle-state')).toBe(_converse.OPENED);
done(); done();
});
})); }));
}); });
}); });

View File

@ -229,8 +229,7 @@
}, },
fetchBookmarks () { fetchBookmarks () {
const deferred = new $.Deferred(); const deferred = utils.getWrappedPromise();
const promise = deferred.promise();
if (this.browserStorage.records.length > 0) { if (this.browserStorage.records.length > 0) {
this.fetch({ this.fetch({
'success': _.bind(this.onCachedBookmarksFetched, this, deferred), 'success': _.bind(this.onCachedBookmarksFetched, this, deferred),
@ -245,7 +244,7 @@
} else { } else {
deferred.resolve(); deferred.resolve();
} }
return promise; return deferred.promise;
}, },
onCachedBookmarksFetched (deferred) { onCachedBookmarksFetched (deferred) {
@ -482,7 +481,7 @@
return; return;
} }
_converse.bookmarks = new _converse.Bookmarks(); _converse.bookmarks = new _converse.Bookmarks();
_converse.bookmarks.fetchBookmarks().always(function () { _converse.bookmarks.fetchBookmarks().then(function () {
_converse.bookmarksview = new _converse.BookmarksView( _converse.bookmarksview = new _converse.BookmarksView(
{'model': _converse.bookmarks} {'model': _converse.bookmarks}
); );

View File

@ -69,22 +69,22 @@
}; };
var isImage = function (url) { var isImage = function (url) {
var deferred = new $.Deferred(); return new Promise((resolve, reject) => {
var img = new Image(); var img = new Image();
var timer = window.setTimeout(function () { var timer = window.setTimeout(function () {
deferred.reject(); reject(new Error("Could not determine whether it's an image"));
img = null; img = null;
}, 3000); }, 3000);
img.onerror = img.onabort = function () { img.onerror = img.onabort = function () {
clearTimeout(timer); clearTimeout(timer);
deferred.reject(); reject(new Error("Could not determine whether it's an image"));
}; };
img.onload = function () { img.onload = function () {
clearTimeout(timer); clearTimeout(timer);
deferred.resolve(img); resolve(img);
}; };
img.src = url; img.src = url;
return deferred.promise(); });
}; };
$.fn.hasScrollBar = function() { $.fn.hasScrollBar = function() {