-
Notifications
You must be signed in to change notification settings - Fork 286
/
startup-wrapper.js
279 lines (245 loc) · 8.31 KB
/
startup-wrapper.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
/* eslint camelcase:0 */
/**
This is a wrapper for `ember-debug.js`
Wraps the script in a function,
and ensures that the script is executed
only after the dom is ready
and the application has initialized.
Also responsible for sending the first tree.
**/
/*eslint prefer-spread: 0 */
/* globals Ember, adapter, env, requireModule */
var currentAdapter = 'basic';
if (typeof adapter !== 'undefined') {
currentAdapter = adapter;
}
var currentEnv = 'production';
if (typeof env !== 'undefined') {
currentEnv = env;
}
// @formatter:off
var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}};
// @formatter:on
(function(adapter) {
var onReady = requireModule('ember-debug/utils/on-ready').onReady;
var compareVersion = requireModule('ember-debug/utils/version').compareVersion;
onEmberReady(function() {
// global to prevent injection
if (window.NO_EMBER_DEBUG) {
return;
}
if (!versionTest(Ember.VERSION, EMBER_VERSIONS_SUPPORTED)) {
// Wrong inspector version. Redirect to the correct version.
sendVersionMiss();
return;
}
// prevent from injecting twice
if (!Ember.EmberInspectorDebugger) {
// Make sure we only work for the supported version
define('ember-debug/config', function() {
return {
default: {
environment: currentEnv
}
};
});
window.EmberInspector = Ember.EmberInspectorDebugger = requireModule('ember-debug/main')['default'];
Ember.EmberInspectorDebugger.Adapter = requireModule('ember-debug/adapters/' + adapter)['default'];
onApplicationStart(function appStarted(instance) {
let app = instance.application;
if (!('__inspector__booted' in app)) {
// Watch for app reset/destroy
app.reopen({
reset: function() {
this.__inspector__booted = false;
this._super.apply(this, arguments);
}
});
}
if (instance && !('__inspector__booted' in instance)) {
instance.reopen({
// Clean up on instance destruction
willDestroy() {
if (Ember.EmberInspectorDebugger.get('owner') === instance) {
Ember.EmberInspectorDebugger.destroyContainer();
Ember.EmberInspectorDebugger.clear();
}
return this._super.apply(this, arguments);
}
});
if (!Ember.EmberInspectorDebugger._application) {
bootEmberInspector(instance);
}
}
});
}
});
function bootEmberInspector(appInstance) {
appInstance.application.__inspector__booted = true;
appInstance.__inspector__booted = true;
// Boot the inspector (or re-boot if already booted, for example in tests)
Ember.EmberInspectorDebugger.set('_application', appInstance.application);
Ember.EmberInspectorDebugger.set('owner', appInstance);
Ember.EmberInspectorDebugger.start(true);
}
function onEmberReady(callback) {
var triggered = false;
var triggerOnce = function(string) {
if (triggered) {
return;
}
if (!window.Ember) {
return;
}
// `Ember.Application` load hook triggers before all of Ember is ready.
// In this case we ignore and wait for the `Ember` load hook.
if (!window.Ember.RSVP) {
return;
}
triggered = true;
callback();
};
// Newest Ember versions >= 1.10
window.addEventListener('Ember', triggerOnce, false);
// Old Ember versions
window.addEventListener('Ember.Application', function() {
if (window.Ember && window.Ember.VERSION && compareVersion(window.Ember.VERSION, '1.10.0') === 1) {
// Ember >= 1.10 should be handled by `Ember` load hook instead.
return;
}
triggerOnce();
}, false);
// Oldest Ember versions or if this was injected after Ember has loaded.
onReady(triggerOnce);
}
// There's probably a better way
// to determine when the application starts
// but this definitely works
function onApplicationStart(callback) {
if (typeof Ember === 'undefined') {
return;
}
const adapterInstance = requireModule('ember-debug/adapters/' + currentAdapter)['default'].create();
adapterInstance.onMessageReceived(function(message) {
if (message.type === 'app-picker-loaded') {
sendApps(adapterInstance, getApplications());
}
if (message.type === 'app-selected') {
const appInstance = getApplications().find(app => Ember.guidFor(app) === message.applicationId);
if (appInstance && appInstance.__deprecatedInstance__) {
bootEmberInspector(appInstance.__deprecatedInstance__);
}
}
});
var apps = getApplications();
sendApps(adapterInstance, apps);
var app;
for (var i = 0, l = apps.length; i < l; i++) {
app = apps[i];
// We check for the existance of an application instance because
// in Ember > 3 tests don't destroy the app when they're done but the app has no booted instances.
if (app._readinessDeferrals === 0) {
let instance = app.__deprecatedInstance__ || (app._applicationInstances && app._applicationInstances[0]);
if (instance) {
// App started
setupInstanceInitializer(app, callback);
callback(instance);
break;
}
}
}
Ember.Application.initializer({
name: 'ember-inspector-booted',
initialize: function(app) {
setupInstanceInitializer(app, callback);
}
});
}
function setupInstanceInitializer(app, callback) {
if (!app.__inspector__setup) {
app.__inspector__setup = true;
// We include the app's guid in the initializer name because in Ember versions < 3
// registering an instance initializer with the same name, even if on a different app,
// triggers an error because instance initializers seem to be global instead of per app.
app.instanceInitializer({
name: 'ember-inspector-app-instance-booted-' + Ember.guidFor(app),
initialize: function(instance) {
callback(instance);
}
});
}
}
/**
* Get all the Ember.Application instances from Ember.Namespace.NAMESPACES
* and add our own applicationId and applicationName to them
* @return {*}
*/
function getApplications() {
var namespaces = Ember.A(Ember.Namespace.NAMESPACES);
var apps = namespaces.filter(function(namespace) {
return namespace instanceof Ember.Application;
});
return apps.map(function(app) {
// Add applicationId and applicationName to the app
var applicationId = Ember.guidFor(app);
var applicationName = app.name || app.modulePrefix || `(unknown app - ${applicationId})`;
Object.assign(app, {
applicationId,
applicationName
});
return app;
});
}
/**
* This function is called if the app's Ember version
* is not supported by this version of the inspector.
*
* It sends a message to the inspector app to redirect
* to an inspector version that supports this Ember version.
*/
function sendVersionMiss() {
var adapter = requireModule('ember-debug/adapters/' + currentAdapter)['default'].create();
adapter.onMessageReceived(function(message) {
if (message.type === 'check-version') {
sendVersionMismatch();
}
});
sendVersionMismatch();
function sendVersionMismatch() {
adapter.sendMessage({
name: 'version-mismatch',
version: Ember.VERSION,
from: 'inspectedWindow'
});
}
}
function sendApps(adapter, apps) {
const serializedApps = apps.map(app => {
return {
applicationName: app.applicationName,
applicationId: app.applicationId
}
});
adapter.sendMessage({
type: 'apps-loaded',
apps: serializedApps,
from: 'inspectedWindow'
});
}
/**
* Checks if a version is between two different versions.
* version should be >= left side, < right side
*
* @param {String} version1
* @param {String} version2
* @return {Boolean}
*/
function versionTest(version, between) {
var fromVersion = between[0];
var toVersion = between[1];
if (compareVersion(version, fromVersion) === -1) {
return false;
}
return !toVersion || compareVersion(version, toVersion) === -1;
}
}(currentAdapter));