Skip to content

Commit

Permalink
Async load Google Chart Library
Browse files Browse the repository at this point in the history
Simpler error handling
  • Loading branch information
naoak committed Jul 4, 2016
1 parent 38f9eb6 commit d94a974
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
4 changes: 0 additions & 4 deletions charts-loader.html

This file was deleted.

27 changes: 21 additions & 6 deletions google-chart.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-ajax/iron-request.html">
<link rel="import" href="../promise-polyfill/promise-polyfill-lite.html">
<link rel="import" href="charts-loader.html">

<!--
`google-chart` encapsulates Google Charts as a web component, allowing you to easily visualize
Expand Down Expand Up @@ -132,10 +131,12 @@
var getChartPackage = function(opt_pkg) {
var pkg = opt_pkg || DEFACTO_CHART_PACKAGE;
if (pkgPromises[pkg]) return pkgPromises[pkg];
pkgPromises[pkg] = new Promise(function(resolve) {
google.charts.load('current', {packages: [pkg]});
google.charts.setOnLoadCallback(function() {
resolve(google.visualization);
pkgPromises[pkg] = libraryPromise.then(function() {
return new Promise(function(resolve) {
google.charts.load('current', {packages: [pkg]});
google.charts.setOnLoadCallback(function() {
resolve(google.visualization);
});
});
});
return pkgPromises[pkg];
Expand All @@ -147,6 +148,15 @@
return CHART_CONSTRUCTORS[type](chartElement);
};

var libraryPromise = new Promise(function(resolve, reject) {
var script = document.createElement('script');
script.src = 'https://www.gstatic.com/charts/loader.js';
script.onload = resolve;
script.onerror = reject;
var s = document.querySelector('script') || document.body;
s.parentNode.insertBefore(script, s);
});

// We can go ahead and load the defacto package because we'll need it to do
// anything (e.g. creating the DataTable to render).
var vizPkg = getChartPackage();
Expand Down Expand Up @@ -389,6 +399,10 @@
return chart;
},

_handleError: function() {

This comment has been minimized.

Copy link
@wesalvaro

wesalvaro Jul 11, 2016

takes no argument

this.fire('google-chart-error');
},

/**
* Redraws the chart.
*
Expand All @@ -409,7 +423,8 @@
chart.draw(data, this.options || {});
return chart;
}.bind(this), function(error) {
this.$.chartdiv.innerHTML = error;
this._handleError(error);
this.$.chartdiv.innerHTML = '';
throw error;
}.bind(this));
this._setSelection();
Expand Down

7 comments on commit d94a974

@wesalvaro
Copy link

Choose a reason for hiding this comment

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

Yeah, this was the magic I was talking about. Seems fine to me but I still feel strongly that this should be fixed with a proper loader element. Is charts the only consumer of this new loader style? If so, I think it's fine to compartmentalize it into an element just in this repository but don't worry about that (but feel free to tackle that if you want).

Let's see what @ebidel thinks about it after you open a PR.

From what I can tell, this looks good for firing errors on library loader issues and drawing issues?

You should add in some tests and make sure the existing ones still behave as expected.

@naoak
Copy link
Owner Author

@naoak naoak commented on d94a974 Jul 11, 2016

Choose a reason for hiding this comment

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

@wesalvaro Thanks for quick review. I'll make a PR with fixing some issues you pointed out.

@naoak
Copy link
Owner Author

@naoak naoak commented on d94a974 Jul 11, 2016

Choose a reason for hiding this comment

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

Well..., I've tried to write some test codes for handling loading error with sinon's useFakeServer(), but it seems that it can only stubs XHR responses, but not ones from script tags. Any ideas for this?

@wesalvaro
Copy link

Choose a reason for hiding this comment

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

Hmm... I'm not familiar with how one could go about testing that, sorry. I don't think it's super important as loading is all that we really care about and manual testing seems straightforward.
A new bug has cropped up for my team wrt calling load multiple times via the old loader. I'm going to wrap the current loading stuff together to address the issue, so hang on a bit, if you would, please.

@naoak
Copy link
Owner Author

@naoak naoak commented on d94a974 Jul 13, 2016

Choose a reason for hiding this comment

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

OK. I can stay here as long as you like. Thanks.

@wesalvaro
Copy link

Choose a reason for hiding this comment

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

OK, I've got the new loader element checked in but I didn't port any of your changes into it. If you want to integrate them into head now, that would be great.

@naoak
Copy link
Owner Author

@naoak naoak commented on d94a974 Jul 20, 2016

Choose a reason for hiding this comment

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

That's awesome! I'll take a look at the new loader.

Please sign in to comment.