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

Migrate all jQuery promises to native ES6 Promise (ref: $.Deferred(), $.when(), $.get() and $.post()) #308

Draft
wants to merge 56 commits into
base: dev
Choose a base branch
from

Conversation

volterra79
Copy link
Member

@volterra79 volterra79 commented Jan 18, 2023

Depends on: #452

List of changes

  • replace $.Deferred::done() with Promise::then()
  • replace $.Deferred::fail() with Promise::catch()
  • replace $.Deferred::alwas() withPromise::finally()
  • replace $.when() with Promise::all()
  • replace $.get() with an internal utility function (ref: src/core/utils/utils/XHR::get())
  • replace $.post() with an internal utility function (ref: src/core/utils/utils/XHR::post())
  • add "util-deprecate" dependency to effectively report deprecation of some features to final developer (ref: src/deprecate.js) add new file src/deprecate.js to effectively report deprecation of some features to final developer
  • add deprecation notice for g3wsdk.core.utils.resolve(value) function
  • add deprecation notice for g3wsdk.core.utils.reject(value) function

Closes: #82
Somehow related to: #98

@volterra79 volterra79 added help wanted Extra attention is needed dependencies Pull requests that update a dependency file labels Jan 18, 2023
@volterra79 volterra79 added this to the v3.9 milestone Jan 18, 2023
@volterra79 volterra79 self-assigned this Jan 18, 2023
@Raruto Raruto changed the title Migrate all jQuery $.Deferred promises to native ES6 Promise and $.get and $.post JQuery method with XHR object Migrate all jQuery promises to native ES6 Promise (ref: $.Deferred, $.get and $.post) Jan 24, 2023
@Raruto Raruto changed the title Migrate all jQuery promises to native ES6 Promise (ref: $.Deferred, $.get and $.post) Migrate all jQuery promises to native ES6 Promise (ref: $.Deferred(), $.get() and $.post() functions) Jan 24, 2023
@Raruto Raruto changed the title Migrate all jQuery promises to native ES6 Promise (ref: $.Deferred(), $.get() and $.post() functions) Migrate all jQuery promises to native ES6 Promise (ref: $.Deferred(), $.when(), $.get() and $.post()) Jan 24, 2023
this.addFeatures(features);
resolve(features);
})
.catch(err => reject(err));
Copy link
Collaborator

Choose a reason for hiding this comment

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

double check, isn't this a superfluous statement?

.catch(err => reject(err));

Copy link
Collaborator

Choose a reason for hiding this comment

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

.catch(err => reject(err));

I didn't mark them all, but I found this kind of repetition quite often in the code

@@ -11,9 +11,7 @@ inherit(KMLDataProvider, DataProvider);
const proto = KMLDataProvider.prototype;

proto.getData = function() {
const d = $.Deferred();
return d.promise();
return new Promise((resolve, reject) => {})
Copy link
Collaborator

Choose a reason for hiding this comment

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

double check, how can such a promise object be fulfilled?

return new Promise((resolve, reject) => {})

Copy link
Member Author

Choose a reason for hiding this comment

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

@Raruto i think that KMLDataProvider is never used. We need to double check and remove it

Copy link
Collaborator

@Raruto Raruto Jan 24, 2023

Choose a reason for hiding this comment

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

ok, i didn't check in too much detail, but still from some other substitutions you've done i guess your initial intent was to write something like this instead:

return Promise.resolve()

@@ -455,7 +457,7 @@ proto._loadVectorData = function(vectorLayer, bbox) {
vectorLayer.setData(vectorDataResponse.vector.data);
if (this._) return vectorDataResponse;
})
.fail(() => {
.catch(() => {
return false;
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

double check, the following statement can be quite ambiguous:

.catch(() => { return false; })

it is best to make explicit what the intent is (ie. reject or resolve the promise) by using one of the following statements:

.catch(() => resolve(false))
.catch(() => reject(false))

Copy link
Member Author

Choose a reason for hiding this comment

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

@Raruto you can replace .catch(() => { return false; }) with .catch(() => reject(false)). We double check if this method is still used.

url,
data:params
}).then(response => resolve(response))
.catch(err => reject(err));
Copy link
Collaborator

@Raruto Raruto Jan 24, 2023

Choose a reason for hiding this comment

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

double check, written that way wouldn't the whole surrounding promise above be superfluous?

return // new Promise((resolve, reject) => {
    XHR.post({ ... })
//    .then(response => resolve(response))
//    .catch(err => reject(err));
// });

.finally(()=> clearTimeout(timeoutKey));
return d.promise();
})
} else this[METHOD]({url, layers, params})
Copy link
Collaborator

