2016-04-15 03:59:30 +00:00
|
|
|
"use strict";
|
|
|
|
|
2019-03-04 17:30:51 +00:00
|
|
|
const { JSDOM } = require( "jsdom" );
|
2016-04-15 03:59:30 +00:00
|
|
|
|
2019-03-04 17:30:51 +00:00
|
|
|
const { window } = new JSDOM( "" );
|
2016-04-15 03:59:30 +00:00
|
|
|
|
2019-03-04 17:30:51 +00:00
|
|
|
const jQuery = require( "../../" )( window );
|
2016-04-15 03:59:30 +00:00
|
|
|
|
2019-03-04 17:30:51 +00:00
|
|
|
module.exports.deferred = () => {
|
|
|
|
let adopted, promised;
|
|
|
|
|
|
|
|
return {
|
|
|
|
resolve: function() {
|
|
|
|
if ( !adopted ) {
|
|
|
|
adopted = jQuery.when.apply( jQuery, arguments );
|
|
|
|
if ( promised ) {
|
|
|
|
adopted.then( promised.resolve, promised.reject );
|
2016-04-15 03:59:30 +00:00
|
|
|
}
|
2019-03-04 17:30:51 +00:00
|
|
|
}
|
|
|
|
return adopted;
|
|
|
|
},
|
|
|
|
reject: function( value ) {
|
|
|
|
if ( !adopted ) {
|
|
|
|
adopted = jQuery.when( jQuery.Deferred().reject( value ) );
|
|
|
|
if ( promised ) {
|
|
|
|
adopted.then( promised.resolve, promised.reject );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return adopted;
|
|
|
|
},
|
2016-04-15 03:59:30 +00:00
|
|
|
|
2019-03-04 17:30:51 +00:00
|
|
|
// A manually-constructed thenable that works even if calls precede resolve/reject
|
|
|
|
promise: {
|
|
|
|
then: function() {
|
|
|
|
if ( !adopted ) {
|
|
|
|
if ( !promised ) {
|
|
|
|
promised = jQuery.Deferred();
|
|
|
|
}
|
|
|
|
return promised.then.apply( promised, arguments );
|
|
|
|
}
|
|
|
|
return adopted.then.apply( adopted, arguments );
|
|
|
|
}
|
|
|
|
}
|
2016-04-15 03:59:30 +00:00
|
|
|
};
|
2019-03-04 17:30:51 +00:00
|
|
|
};
|