Skip to content

Commit

Permalink
Merge pull request #135 from salesforce-ux/github-button
Browse files Browse the repository at this point in the history
Handling GitHubButton mounted state
  • Loading branch information
aputinski committed Nov 20, 2015
2 parents b561b1b + 7671e67 commit 8b1e599
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
14 changes: 10 additions & 4 deletions app_modules/site/components/github-button/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND

import React from 'react';
import classNames from 'classnames';
import sitemap from 'app_modules/site/navigation/sitemap';
import protectFromUnmount from 'app_modules/site/util/component/protect-from-unmount';
import CTALink from 'app_modules/site/components/cta-link';
import Img from 'app_modules/ui/img';
import { prefix as pf } from 'app_modules/ui/util/component';
Expand All @@ -29,22 +29,28 @@ class GithubButton extends React.Component {
getStars(cb) {
const req = new XMLHttpRequest()
req.onreadystatechange = () => {
if (req.readyState == 4) {
if (req.readyState == 4 && req.responseText) {
const json = JSON.parse(req.responseText);
cb(json);
}
}
req.open('GET', 'https://api.github.com/repos/salesforce-ux/design-system', true);
req.send();
return req;
}

componentDidMount() {
this.getStars((data) => {
this.protect = protectFromUnmount();
this.asyncReq = this.protect(this.getStars((data) => {
this.setState({
stargazersCount: data.stargazers_count || 0,
repoData: data
});
}.bind(this))
}.bind(this)))
}

componentWillUnmount() {
this.protect.unmount();
}

render() {
Expand Down
35 changes: 35 additions & 0 deletions app_modules/site/util/component/protect-from-unmount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright (c) 2015, salesforce.com, inc. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
* Protect state-setting callbacks from unmounted components
*
* This is a workaround method to React's ES6 deprecation of `isMounted()`
*
* Refer to:
* https://github.com/facebook/react/issues/5465
* https://gist.github.com/jimbolla/f548d20da071b7c67807
*
* @returns {function}
*/

function protectFromUnmount() {
let hasUnmounted_ = false;
const protect = (callback) => {
return (...args) => {
!hasUnmounted_ && callback(...args);
}
};
protect.unmount = () => hasUnmounted_ = true;
return protect;
}

export default protectFromUnmount;

0 comments on commit 8b1e599

Please sign in to comment.