-
Notifications
You must be signed in to change notification settings - Fork 138
/
router-ext.js
291 lines (254 loc) · 7.88 KB
/
router-ext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import Router from '@ember/routing/router';
import RSVP from 'rsvp';
import { assert } from '@ember/debug';
import { get } from '@ember/object';
import { getOwner } from '@ember/application';
import Ember from 'ember';
import Route from '@ember/routing/route';
const defaultSerialize = Route.proto().serialize;
function hasDefaultSerialize(handler) {
return handler.serialize === defaultSerialize;
}
const {
Logger: { info }
} = Ember;
Router.reopen({
init() {
this._super(...arguments);
this._enginePromises = Object.create(null);
this._seenHandlers = Object.create(null);
// We lookup the asset loader service instead of injecting it so that we
// don't blow up unit tests for consumers
this._assetLoader = getOwner(this).lookup('service:asset-loader');
},
/**
* When going to an Engine route, we check for QP meta in the BucketCache
* instead of checking the Route (which may not exist yet). We populate
* the BucketCache after loading the Route the first time.
*
* @override
*/
_getQPMeta(handlerInfo) {
let routeName = handlerInfo.name;
let isWithinEngine = this._engineInfoByRoute[routeName];
let hasBeenLoaded = this._seenHandlers[routeName];
if (isWithinEngine && !hasBeenLoaded) {
return undefined;
}
return this._super(...arguments);
},
/**
* We override this to fetch assets when crossing into a lazy Engine for the
* first time. For other cases we do the normal thing.
*
* @override
*/
_getHandlerFunction() {
let seen = this._seenHandlers;
let owner = getOwner(this);
return name => {
let engineInfo = this._engineInfoByRoute[name];
if (engineInfo) {
let engineInstance = this._getEngineInstance(engineInfo);
if (engineInstance) {
return this._getHandlerForEngine(
seen,
name,
engineInfo.localFullName,
engineInstance
);
} else {
return this._loadEngineInstance(engineInfo).then(instance => {
return this._getHandlerForEngine(
seen,
name,
engineInfo.localFullName,
instance
);
});
}
}
// If we don't cross into an Engine, then the routeName and localRouteName
// are the same.
return this._internalGetHandler(seen, name, name, owner);
};
},
/**
* Gets the handler for a route from an Engine instance, proxies to the
* _internalGetHandler method.
*
* @private
* @method _getHandlerForEngine
* @param {Object} seen
* @param {String} routeName
* @param {String} localRouteName
* @param {Owner} routeOwner
* @return {EngineInstance} engineInstance
*/
_getHandlerForEngine(seen, routeName, localRouteName, engineInstance) {
let handler = this._internalGetHandler(
seen,
routeName,
localRouteName,
engineInstance
);
if (!hasDefaultSerialize(handler)) {
throw new Error(
'Defining a custom serialize method on an Engine route is not supported.'
);
}
return handler;
},
/**
* This method is responsible for actually doing the lookup in getHandler.
* It is separate so that it can be used from different code paths.
*
* @private
* @method _internalGetHandler
* @param {Object} seen
* @param {String} routeName
* @param {String} localRouteName
* @param {Owner} routeOwner
* @return {Route} handler
*/
_internalGetHandler(seen, routeName, localRouteName, routeOwner) {
const fullRouteName = 'route:' + localRouteName;
let handler = routeOwner.lookup(fullRouteName);
if (seen[routeName] && handler) {
return handler;
}
seen[routeName] = true;
if (!handler) {
const DefaultRoute = routeOwner.factoryFor
? routeOwner.factoryFor('route:basic').class
: routeOwner._lookupFactory('route:basic');
routeOwner.register(fullRouteName, DefaultRoute.extend());
handler = routeOwner.lookup(fullRouteName);
if (get(this, 'namespace.LOG_ACTIVE_GENERATION')) {
info(`generated -> ${fullRouteName}`, { fullName: fullRouteName });
}
}
handler._setRouteName(localRouteName);
if (handler._populateQPMeta) {
handler._populateQPMeta();
}
return handler;
},
/**
* Checks the owner to see if it has a registration for an Engine. This is a
* proxy to tell if an Engine's assets are loaded or not.
*
* @private
* @method _engineIsLoaded
* @param {String} name
* @return {Boolean}
*/
_engineIsLoaded(name) {
let owner = getOwner(this);
return owner.hasRegistration('engine:' + name);
},
/**
* Registers an Engine that was recently loaded.
*
* @private
* @method _registerEngine
* @param {String} name
* @return {Void}
*/
_registerEngine(name) {
let owner = getOwner(this);
if (!owner.hasRegistration('engine:' + name)) {
owner.register(
'engine:' + name,
window.require(name + '/engine').default
);
}
},
/**
* Gets the instance of an Engine with the specified name and instanceId.
*
* @private
* @method _getEngineInstance
* @param {Object} engineInfo
* @param {String} engineInfo.name
* @param {String} engineInfo.instanceId
* @return {EngineInstance}
*/
_getEngineInstance({ name, instanceId }) {
let engineInstances = this._engineInstances;
return engineInstances[name] && engineInstances[name][instanceId];
},
/**
* Loads an instance of an Engine with the specified name and instanceId.
* Returns a Promise for both Eager and Lazy Engines. This function loads the
* assets for any Lazy Engines.
*
* @private
* @method _loadEngineInstance
* @param {Object} engineInfo
* @param {String} engineInfo.name
* @param {String} engineInfo.instanceId
* @param {String} engineInfo.mountPoint
* @return {Promise<EngineInstance>}
*/
_loadEngineInstance({ name, instanceId, mountPoint }) {
let enginePromises = this._enginePromises;
if (!enginePromises[name]) {
enginePromises[name] = Object.create(null);
}
let enginePromise = enginePromises[name][instanceId];
// We already have a Promise for this engine instance
if (enginePromise) {
return enginePromise;
}
if (this._engineIsLoaded(name)) {
// The Engine is loaded, but has no Promise
enginePromise = RSVP.resolve();
} else {
// The Engine is not loaded and has no Promise
enginePromise = this._assetLoader.loadBundle(name).then(
() => this._registerEngine(name),
error => {
enginePromises[name][instanceId] = undefined;
throw error;
}
);
}
return (enginePromises[name][instanceId] = enginePromise.then(() => {
return this._constructEngineInstance({ name, instanceId, mountPoint });
}));
},
/**
* Constructs an instance of an Engine based on an engineInfo object.
* TODO: Figure out if this works with nested Engines.
*
* @private
* @method _constructEngineInstance
* @param {Object} engineInfo
* @param {String} engineInfo.name
* @param {String} engineInfo.instanceId
* @param {String} engineInfo.mountPoint
* @return {Promise<EngineInstance>}
*/
_constructEngineInstance({ name, instanceId, mountPoint }) {
let owner = getOwner(this);
assert(
"You attempted to mount the engine '" +
name +
"' in your router map, but the engine cannot be found.",
owner.hasRegistration(`engine:${name}`)
);
let engineInstances = this._engineInstances;
if (!engineInstances[name]) {
engineInstances[name] = Object.create(null);
}
let engineInstance = owner.buildChildEngineInstance(name, {
routable: true,
mountPoint,
});
engineInstances[name][instanceId] = engineInstance;
return engineInstance.boot().then(() => {
return engineInstance;
});
},
});