Skip to content

Commit b155d0f

Browse files
committed
Make sure the same sandbox class and sandbox globals are used during reloads
When reloading the app (for exampel during rebuilds), we need to make sure we use the same sandbox class and the same set of sandboxGlobals that were used to initial creation of AppInstance. Discovered this while working on the serve refactor in ember-cli-fastboot.
1 parent cec3c14 commit b155d0f

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

src/ember-app.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class EmberApp {
6060
* @param {Object} [sandboxGlobals={}] any additional variables to expose in the sandbox or override existing in the sandbox
6161
*/
6262
buildSandbox(distPath, sandboxClass, sandboxGlobals) {
63-
let Sandbox = sandboxClass || require('./vm-sandbox');
6463
let sandboxRequire = this.buildWhitelistedRequire(this.moduleWhitelist, distPath);
6564
let config = this.appConfig;
6665
function appConfig() {
@@ -81,7 +80,7 @@ class EmberApp {
8180
}
8281
}
8382

84-
return new Sandbox({
83+
return new sandboxClass({
8584
globals: globals
8685
});
8786
}

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class FastBoot {
4545
options = options || {};
4646

4747
this.distPath = options.distPath;
48-
this.sandbox = options.sandbox;
48+
this.sandbox = options.sandbox || require('./vm-sandbox');
4949
this.sandboxGlobals = options.sandboxGlobals || {};
5050
this.resilient = !!options.resilient || false;
5151

@@ -91,11 +91,14 @@ class FastBoot {
9191
this._app.destroy();
9292
}
9393

94-
this._buildEmberApp(options ? options.distPath : null);
94+
options = options || {};
95+
this._buildEmberApp(options.distPath || null);
9596
}
9697

9798
_buildEmberApp(distPath, sandbox, sandboxGlobals) {
9899
distPath = distPath || this.distPath;
100+
sandbox = sandbox || this.sandbox;
101+
sandboxGlobals = sandboxGlobals || this.sandboxGlobals;
99102

100103
if (!distPath) {
101104
throw new Error('You must instantiate FastBoot with a distPath ' +

test/fastboot-test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,62 @@ describe("FastBoot", function() {
202202
}
203203
});
204204

205+
it("can reload the app using the same sandboxGlobals", function() {
206+
var fastboot = new FastBoot({
207+
distPath: fixture('basic-app'),
208+
sandboxGlobals: {
209+
foo: 5,
210+
najax: 'undefined',
211+
myVar: 'undefined'
212+
}
213+
});
214+
215+
return fastboot.visit('/')
216+
.then(r => r.html())
217+
.then(html => expect(html).to.match(/Welcome to Ember/))
218+
.then(hotReloadApp)
219+
.then(() => fastboot.visit('/foo'))
220+
.then(r => r.html())
221+
.then((html) => {
222+
expect(html).to.match(/foo from sandbox: 5/);
223+
expect(html).to.match(/najax in sandbox: undefined/);
224+
});
225+
226+
function hotReloadApp() {
227+
fastboot.reload({
228+
distPath: fixture('custom-sandbox')
229+
});
230+
}
231+
});
232+
233+
it("can reload the app using the same sandbox class", function() {
234+
var fastboot = new FastBoot({
235+
distPath: fixture('basic-app'),
236+
sandbox: CustomSandbox,
237+
sandboxGlobals: {
238+
myVar: 2,
239+
foo: 'undefined',
240+
najax: 'undefined'
241+
}
242+
});
243+
244+
return fastboot.visit('/')
245+
.then(r => r.html())
246+
.then(html => expect(html).to.match(/Welcome to Ember/))
247+
.then(hotReloadApp)
248+
.then(() => fastboot.visit('/foo'))
249+
.then(r => r.html())
250+
.then((html) => {
251+
expect(html).to.match(/myVar in sandbox: 2/);
252+
});
253+
254+
function hotReloadApp() {
255+
fastboot.reload({
256+
distPath: fixture('custom-sandbox')
257+
});
258+
}
259+
});
260+
205261
it("reads the config from package.json", function() {
206262
var fastboot = new FastBoot({
207263
distPath: fixture('config-app')

0 commit comments

Comments
 (0)