-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
renderer.js
419 lines (331 loc) · 10.3 KB
/
renderer.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import { RootReference } from './utils/references';
import {
run,
setHasViews,
runInTransaction
} from 'ember-metal';
import { CURRENT_TAG, UNDEFINED_REFERENCE } from '@glimmer/reference';
import {
fallbackViewRegistry,
getViewElement,
setViewElement,
getViewId
} from 'ember-views';
import { BOUNDS } from './component';
import { RootComponentDefinition } from './component-managers/root';
import { TopLevelOutletComponentDefinition } from './component-managers/outlet';
import { assert } from 'ember-debug';
const { backburner } = run;
class DynamicScope {
constructor(view, outletState, rootOutletState, targetObject) {
this.view = view;
this.outletState = outletState;
this.rootOutletState = rootOutletState;
}
child() {
return new DynamicScope(
this.view, this.outletState, this.rootOutletState
);
}
get(key) {
assert(`Using \`-get-dynamic-scope\` is only supported for \`outletState\` (you used \`${key}\`).`, key === 'outletState');
return this.outletState;
}
set(key, value) {
assert(`Using \`-with-dynamic-scope\` is only supported for \`outletState\` (you used \`${key}\`).`, key === 'outletState');
this.outletState = value;
return value;
}
}
class RootState {
constructor(root, env, template, self, parentElement, dynamicScope) {
assert(`You cannot render \`${self.value()}\` without a template.`, template);
this.id = getViewId(root);
this.env = env;
this.root = root;
this.result = undefined;
this.shouldReflush = false;
this.destroyed = false;
this._removing = false;
let options = this.options = {
alwaysRevalidate: false
};
this.render = () => {
let iterator = template.render(self, parentElement, dynamicScope);
let iteratorResult;
do {
iteratorResult = iterator.next();
} while (!iteratorResult.done);
let result = this.result = iteratorResult.value;
// override .render function after initial render
this.render = () => result.rerender(options);
};
}
isFor(possibleRoot) {
return this.root === possibleRoot;
}
destroy() {
let { result, env } = this;
this.destroyed = true;
this.env = null;
this.root = null;
this.result = null;
this.render = null;
if (result) {
/*
Handles these scenarios:
* When roots are removed during standard rendering process, a transaction exists already
`.begin()` / `.commit()` are not needed.
* When roots are being destroyed manually (`component.append(); component.destroy() case), no
transaction exists already.
* When roots are being destroyed during `Renderer#destroy`, no transaction exists
*/
let needsTransaction = !env.inTransaction;
if (needsTransaction) {
env.begin();
}
result.destroy();
if (needsTransaction) {
env.commit();
}
}
}
}
const renderers = [];
export function _resetRenderers() {
renderers.length = 0;
}
setHasViews(() => renderers.length > 0);
function register(renderer) {
assert('Cannot register the same renderer twice', renderers.indexOf(renderer) === -1);
renderers.push(renderer);
}
function deregister(renderer) {
let index = renderers.indexOf(renderer);
assert('Cannot deregister unknown unregistered renderer', index !== -1);
renderers.splice(index, 1);
}
function loopBegin() {
for (let i = 0; i < renderers.length; i++) {
renderers[i]._scheduleRevalidate();
}
}
function K() {}
let loops = 0;
function loopEnd(current, next) {
for (let i = 0; i < renderers.length; i++) {
if (!renderers[i]._isValid()) {
if (loops > 10) {
loops = 0;
// TODO: do something better
renderers[i].destroy();
throw new Error('infinite rendering invalidation detected');
}
loops++;
return backburner.join(null, K);
}
}
loops = 0;
}
backburner.on('begin', loopBegin);
backburner.on('end', loopEnd);
class Renderer {
constructor(env, rootTemplate, _viewRegistry = fallbackViewRegistry, destinedForDOM = false) {
this._env = env;
this._rootTemplate = rootTemplate;
this._viewRegistry = _viewRegistry;
this._destinedForDOM = destinedForDOM;
this._destroyed = false;
this._roots = [];
this._lastRevision = null;
this._isRenderingRoots = false;
this._removedRoots = [];
}
// renderer HOOKS
appendOutletView(view, target) {
let definition = new TopLevelOutletComponentDefinition(view);
let outletStateReference = view.toReference();
let targetObject = view.outletState.render.controller;
this._appendDefinition(view, definition, target, outletStateReference, targetObject);
}
appendTo(view, target) {
let rootDef = new RootComponentDefinition(view);
this._appendDefinition(view, rootDef, target);
}
_appendDefinition(root, definition, target, outletStateReference = UNDEFINED_REFERENCE, targetObject = null) {
let self = new RootReference(definition);
let dynamicScope = new DynamicScope(null, outletStateReference, outletStateReference, true, targetObject);
let rootState = new RootState(root, this._env, this._rootTemplate, self, target, dynamicScope);
this._renderRoot(rootState);
}
rerender(view) {
this._scheduleRevalidate();
}
register(view) {
let id = getViewId(view);
assert('Attempted to register a view with an id already in use: ' + id, !this._viewRegistry[id]);
this._viewRegistry[id] = view;
}
unregister(view) {
delete this._viewRegistry[getViewId(view)];
}
remove(view) {
view._transitionTo('destroying');
this.cleanupRootFor(view);
setViewElement(view, null);
if (this._destinedForDOM) {
view.trigger('didDestroyElement');
}
if (!view.isDestroying) {
view.destroy();
}
}
cleanupRootFor(view) {
// no need to cleanup roots if we have already been destroyed
if (this._destroyed) { return; }
let roots = this._roots;
// traverse in reverse so we can remove items
// without mucking up the index
let i = this._roots.length;
while (i--) {
let root = roots[i];
if (root.isFor(view)) {
root.destroy();
}
}
}
destroy() {
if (this._destroyed) {
return;
}
this._destroyed = true;
this._clearAllRoots();
}
getElement(view) {
// overridden in the subclasses
}
getBounds(view) {
let bounds = view[BOUNDS];
let parentElement = bounds.parentElement();
let firstNode = bounds.firstNode();
let lastNode = bounds.lastNode();
return { parentElement, firstNode, lastNode };
}
createElement(tagName) {
return this._env.getAppendOperations().createElement(tagName);
}
_renderRoot(root) {
let { _roots: roots } = this;
roots.push(root);
if (roots.length === 1) {
register(this);
}
this._renderRootsTransaction();
}
_renderRoots() {
let { _roots: roots, _env: env, _removedRoots: removedRoots } = this;
let globalShouldReflush, initialRootsLength;
do {
env.begin();
// ensure that for the first iteration of the loop
// each root is processed
initialRootsLength = roots.length;
globalShouldReflush = false;
for (let i = 0; i < roots.length; i++) {
let root = roots[i];
if (root.destroyed) {
// add to the list of roots to be removed
// they will be removed from `this._roots` later
removedRoots.push(root);
// skip over roots that have been marked as destroyed
continue;
}
let { shouldReflush } = root;
// when processing non-initial reflush loops,
// do not process more roots than needed
if (i >= initialRootsLength && !shouldReflush) {
continue;
}
root.options.alwaysRevalidate = shouldReflush;
// track shouldReflush based on this roots render result
shouldReflush = root.shouldReflush = runInTransaction(root, 'render');
// globalShouldReflush should be `true` if *any* of
// the roots need to reflush
globalShouldReflush = globalShouldReflush || shouldReflush;
}
this._lastRevision = CURRENT_TAG.value();
env.commit();
} while (globalShouldReflush || roots.length > initialRootsLength);
// remove any roots that were destroyed during this transaction
while (removedRoots.length) {
let root = removedRoots.pop();
let rootIndex = roots.indexOf(root);
roots.splice(rootIndex, 1);
}
if (this._roots.length === 0) {
deregister(this);
}
}
_renderRootsTransaction() {
if (this._isRenderingRoots) {
// currently rendering roots, a new root was added and will
// be processed by the existing _renderRoots invocation
return;
}
// used to prevent calling _renderRoots again (see above)
// while we are actively rendering roots
this._isRenderingRoots = true;
let completedWithoutError = false;
try {
this._renderRoots();
completedWithoutError = true;
} finally {
if (!completedWithoutError) {
this._lastRevision = CURRENT_TAG.value();
}
this._isRenderingRoots = false;
}
}
_clearAllRoots() {
let roots = this._roots;
for (let i = 0; i < roots.length; i++) {
let root = roots[i];
root.destroy();
}
this._removedRoots.length = 0;
this._roots = null;
// if roots were present before destroying
// deregister this renderer instance
if (roots.length) {
deregister(this);
}
}
_scheduleRevalidate() {
backburner.scheduleOnce('render', this, this._revalidate);
}
_isValid() {
return this._destroyed || this._roots.length === 0 || CURRENT_TAG.validate(this._lastRevision);
}
_revalidate() {
if (this._isValid()) {
return;
}
this._renderRootsTransaction();
}
}
export class InertRenderer extends Renderer {
static create({ env, rootTemplate, _viewRegistry }) {
return new this(env, rootTemplate, _viewRegistry, false);
}
getElement(view) {
throw new Error('Accessing `this.element` is not allowed in non-interactive environments (such as FastBoot).');
}
}
export class InteractiveRenderer extends Renderer {
static create({ env, rootTemplate, _viewRegistry }) {
return new this(env, rootTemplate, _viewRegistry, true);
}
getElement(view) {
return getViewElement(view);
}
}