Skip to content

Commit e2b49cd

Browse files
author
Ives van Hoorne
committed
Update state handling and skipEval scenarios
1 parent 2daf65c commit e2b49cd

File tree

6 files changed

+53
-19
lines changed

6 files changed

+53
-19
lines changed

packages/app/src/sandbox/compile.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ async function compile({
299299
actionsEnabled = hasActions;
300300
handleExternalResources(externalResources);
301301

302+
let managerModuleToTranspile = null;
302303
try {
303304
const templateDefinition = getDefinition(template);
304305
const configurations = parseConfigurations(
@@ -373,7 +374,7 @@ async function compile({
373374
}
374375

375376
const main = absolute(foundMain);
376-
const managerModuleToTranspile = modules[main];
377+
managerModuleToTranspile = modules[main];
377378

378379
dispatch({ type: 'status', status: 'transpiling' });
379380

@@ -499,15 +500,8 @@ async function compile({
499500

500501
debug(`Total time: ${Date.now() - startTime}ms`);
501502

502-
const managerState = {
503-
...manager.serialize(),
504-
};
505-
delete managerState.cachedPaths;
506-
managerState.entry = managerModuleToTranspile.path;
507-
508503
dispatch({
509504
type: 'success',
510-
state: managerState,
511505
});
512506

513507
manager.save();
@@ -529,6 +523,21 @@ async function compile({
529523
window.dispatchEvent(event);
530524

531525
hadError = true;
526+
} finally {
527+
if (manager) {
528+
const managerState = {
529+
...manager.serialize(),
530+
};
531+
delete managerState.cachedPaths;
532+
managerState.entry = managerModuleToTranspile
533+
? managerModuleToTranspile.path
534+
: null;
535+
536+
dispatch({
537+
type: 'state',
538+
state: managerState,
539+
});
540+
}
532541
}
533542
firstLoad = false;
534543

packages/react-sandpack/src/components/SandpackProvider/SandpackProvider.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default class SandpackProvider extends React.PureComponent<
7979
}
8080

8181
handleMessage = (m: any) => {
82-
if (m.type === 'success') {
82+
if (m.type === 'state') {
8383
this.setState({ managerState: m.state });
8484
} else if (m.type === 'start') {
8585
this.setState({ errors: [] });
@@ -131,6 +131,12 @@ export default class SandpackProvider extends React.PureComponent<
131131
return newFiles;
132132
}
133133

134+
getOptions = () => {
135+
return {
136+
skipEval: this.props.skipEval,
137+
};
138+
};
139+
134140
setupFrame = (el: HTMLIFrameElement) => {
135141
if (el) {
136142
this.manager = new Manager(
@@ -143,9 +149,7 @@ export default class SandpackProvider extends React.PureComponent<
143149
),
144150
template: this.props.template,
145151
},
146-
{
147-
skipEval: this.props.skipEval,
148-
}
152+
this.getOptions()
149153
);
150154

151155
this.iframe = el;
@@ -180,6 +184,10 @@ export default class SandpackProvider extends React.PureComponent<
180184

181185
this.updateFiles(newFiles);
182186
}
187+
188+
if (this.manager && this.props.skipEval !== props.skipEval) {
189+
this.manager.updateOptions(this.getOptions());
190+
}
183191
}
184192

185193
componentWillUnmount() {

packages/react-sandpack/stories/InTheWild.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ stories.addWithJSX('babel', () => (
138138
}}
139139
entry="/index.js"
140140
template="babel-repl"
141-
skipEval
142141
>
143142
<div
144143
style={{

packages/sandpack/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@
7676
},
7777
"dependencies": {
7878
"codesandbox-api": "^0.0.18",
79-
"codesandbox-import-utils": "^1.2.3"
79+
"codesandbox-import-utils": "^1.2.3",
80+
"lodash.isequal": "^4.5.0"
8081
},
8182
"devDependencies": {
8283
"@types/jest": "^22.0.0",
84+
"@types/lodash.isequal": "^4.5.2",
8385
"@types/node": "^9.3.0",
8486
"colors": "^1.1.2",
8587
"commitizen": "^2.9.6",

packages/sandpack/src/manager/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import { dispatch, listen, registerFrame } from 'codesandbox-api';
33
import { getTemplate } from 'codesandbox-import-utils/lib/create-sandbox/templates';
44

5+
import isEqual from 'lodash.isequal';
6+
57
import generatePackageJSON, {
68
getPackageJSON,
79
} from '../utils/generate-package-json';
@@ -68,7 +70,6 @@ export default class PreviewManager {
6870
iframe: HTMLIFrameElement;
6971
options: IManagerOptions;
7072
listener?: Function;
71-
skipEval: boolean;
7273
bundlerURL: string;
7374

7475
sandboxInfo: ISandboxInfo;
@@ -99,8 +100,6 @@ export default class PreviewManager {
99100
}
100101
this.iframe.src = this.bundlerURL;
101102

102-
this.skipEval = options.skipEval || false;
103-
104103
this.listener = listen((message: any) => {
105104
switch (message.type) {
106105
case 'initialized': {
@@ -118,6 +117,13 @@ export default class PreviewManager {
118117
});
119118
}
120119

120+
updateOptions(options: IManagerOptions) {
121+
if (!isEqual(this.options, options)) {
122+
this.options = options;
123+
this.updatePreview();
124+
}
125+
}
126+
121127
updatePreview(sandboxInfo = this.sandboxInfo) {
122128
this.sandboxInfo = sandboxInfo;
123129

@@ -165,7 +171,7 @@ export default class PreviewManager {
165171
this.sandboxInfo.template ||
166172
getTemplate(packageJSON, normalizedModules),
167173
showOpenInCodeSandbox: true,
168-
skipEval: this.skipEval,
174+
skipEval: this.options.skipEval || false,
169175
});
170176
}
171177

yarn.lock

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,16 @@
585585
version "22.2.0"
586586
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-22.2.0.tgz#55ce83139f7ad1b48b414c3927746614c6963c0f"
587587

588+
"@types/lodash.isequal@^4.5.2":
589+
version "4.5.2"
590+
resolved "https://registry.yarnpkg.com/@types/lodash.isequal/-/lodash.isequal-4.5.2.tgz#adbdff67f7c956ed703009e5466a34eeddb0b712"
591+
dependencies:
592+
"@types/lodash" "*"
593+
594+
"@types/lodash@*":
595+
version "4.14.104"
596+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.104.tgz#53ee2357fa2e6e68379341d92eb2ecea4b11bb80"
597+
588598
589599
version "4.14.99"
590600
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.99.tgz#e6e10c0a4cc16c7409b3181f1e66880d2fb7d4dc"
@@ -11374,7 +11384,7 @@ lodash.isarray@^3.0.0:
1137411384
version "3.0.4"
1137511385
resolved "https://registry.yarnpkg.com/lodash.isarray/-/lodash.isarray-3.0.4.tgz#79e4eb88c36a8122af86f844aa9bcd851b5fbb55"
1137611386

11377-
lodash.isequal@^4.0.0:
11387+
lodash.isequal@^4.0.0, lodash.isequal@^4.5.0:
1137811388
version "4.5.0"
1137911389
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
1138011390

0 commit comments

Comments
 (0)