Choose a reason for hiding this comment

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

😩 Damn weird!

else statement + missing brackets + a multiline istruction

// return a promise that will be reolved if all step go right
return d.promise();
return new Promise((resolve, reject) => {
//** Assign d module variable to an object having resolve, reject methods used on other module method
Copy link
Collaborator

@Raruto Raruto Jan 24, 2023

Choose a reason for hiding this comment

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

Specific code comments (jsdoc, but not only those) on the usefulness of a variable must be written when declaring it otherwise your development IDE is not able to recognize them, that is:

/**
 * Module variable having resolve, reject methods used on other module method
 */
let d;

...

d = {
  resolve,
  reject
};

src/app/main.js Outdated
@@ -1164,7 +1164,7 @@ ApplicationTemplate.Services = {
floatbar: null
};

ApplicationTemplate.fail = function({language='en', error }) {
ApplicationTemplate.catch = function({language='en', error }) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

TODO: also double check that it hasn't been renamed elsewhere

Suggested change
ApplicationTemplate.catch = function({language='en', error }) {
ApplicationTemplate.fail = function({language='en', error }) {

src/app/main.js Outdated
@@ -1205,7 +1205,7 @@ ApplicationService.init()
if (error.responseJSON && error.responseJSON.error.data) error = error.responseJSON.error.data;
else if (error.statusText) error = error.statusText;
}
ApplicationTemplate.fail({
ApplicationTemplate.catch({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
ApplicationTemplate.catch({
ApplicationTemplate.fail({

@@ -135,7 +135,7 @@ function QueryService(){
request.then(response => {
const results = this.handleResponse(response, query);
resolve(results);
}).fail(reject)
}).catch(reject)
Copy link
Collaborator

@Raruto Raruto Jan 24, 2023

Choose a reason for hiding this comment

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

double check, written like that the external promise seems superfluous:

return // new Promise((resolve, reject) => {
  request.then(response => {
    const results = this.handleResponse(response, query);
    resolve(results);
  })
// .catch(reject)

@@ -44,7 +44,7 @@ function SearchService(){
resolve({
data
})
}).fail(reject)
}).catch(reject)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Raruto Raruto added breaking Any code changes that are not or may not be backward compatible and removed dependencies Pull requests that update a dependency file refactoring Anything which could result in a API change labels Jan 31, 2023
Comment on lines +89 to +95
return new Promise((resolve, reject) =>{
this.provider.getFeatures(options)
.then(features => {
this.addFeatures(features);
resolve(features);
})
.catch(err => reject(err));
Copy link
Collaborator

@Raruto Raruto Feb 3, 2023

Choose a reason for hiding this comment

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

Not sure what the previous behavior was, but now I think you need to add the return (resolve, reject) => this.provider.getFeatures from inside the promise to the outside to make . catch(err => reject(err)) also "catchable" from outside of this function (otherwise it will only throw an unhandled fatal error):

Suggested change
return new Promise((resolve, reject) =>{
this.provider.getFeatures(options)
.then(features => {
this.addFeatures(features);
resolve(features);
})
.catch(err => reject(err));
return new Promise((resolve, reject) =>
this.provider
.getFeatures(options)
.then(features => resolve(this.addFeatures(features)))
.catch(err => reject(err))
);

Anyway i'm not so sure about this statement, check it carefully too

@Raruto Raruto modified the milestones: v3.9, v4.0 Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Any code changes that are not or may not be backward compatible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrate all jQuery $.Deferred promises to native ES6 Promise
2 participants