@@ -123,13 +123,10 @@ class EmberApp {
123123 /**
124124 * @private
125125 *
126- * Initializes the sandbox by evaluating the Ember app's JavaScript
127- * code, then retrieves the application factory from the sandbox and creates a new
128- * `Ember.Application`.
126+ * Loads the app and vendor files in the sandbox (Node vm).
129127 *
130- * @returns {Ember.Application } the Ember application from the sandbox
131- */
132- retrieveSandboxedApp ( ) {
128+ */
129+ loadAppFiles ( ) {
133130 let sandbox = this . sandbox ;
134131 let appFilePath = this . appFilePath ;
135132 let vendorFilePath = this . vendorFilePath ;
@@ -146,6 +143,17 @@ class EmberApp {
146143
147144 sandbox . eval ( appFile , appFilePath ) ;
148145 debug ( "app file evaluated" ) ;
146+ }
147+
148+ /**
149+ * @private
150+ *
151+ * Create the ember application in the sandbox.
152+ *
153+ */
154+ createEmberApp ( ) {
155+ let sandbox = this . sandbox ;
156+ let appFilePath = this . appFilePath ;
149157
150158 // Retrieve the application factory from within the sandbox
151159 let AppFactory = sandbox . run ( function ( ctx ) {
@@ -161,6 +169,21 @@ class EmberApp {
161169 return AppFactory [ 'default' ] ( ) ;
162170 }
163171
172+ /**
173+ * @private
174+ *
175+ * Initializes the sandbox by evaluating the Ember app's JavaScript
176+ * code, then retrieves the application factory from the sandbox and creates a new
177+ * `Ember.Application`.
178+ *
179+ * @returns {Ember.Application } the Ember application from the sandbox
180+ */
181+ retrieveSandboxedApp ( ) {
182+ this . loadAppFiles ( ) ;
183+
184+ return this . createEmberApp ( ) ;
185+ }
186+
164187 /**
165188 * Destroys the app and its sandbox.
166189 */
@@ -186,6 +209,39 @@ class EmberApp {
186209 } ) ;
187210 }
188211
212+ /**
213+ * @private
214+ *
215+ * Main funtion that creates the app instance for every `visit` request, boots
216+ * the app instance and then visits the given route and destroys the app instance
217+ * when the route is finished its render cycle.
218+ *
219+ * @param {string } path the URL path to render, like `/photos/1`
220+ * @param {Object } fastbootInfo An object holding per request info
221+ * @param {Object } bootOptions An object containing the boot options that are used by
222+ * by ember to decide whether it needs to do rendering or not.
223+ * @param {Object } result
224+ * @return {Promise<instance> } instance
225+ */
226+ visitRoute ( path , fastbootInfo , bootOptions , result ) {
227+ let instance ;
228+
229+ return this . buildAppInstance ( )
230+ . then ( appInstance => {
231+ instance = appInstance ;
232+ result . instance = instance ;
233+ registerFastBootInfo ( fastbootInfo , instance ) ;
234+
235+ return instance . boot ( bootOptions ) ;
236+ } )
237+ . then ( ( ) => result . instanceBooted = true )
238+ . then ( ( ) => instance . visit ( path , bootOptions ) )
239+ . then ( ( ) => waitForApp ( instance ) )
240+ . then ( ( ) => {
241+ return instance ;
242+ } ) ;
243+ }
244+
189245 /**
190246 * Creates a new application instance and renders the instance at a specific
191247 * URL, returning a promise that resolves to a {@link Result}. The `Result`
@@ -225,8 +281,6 @@ class EmberApp {
225281
226282 let doc = bootOptions . document ;
227283
228- let instance ;
229-
230284 let result = new Result ( {
231285 doc : doc ,
232286 html : html ,
@@ -246,17 +300,11 @@ class EmberApp {
246300 } , destroyAppInstanceInMs ) ;
247301 }
248302
249- return this . buildAppInstance ( )
303+ let instance ;
304+ return this . visitRoute ( path , fastbootInfo , bootOptions , result )
250305 . then ( appInstance => {
251306 instance = appInstance ;
252- result . instance = instance ;
253- registerFastBootInfo ( fastbootInfo , instance ) ;
254-
255- return instance . boot ( bootOptions ) ;
256307 } )
257- . then ( ( ) => result . instanceBooted = true )
258- . then ( ( ) => instance . visit ( path , bootOptions ) )
259- . then ( ( ) => waitForApp ( instance ) )
260308 . then ( ( ) => {
261309 if ( ! disableShoebox ) {
262310 // if shoebox is not disabled, then create the shoebox and send API data
0 commit comments