Skip to content

Commit

Permalink
added LazyLoadingErrorBoundary for wrapping suspense case, added lice…
Browse files Browse the repository at this point in the history
…nse info if missing, cleaned up unnecessary parts
  • Loading branch information
anton-karlovskiy committed Aug 15, 2019
1 parent f915320 commit 1309d73
Show file tree
Hide file tree
Showing 39 changed files with 599 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import React from 'react';

/*** Adapted from https://reactjs.org/docs/error-boundaries.html ***/
export default class LazyLoadingErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = {hasError: false};
}

static getDerivedStateFromError(error) {
return {hasError: true};
}

render() {
if (this.state.hasError) {
return (
<div>
<button onClick={() => window.location.reload()}>Click to Reload</button>
<p>Lazy-loading failed!</p>
</div>
);
}

return this.props.children;
}
}
17 changes: 10 additions & 7 deletions cra-device-class-aware-loading/src/components/Product/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import React, { lazy, Suspense, Fragment } from 'react';

import LazyLoadingErrorBoundary from '../LazyLoadingErrorBoundary';
import { useDeviceParams } from '../../utils/hooks';
import { Multicore_Score_Threshold } from '../../config';

Expand Down Expand Up @@ -49,13 +50,15 @@ const Product = ({ imageUrl, ...rest }) => {
unsupportMessage={unsupportMessage}
modelName={name}
multicoreScore={multicore_score} />
<Suspense fallback={Loading}>
{ multicore_score > Multicore_Score_Threshold || unsupportMessage ? (
<LazyHeavy imageUrl={imageUrl} {...rest} />
) : (
<LazyLight imageUrl={imageUrl} {...rest} />
) }
</Suspense>
<LazyLoadingErrorBoundary>
<Suspense fallback={Loading}>
{ multicore_score > Multicore_Score_Threshold || unsupportMessage ? (
<LazyHeavy imageUrl={imageUrl} {...rest} />
) : (
<LazyLight imageUrl={imageUrl} {...rest} />
) }
</Suspense>
</LazyLoadingErrorBoundary>
</Fragment>
);
};
Expand Down
16 changes: 16 additions & 0 deletions cra-device-class-aware-loading/src/serviceWorker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// This optional code is used to register a service worker.
// register() is not called by default.

Expand Down
19 changes: 0 additions & 19 deletions cra-memory-considerate-loading-sketchfab/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
text-align: center;
}

.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 40vmin;
pointer-events: none;
}

.App-header {
background-color: #282c34;
min-height: 100vh;
Expand All @@ -34,16 +28,3 @@
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
16 changes: 16 additions & 0 deletions cra-memory-considerate-loading-sketchfab/src/App.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import React from 'react';

/*** Adapted from https://reactjs.org/docs/error-boundaries.html ***/
export default class LazyLoadingErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = {hasError: false};
}

static getDerivedStateFromError(error) {
return {hasError: true};
}

render() {
if (this.state.hasError) {
return (
<div>
<button onClick={() => window.location.reload()}>Click to Reload</button>
<p>Lazy-loading failed!</p>
</div>
);
}

return this.props.children;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/

import React, { Fragment, Suspense, lazy } from 'react';
import React, { Suspense, lazy } from 'react';

import Loading from '../Loading';
import LazyLoadingErrorBoundary from '../LazyLoadingErrorBoundary';

const SketchFabEmbed = lazy(() => import('./SketchFabEmbed'));
const LazyModelImageViewer = lazy(() => import('./ModelImageViewer'));
Expand All @@ -25,19 +26,17 @@ const ModelViewer = ({ model, fallbackSrc, memoryStatus }) => {
const { overLoad } = memoryStatus;

const viewer = overLoad ? (
<Suspense fallback={<Loading />}>
<LazyModelImageViewer src={fallbackSrc} />
</Suspense>
<LazyModelImageViewer src={fallbackSrc} />
) : (
<Suspense fallback={<Loading />}>
<SketchFabEmbed model={model} />
</Suspense>
<SketchFabEmbed model={model} />
);

return (
<Fragment>
{viewer}
</Fragment>
<LazyLoadingErrorBoundary>
<Suspense fallback={<Loading />}>
{viewer}
</Suspense>
</LazyLoadingErrorBoundary>
);
};

Expand Down
7 changes: 0 additions & 7 deletions cra-memory-considerate-loading-sketchfab/src/logo.svg

This file was deleted.

16 changes: 16 additions & 0 deletions cra-memory-considerate-loading-sketchfab/src/serviceWorker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// This optional code is used to register a service worker.
// register() is not called by default.

Expand Down
19 changes: 0 additions & 19 deletions cra-memory-considerate-loading/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
text-align: center;
}

.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 40vmin;
pointer-events: none;
}

.App-header {
background-color: #282c34;
min-height: 100vh;
Expand All @@ -34,16 +28,3 @@
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
16 changes: 16 additions & 0 deletions cra-memory-considerate-loading/src/App.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import React from 'react';

/*** Adapted from https://reactjs.org/docs/error-boundaries.html ***/
export default class LazyLoadingErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = {hasError: false};
}

static getDerivedStateFromError(error) {
return {hasError: true};
}

render() {
if (this.state.hasError) {
return (
<div>
<button onClick={() => window.location.reload()}>Click to Reload</button>
<p>Lazy-loading failed!</p>
</div>
);
}

return this.props.children;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/

import React, { Fragment, Suspense, lazy } from 'react';
import React, { Suspense, lazy } from 'react';

import Loading from '../Loading';
import LazyLoadingErrorBoundary from '../LazyLoadingErrorBoundary';

const LazyModel3DViewer = lazy(() => import('./Model3DViewer'));
const LazyModelImageViewer = lazy(() => import('./ModelImageViewer'));
Expand All @@ -25,19 +26,17 @@ const ModelViewer = ({ src, fallbackSrc, memoryStatus }) => {
const { overLoad } = memoryStatus;

const viewer = overLoad ? (
<Suspense fallback={<Loading />}>
<LazyModelImageViewer src={fallbackSrc} />
</Suspense>
<LazyModelImageViewer src={fallbackSrc} />
) : (
<Suspense fallback={<Loading />}>
<LazyModel3DViewer src={src} />
</Suspense>
<LazyModel3DViewer src={src} />
);

return (
<Fragment>
<LazyLoadingErrorBoundary>
<Suspense fallback={<Loading />}>
{viewer}
</Fragment>
</Suspense>
</LazyLoadingErrorBoundary>
);
};

Expand Down
7 changes: 0 additions & 7 deletions cra-memory-considerate-loading/src/logo.svg

This file was deleted.

Loading

0 comments on commit 1309d73

Please sign in to comment.