Skip to content

Commit 6bf07cb

Browse files
authored
Update dependency loading screen (codesandbox#412)
* Change loading screen * Fix loading screen info
1 parent e8a1cf1 commit 6bf07cb

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
lines changed

packages/app/src/app/components/sandbox/Preview/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ export default class Preview extends React.PureComponent<Props, State> {
128128
return;
129129
}
130130

131+
if (prevProps.dependencies !== this.props.dependencies) {
132+
// Changed dependencies
133+
this.executeCodeImmediately();
134+
return;
135+
}
136+
131137
if (prevProps.module.id !== this.props.module.id) {
132138
if (prevProps.isInProjectView && this.props.isInProjectView) {
133139
// If user only navigated while watching project

packages/app/src/sandbox/npm/fetch-dependencies.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async function getDependencies(dependencies: Object) {
124124
);
125125
return bucketManifest;
126126
} catch (e) {
127-
dispatch(actions.notifications.show('Resolving dependencies...'));
127+
setScreen({ type: 'loading', text: 'Resolving Dependencies...' });
128128

129129
// The dep has not been generated yet...
130130
const { url } = await requestPackager(
@@ -141,10 +141,8 @@ export default async function fetchDependencies(npmDependencies: Dependencies) {
141141
// New Packager flow
142142

143143
try {
144-
dispatch(actions.notifications.show('Downloading dependencies...'));
145144
const result = await getDependencies(npmDependencies);
146-
setScreen({ type: 'loading', text: 'Transpiling...' });
147-
dispatch(actions.notifications.show('Dependencies loaded!', 'success'));
145+
setScreen({ type: 'loading', text: 'Transpiling Modules...' });
148146

149147
return result;
150148
} catch (e) {

packages/app/src/sandbox/npm/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ export default async function loadDependencies(dependencies: NPMDependencies) {
3636

3737
const data = await fetchDependencies(dependenciesWithoutTypings);
3838
manifest = data;
39-
40-
setScreen({ type: 'loading', text: 'Downloading Dependencies...' });
4139
}
4240
} else {
4341
manifest = null;

packages/app/src/sandbox/status-screen/index.js

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @flow
22
// This is the loading screen
3+
import loadingHtml from './loading-screen.html';
34

45
type LoadingScreen = {
56
type: 'loading',
@@ -10,37 +11,64 @@ type Screen = LoadingScreen;
1011

1112
let currentScreen: ?Screen = null;
1213
let firstLoaded = null;
13-
const LOADING_SCREEN_ID = 'csb-loading-screen';
14+
15+
let iframeReference = null;
16+
17+
function changeText(text: string) {
18+
if (iframeReference) {
19+
iframeReference.contentDocument
20+
.getElementsByClassName('text')
21+
.item(0).textContent = text;
22+
}
23+
}
24+
25+
function createOverlay() {
26+
return new Promise(resolve => {
27+
const iframe = document.createElement('iframe');
28+
29+
iframe.setAttribute(
30+
'style',
31+
`position: fixed; top: 0; left: 0; width: 100%; height: 100%; border: none; z-index: 2147483647;`
32+
);
33+
34+
iframe.onload = () => {
35+
iframe.contentDocument.body.innerHTML = loadingHtml;
36+
iframeReference = iframe;
37+
38+
if (currentScreen) {
39+
changeText(currentScreen.text);
40+
}
41+
resolve();
42+
};
43+
44+
document.body.appendChild(iframe);
45+
});
46+
}
1447

1548
export function resetScreen() {
16-
if (document.getElementById(LOADING_SCREEN_ID)) {
17-
document.body.innerHTML = '';
18-
currentScreen = null;
49+
currentScreen = null;
50+
try {
51+
window.document.body.removeChild(iframeReference);
52+
iframeReference = null;
53+
} catch (e) {
54+
/* nothing */
1955
}
2056
}
2157

2258
export default function setScreen(screen: Screen) {
23-
if (document.getElementById(LOADING_SCREEN_ID)) {
24-
if (!firstLoaded && !currentScreen) {
59+
if (!iframeReference && !currentScreen) {
60+
if (!firstLoaded) {
2561
// Give the illusion of faster loading by showing the loader screen later
26-
firstLoaded = setTimeout(async () => {
62+
firstLoaded = setTimeout(() => {
2763
if (currentScreen) {
28-
const loadingHtml = await import(/* webpackChunkName: 'loading-screen' */ './loading-screen.html');
29-
30-
if (currentScreen) {
31-
document.body.innerHTML = loadingHtml;
32-
33-
document.getElementsByClassName('text').item(0).textContent =
34-
currentScreen.text;
35-
}
64+
createOverlay();
3665
}
37-
}, 500);
38-
} else if (screen.type === 'loading') {
39-
if (document.getElementsByClassName('text').item(0)) {
40-
document.getElementsByClassName('text').item(0).textContent =
41-
screen.text;
42-
}
66+
}, 600);
67+
} else {
68+
createOverlay(screen.text);
4369
}
70+
} else if (screen.type === 'loading') {
71+
changeText(screen.text);
4472
}
4573

4674
currentScreen = screen;

0 commit comments

Comments
 (0)