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

Promise support #2988

Merged
merged 24 commits into from
May 21, 2019
Merged

Promise support #2988

merged 24 commits into from
May 21, 2019

Conversation

jacekbogdanski
Copy link
Member

What is the purpose of this pull request?

Does your PR contain necessary tests?

All patches which change the editor code must include tests. You can always read more
on PR testing,
how to set the testing environment and
how to create tests
in the official CKEditor documentation.

This PR contains

  • Unit tests
  • Manual tests

What changes did you make?

After some research, it looks like ES6-Promise polyfill should suit our needs. It's pretty lightweight (6.17 KB / 2.4 KB gzipped) and seems to have the same API as native Promise. It's also recommended by MDN Promise docs.

Closes #2962

Copy link
Contributor

@engineering-this engineering-this left a comment

Choose a reason for hiding this comment

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

General issues:

  • Promises are not loaded on built version.
    Screenshot 2019-04-15 at 15 45 37
    It looks like polyfill.js file is not included in the build.
  • Used library should be listed in LICENSE.md instead of Q.

if ( window.Promise ) {
CKEDITOR.tools.promise = Promise;
} else {
var script = new CKEDITOR.dom.element( 'script' );
Copy link
Contributor

Choose a reason for hiding this comment

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

You can simplify it by using CKEDITOR.scriptLoader.load( src, callback );.

if ( window.Promise ) {
assert.areSame( window.Promise, CKEDITOR.tools.promise, 'Native Promise should be enabled' );
} else {
assert.areSame( window.ES6Promise, CKEDITOR.tools.promise, 'Polyfill Promise should be enabled' );
Copy link
Contributor

Choose a reason for hiding this comment

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

False positive case:
When polyfill library is not loaded test passes, as undefined === undefined.

* } );
* ```
*
* @param {Function} resolver
Copy link
Contributor

Choose a reason for hiding this comment

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

Function params can be listed as attributes, like in CKEDITOR.tools.array.forEach.

@jacekbogdanski
Copy link
Member Author

I realized the issue here - the whole core folder is minified by the builder, so there is no way to add the polyfill conditionally in that place. Instead, I created vendor folder for such 3rd party libs. It seems to be included in a built version and additionally minified, so we can use the full version for debugging.

I still would like to keep core/promise.js file instead of inlining this code inside core/tools.js, although in fact, it's tiny now.

Please, see ckeditor/ckeditor4-docs#242 for the error code (you may wanna review this one also).

},

// (#2962)
'test missing promise polyfill': function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This test will fail in built version as CKEDITOR.loader is absent because all core modules are bundled.

@engineering-this engineering-this removed their assignment Apr 16, 2019
@engineering-this
Copy link
Contributor

Please, see ckeditor/ckeditor4-docs#242 for the error code (you may wanna review this one also).

I think it should be reviewed after promise is merged.

@jacekbogdanski jacekbogdanski self-assigned this Apr 16, 2019
@jacekbogdanski
Copy link
Member Author

I'm wondering how to fix the test for built version #2988 (review) - maybe it's a hint for poorly written API?

tl;dr core/promise.js module is loaded when bootstrapping CKEditor and contains logic responsible for fetching promise polyfill (very simple, yet still requires testing). I'm trying to verify the sad path when there is no promise polyfill available at vendors/promise.js:

Options which comes to my mind:

  • Enable unit test only for dev version
  • Refactor promise loading into a separate function, something like CKEDITOR.promiseLoader.load, so it can be easily tested, although adds mostly unused API and also pollutes CKEDITOR namespace
  • Resign from CKEDITOR.error message - at some point editor will blow with missing CKEDITOR.tools.promise type

Honestly, I'm feeling like I did some overengineering here and there is no reason for an explicit error message. WDYT?

@engineering-this
Copy link
Contributor

Error message is helpful. I'd ignore test under condition: !CKEDITOR.loader. It's unlikely that message will work correctly in dev and not in build. Additionally we can test it manually by deleting polyfill file.

@jacekbogdanski
Copy link
Member Author

jacekbogdanski commented Apr 16, 2019

Additionally we can test it manually by deleting polyfill file.

That would be a bit difficult to do during a testing phase, although I agree that we maybe could disable test for built version, as it's only testing if an error message occurs, not if a promise has been correctly loaded.

@engineering-this
Copy link
Contributor

Missing an error message doesn't really break things, so I'd just check it before merging PR.

Copy link
Contributor

@engineering-this engineering-this left a comment

Choose a reason for hiding this comment

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

LG2M 👍

Copy link
Contributor

@f1ames f1ames left a comment

Choose a reason for hiding this comment

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

I wonder if we could move ES6Promise js file from vendor dir to adapters dir. Well, this is not strictly an adapter, but now we have adapters with one jquery file and vendor dir with one promise.js file 🤔 (Btw. we cannot rename adapters to not break backward compatibility).

When it comes to unit test, there are two test promise initialization and test missing promise polyfill. Do you think it would make sense to add e.g. one more test to assert how CKEDITOR.tools.promise behaves? It will be testing native code / polyfill so I'm not sure. OTOH, if this test will start failing it means something changed in the implementation (not sure if it's probable).

I created vendor folder for such 3rd party libs. It seems to be included in a built version and additionally minified, so we can use the full version for debugging.

Is the content of this folder also minified during build?

LICENSE.md Outdated Show resolved Hide resolved
core/promise.js Outdated Show resolved Hide resolved
core/promise.js Outdated Show resolved Hide resolved
core/promise.js Show resolved Hide resolved
tests/_benderjs/ckeditor/static/tools.js Show resolved Hide resolved
tests/core/tools/promise.js Show resolved Hide resolved
@jacekbogdanski jacekbogdanski self-assigned this May 17, 2019
@jacekbogdanski
Copy link
Member Author

I wonder if we could move ES6Promise js file from vendor dir to adapters dir. Well, this is not strictly an adapter, but now we have adapters with one jquery file and vendor dir with one promise.js file thinking (Btw. we cannot rename adapters to not break backward compatibility).

If you are just worried about the additional folder, don't be - there is no filesystem limit we can exceed with that 😄 To be more serious - you already wrote that it's not adapter and in the future, we may have additional vendor libs with the same problem. It will be easier to keep 2 separate namespaces for that than always close eye for "not strictly adapter" argument. Although it's not something I want to fight for, if you still would like to move all vendor libs to adapter folder, it's fine for me. But the name may be misleading for the users.

When it comes to unit test, there are two test promise initialization and test missing promise polyfill. Do you think it would make sense to add e.g. one more test to assert how CKEDITOR.tools.promise behaves? It will be testing native code / polyfill so I'm not sure. OTOH, if this test will start failing it means something changed in the implementation (not sure if it's probable).

We are not testing the native Array object nor other types. I would make no exception here and keep it simple.

OTOH, if this test will start failing it means something changed in the implementation (not sure if it's probable).

The Internet would fall with such breaking native API change 😄 I think we will be fine.

Is the content of this folder also minified during build?

It is.

@f1ames
Copy link
Contributor

f1ames commented May 21, 2019

Rebased on the latest major.

Copy link
Contributor

@f1ames f1ames left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@f1ames f1ames merged commit 680bdd9 into major May 21, 2019
@CKEditorBot CKEditorBot deleted the t/2962 branch May 21, 2019 13:11
@engineering-this engineering-this removed their request for review May 30, 2019 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Promise support
4 participants