forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfn.dart
908 lines (725 loc) · 22.3 KB
/
fn.dart
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
library fn;
import 'dart:async';
import 'dart:collection';
import 'dart:sky' as sky;
import 'reflect.dart' as reflect;
final sky.Tracing _tracing = sky.window.tracing;
bool _initIsInCheckedMode() {
String testFn(i) { double d = i; return d.toString(); }
try {
testFn('not a double');
} catch (ex) {
return true;
}
return false;
}
final bool _isInCheckedMode = _initIsInCheckedMode();
final bool _shouldLogRenderDuration = false;
final bool _shouldTrace = false;
class Style {
final String _className;
static final Map<String, Style> _cache = new HashMap<String, Style>();
static int _nextStyleId = 1;
static String _getNextClassName() { return "style${_nextStyleId++}"; }
Style extend(Style other) {
var className = "$_className ${other._className}";
return _cache.putIfAbsent(className, () {
return new Style._internal(className);
});
}
factory Style(String styles) {
return _cache.putIfAbsent(styles, () {
var className = _getNextClassName();
sky.Element styleNode = sky.document.createElement('style');
styleNode.setChild(new sky.Text(".$className { $styles }"));
sky.document.appendChild(styleNode);
return new Style._internal(className);
});
}
Style._internal(this._className);
}
void _parentInsertBefore(sky.ParentNode parent,
sky.Node node,
sky.Node ref) {
if (ref != null) {
ref.insertBefore([node]);
} else {
parent.appendChild(node);
}
}
enum _SyncOperation { IDENTICAL, INSERTION, STATEFUL, STATELESS, REMOVAL }
/*
* All Effen nodes derive from Node. All nodes have a _parent, a _key and
* can be sync'd.
*/
abstract class Node {
String _key;
Node _parent;
sky.Node _root;
bool _defunct = false;
int _nodeDepth;
Node({ Object key }) {
_key = key == null ? "$runtimeType" : "$runtimeType-$key";
}
// Subclasses which implements Nodes that become stateful may return true
// if the |old| node has become stateful and should be retained.
bool _willSync(Node old) => false;
void _sync(Node old, sky.ParentNode host, sky.Node insertBefore);
void _remove() {
_defunct = true;
_root = null;
}
void _ensureDepth() {
if (_nodeDepth == null) {
_nodeDepth = 0;
Node parent = _parent;
while (parent != null) {
_nodeDepth++;
parent = parent._parent;
}
}
}
void _trace(String message) {
if (!_shouldTrace)
return;
_ensureDepth();
StringBuffer buffer = new StringBuffer();
int depth = _nodeDepth;
while (depth-- > 0) {
buffer.write(' ');
}
buffer.write(message);
print(buffer);
}
void _traceSync(_SyncOperation op, String key) {
if (!_shouldTrace)
return;
String opString = op.toString().toLowerCase();
String outString = opString.substring(opString.indexOf('.') + 1);
_trace('_sync($outString) $key');
}
void _removeChild(Node node) {
_traceSync(_SyncOperation.REMOVAL, node._key);
node._remove();
}
// Returns the child which should be retained as the child of this node.
Node _syncChild(Node node, Node oldNode, sky.ParentNode host,
sky.Node insertBefore) {
assert(oldNode == null || node._key == oldNode._key);
if (node == oldNode) {
_traceSync(_SyncOperation.IDENTICAL, node._key);
return node; // Nothing to do. Subtrees must be identical.
}
// TODO(rafaelw): This eagerly removes the old DOM. It may be that a
// new component was built that could re-use some of it. Consider
// syncing the new VDOM against the old one.
if (oldNode != null && node._key != oldNode._key) {
_removeChild(oldNode);
}
if (node._willSync(oldNode)) {
_traceSync(_SyncOperation.STATEFUL, node._key);
oldNode._sync(node, host, insertBefore);
node._defunct = true;
assert(oldNode._root is sky.Node);
return oldNode;
}
node._parent = this;
if (oldNode == null) {
_traceSync(_SyncOperation.INSERTION, node._key);
} else {
_traceSync(_SyncOperation.STATELESS, node._key);
}
node._sync(oldNode, host, insertBefore);
if (oldNode != null)
oldNode._defunct = true;
assert(node._root is sky.Node);
return node;
}
}
abstract class ContentNode extends Node {
Node content;
ContentNode(Node content) : this.content = content, super(key: content._key);
void _sync(Node old, sky.ParentNode host, sky.Node insertBefore) {
Node oldContent = old == null ? null : (old as ContentNode).content;
content = _syncChild(content, oldContent, host, insertBefore);
_root = content._root;
}
void _remove() {
_removeChild(content);
super._remove();
}
}
class StyleNode extends ContentNode {
final Style style;
StyleNode(Node content, this.style): super(content);
}
/*
* RenderNodes correspond to a desired state of a sky.Node. They are fully
* immutable, with one exception: A Node which is a Component which lives within
* an Element's children list, may be replaced with the "old" instance if it
* has become stateful.
*/
abstract class RenderNode extends Node {
static final Map<sky.Node, RenderNode> _nodeMap =
new HashMap<sky.Node, RenderNode>();
static RenderNode _getMounted(sky.Node node) => _nodeMap[node];
RenderNode({ Object key }) : super(key: key);
RenderNode get _emptyNode;
sky.Node _createNode();
void _sync(Node old, sky.ParentNode host, sky.Node insertBefore) {
if (old == null) {
_root = _createNode();
_parentInsertBefore(host, _root, insertBefore);
old = _emptyNode;
} else {
_root = old._root;
}
_nodeMap[_root] = this;
_syncNode(old);
}
void _syncNode(RenderNode old);
void _remove() {
assert(_root != null);
_root.remove();
_nodeMap.remove(_root);
super._remove();
}
}
typedef GestureEventListener(sky.GestureEvent e);
typedef PointerEventListener(sky.PointerEvent e);
typedef EventListener(sky.Event e);
class EventTarget extends ContentNode {
final Map<String, sky.EventListener> listeners;
static final Set<String> _registeredEvents = new HashSet<String>();
static Map<String, sky.EventListener> _createListeners({
EventListener onWheel,
GestureEventListener onGestureFlingCancel,
GestureEventListener onGestureFlingStart,
GestureEventListener onGestureScrollStart,
GestureEventListener onGestureScrollUpdate,
GestureEventListener onGestureTap,
GestureEventListener onGestureTapDown,
PointerEventListener onPointerCancel,
PointerEventListener onPointerDown,
PointerEventListener onPointerMove,
PointerEventListener onPointerUp,
Map<String, sky.EventListener> custom
}) {
var listeners = custom != null ?
new HashMap<String, sky.EventListener>.from(custom) :
new HashMap<String, sky.EventListener>();
if (onWheel != null)
listeners['wheel'] = onWheel;
if (onGestureFlingCancel != null)
listeners['gestureflingcancel'] = onGestureFlingCancel;
if (onGestureFlingStart != null)
listeners['gestureflingstart'] = onGestureFlingStart;
if (onGestureScrollStart != null)
listeners['gesturescrollstart'] = onGestureScrollStart;
if (onGestureScrollUpdate != null)
listeners['gesturescrollupdate'] = onGestureScrollUpdate;
if (onGestureTap != null)
listeners['gesturetap'] = onGestureTap;
if (onGestureTapDown != null)
listeners['gesturetapdown'] = onGestureTapDown;
if (onPointerCancel != null)
listeners['pointercancel'] = onPointerCancel;
if (onPointerDown != null)
listeners['pointerdown'] = onPointerDown;
if (onPointerMove != null)
listeners['pointermove'] = onPointerMove;
if (onPointerUp != null)
listeners['pointerup'] = onPointerUp;
return listeners;
}
EventTarget(Node content, {
EventListener onWheel,
GestureEventListener onGestureFlingCancel,
GestureEventListener onGestureFlingStart,
GestureEventListener onGestureScrollStart,
GestureEventListener onGestureScrollUpdate,
GestureEventListener onGestureTap,
GestureEventListener onGestureTapDown,
PointerEventListener onPointerCancel,
PointerEventListener onPointerDown,
PointerEventListener onPointerMove,
PointerEventListener onPointerUp,
Map<String, sky.EventListener> custom
}) : listeners = _createListeners(
onWheel: onWheel,
onGestureFlingCancel: onGestureFlingCancel,
onGestureFlingStart: onGestureFlingStart,
onGestureScrollUpdate: onGestureScrollUpdate,
onGestureScrollStart: onGestureScrollStart,
onGestureTap: onGestureTap,
onGestureTapDown: onGestureTapDown,
onPointerCancel: onPointerCancel,
onPointerDown: onPointerDown,
onPointerMove: onPointerMove,
onPointerUp: onPointerUp,
custom: custom
),
super(content);
void _handleEvent(sky.Event e) {
sky.EventListener listener = listeners[e.type];
if (listener != null) {
listener(e);
}
}
static void _dispatchEvent(sky.Event e) {
Node target = RenderNode._getMounted(e.target);
// TODO(rafaelw): StopPropagation?
while (target != null) {
if (target is EventTarget) {
(target as EventTarget)._handleEvent(e);
}
target = target._parent;
}
}
static void _ensureDocumentListener(String eventType) {
if (_registeredEvents.add(eventType)) {
sky.document.addEventListener(eventType, _dispatchEvent);
}
}
void _sync(Node old, sky.ParentNode host, sky.Node insertBefore) {
for (var type in listeners.keys) {
_ensureDocumentListener(type);
}
super._sync(old, host, insertBefore);
}
}
class Text extends RenderNode {
final String data;
// Text nodes are special cases of having non-unique keys (which don't need
// to be assigned as part of the API). Since they are unique in not having
// children, there's little point to reordering, so we always just re-assign
// the data.
Text(this.data) : super(key:'*text*');
static final Text _emptyText = new Text(null);
RenderNode get _emptyNode => _emptyText;
sky.Node _createNode() {
return new sky.Text(data);
}
void _syncNode(RenderNode old) {
if (old == _emptyText)
return; // we set inside _createNode();
(_root as sky.Text).data = data;
}
}
final List<Node> _emptyList = new List<Node>();
abstract class Element extends RenderNode {
String get _tagName;
sky.Node _createNode() => sky.document.createElement(_tagName);
final List<Node> children;
final Style style;
final String inlineStyle;
String _class;
Element({
Object key,
List<Node> children,
this.style,
this.inlineStyle
}) : this.children = children == null ? _emptyList : children,
super(key:key) {
if (_isInCheckedMode) {
_debugReportDuplicateIds();
}
}
void _remove() {
super._remove();
if (children != null) {
for (var child in children) {
_removeChild(child);
}
}
}
void _debugReportDuplicateIds() {
var idSet = new HashSet<String>();
for (var child in children) {
if (child is Text) {
continue; // Text nodes all have the same key and are never reordered.
}
if (!idSet.add(child._key)) {
throw '''If multiple (non-Text) nodes of the same type exist as children
of another node, they must have unique keys.''';
}
}
}
void _ensureClass() {
if (_class == null) {
List<Style> styles = new List<Style>();
if (style != null) {
styles.add(style);
}
Node parent = _parent;
while (parent != null && parent is! RenderNode) {
if (parent is StyleNode)
styles.add((parent as StyleNode).style);
parent = parent._parent;
}
_class = styles.map((s) => s._className).join(' ');
}
}
void _syncNode(RenderNode old) {
Element oldElement = old as Element;
sky.Element root = _root as sky.Element;
_ensureClass();
if (_class != oldElement._class && _class != '')
root.setAttribute('class', _class);
if (inlineStyle != oldElement.inlineStyle)
root.setAttribute('style', inlineStyle);
_syncChildren(oldElement);
}
void _syncChildren(Element oldElement) {
sky.Element root = _root as sky.Element;
assert(root != null);
var startIndex = 0;
var endIndex = children.length;
var oldChildren = oldElement.children;
var oldStartIndex = 0;
var oldEndIndex = oldChildren.length;
sky.Node nextSibling = null;
Node currentNode = null;
Node oldNode = null;
void sync(int atIndex) {
children[atIndex] = _syncChild(currentNode, oldNode, _root, nextSibling);
}
// Scan backwards from end of list while nodes can be directly synced
// without reordering.
while (endIndex > startIndex && oldEndIndex > oldStartIndex) {
currentNode = children[endIndex - 1];
oldNode = oldChildren[oldEndIndex - 1];
if (currentNode._key != oldNode._key) {
break;
}
endIndex--;
oldEndIndex--;
sync(endIndex);
nextSibling = currentNode._root;
}
HashMap<String, Node> oldNodeIdMap = null;
bool oldNodeReordered(String key) {
return oldNodeIdMap != null &&
oldNodeIdMap.containsKey(key) &&
oldNodeIdMap[key] == null;
}
void advanceOldStartIndex() {
oldStartIndex++;
while (oldStartIndex < oldEndIndex &&
oldNodeReordered(oldChildren[oldStartIndex]._key)) {
oldStartIndex++;
}
}
void ensureOldIdMap() {
if (oldNodeIdMap != null)
return;
oldNodeIdMap = new HashMap<String, Node>();
for (int i = oldStartIndex; i < oldEndIndex; i++) {
var node = oldChildren[i];
if (node is! Text) {
oldNodeIdMap.putIfAbsent(node._key, () => node);
}
}
}
bool searchForOldNode() {
if (currentNode is Text)
return false; // Never re-order Text nodes.
ensureOldIdMap();
oldNode = oldNodeIdMap[currentNode._key];
if (oldNode == null)
return false;
oldNodeIdMap[currentNode._key] = null; // mark it reordered.
_parentInsertBefore(root, oldNode._root, nextSibling);
return true;
}
// Scan forwards, this time we may re-order;
nextSibling = root.firstChild;
while (startIndex < endIndex && oldStartIndex < oldEndIndex) {
currentNode = children[startIndex];
oldNode = oldChildren[oldStartIndex];
if (currentNode._key == oldNode._key) {
assert(currentNode.runtimeType == oldNode.runtimeType);
nextSibling = nextSibling.nextSibling;
sync(startIndex);
startIndex++;
advanceOldStartIndex();
continue;
}
oldNode = null;
searchForOldNode();
sync(startIndex);
startIndex++;
}
// New insertions
oldNode = null;
while (startIndex < endIndex) {
currentNode = children[startIndex];
sync(startIndex);
startIndex++;
}
// Removals
currentNode = null;
while (oldStartIndex < oldEndIndex) {
oldNode = oldChildren[oldStartIndex];
_removeChild(oldNode);
advanceOldStartIndex();
}
}
}
class Container extends Element {
String get _tagName => 'div';
static final Container _emptyContainer = new Container();
RenderNode get _emptyNode => _emptyContainer;
Container({
Object key,
List<Node> children,
Style style,
String inlineStyle
}) : super(
key: key,
children: children,
style: style,
inlineStyle: inlineStyle
);
}
class Image extends Element {
String get _tagName => 'img';
static final Image _emptyImage = new Image();
RenderNode get _emptyNode => _emptyImage;
final String src;
final int width;
final int height;
Image({
Object key,
List<Node> children,
Style style,
String inlineStyle,
this.width,
this.height,
this.src
}) : super(
key: key,
children: children,
style: style,
inlineStyle: inlineStyle
);
void _syncNode(Node old) {
super._syncNode(old);
Image oldImage = old as Image;
sky.HTMLImageElement skyImage = _root as sky.HTMLImageElement;
if (src != oldImage.src)
skyImage.src = src;
if (width != oldImage.width)
skyImage.style['width'] = '${width}px';
if (height != oldImage.height)
skyImage.style['height'] = '${height}px';
}
}
class Anchor extends Element {
String get _tagName => 'a';
static final Anchor _emptyAnchor = new Anchor();
Node get _emptyNode => _emptyAnchor;
final String href;
final int width;
final int height;
Anchor({
Object key,
List<Node> children,
Style style,
String inlineStyle,
this.width,
this.height,
this.href
}) : super(
key: key,
children: children,
style: style,
inlineStyle: inlineStyle
);
void _syncNode(Node old) {
super._syncNode(old);
Anchor oldAnchor = old as Anchor;
sky.HTMLAnchorElement skyAnchor = _root as sky.HTMLAnchorElement;
if (href != oldAnchor.href)
skyAnchor.href = href;
}
}
Set<Component> _mountedComponents = new HashSet<Component>();
Set<Component> _unmountedComponents = new HashSet<Component>();
void _enqueueDidMount(Component c) {
assert(!_notifingMountStatus);
_mountedComponents.add(c);
}
void _enqueueDidUnmount(Component c) {
assert(!_notifingMountStatus);
_unmountedComponents.add(c);
}
bool _notifingMountStatus = false;
void _notifyMountStatusChanged() {
try {
_notifingMountStatus = true;
_unmountedComponents.forEach((c) => c._didUnmount());
_mountedComponents.forEach((c) => c._didMount());
_mountedComponents.clear();
_unmountedComponents.clear();
} finally {
_notifingMountStatus = false;
}
}
List<Component> _dirtyComponents = new List<Component>();
bool _buildScheduled = false;
bool _inRenderDirtyComponents = false;
void _buildDirtyComponents() {
_tracing.begin('fn::_buildDirtyComponents');
Stopwatch sw;
if (_shouldLogRenderDuration)
sw = new Stopwatch()..start();
try {
_inRenderDirtyComponents = true;
_dirtyComponents.sort((a, b) => a._order - b._order);
for (var comp in _dirtyComponents) {
comp._buildIfDirty();
}
_dirtyComponents.clear();
_buildScheduled = false;
} finally {
_inRenderDirtyComponents = false;
}
_notifyMountStatusChanged();
if (_shouldLogRenderDuration) {
sw.stop();
print('Render took ${sw.elapsedMicroseconds} microseconds');
}
_tracing.end('fn::_buildDirtyComponents');
}
void _scheduleComponentForRender(Component c) {
assert(!_inRenderDirtyComponents);
_dirtyComponents.add(c);
if (!_buildScheduled) {
_buildScheduled = true;
new Future.microtask(_buildDirtyComponents);
}
}
abstract class Component extends Node {
bool get _isBuilding => _currentlyBuilding == this;
bool _dirty = true;
sky.Node get _host => _root.parentNode;
sky.Node get _insertionPoint => _root == null ? _root : _root.nextSibling;
Node _built;
final int _order;
static int _currentOrder = 0;
bool _stateful;
static Component _currentlyBuilding;
List<Function> _mountCallbacks;
List<Function> _unmountCallbacks;
void onDidMount(Function fn) {
if (_mountCallbacks == null)
_mountCallbacks = new List<Function>();
_mountCallbacks.add(fn);
}
void onDidUnmount(Function fn) {
if (_unmountCallbacks == null)
_unmountCallbacks = new List<Function>();
_unmountCallbacks.add(fn);
}
Component({ Object key, bool stateful })
: _stateful = stateful != null ? stateful : false,
_order = _currentOrder + 1,
super(key:key);
Component.fromArgs(Object key, bool stateful)
: this(key: key, stateful: stateful);
void _didMount() {
if (_mountCallbacks != null)
_mountCallbacks.forEach((fn) => fn());
}
void _didUnmount() {
if (_unmountCallbacks != null)
_unmountCallbacks.forEach((fn) => fn());
}
// TODO(rafaelw): It seems wrong to expose DOM at all. This is presently
// needed to get sizing info.
sky.Node getRoot() => _root;
void _remove() {
assert(_built != null);
assert(_root != null);
_removeChild(_built);
_built = null;
_enqueueDidUnmount(this);
super._remove();
}
bool _willSync(Node old) {
Component oldComponent = old as Component;
if (oldComponent == null || !oldComponent._stateful)
return false;
// Make |this| the "old" Component
_stateful = false;
_built = oldComponent._built;
assert(_built != null);
// Make |oldComponent| the "new" component
reflect.copyPublicFields(this, oldComponent);
oldComponent._built = null;
oldComponent._dirty = true;
return true;
}
/* There are three cases here:
* 1) Building for the first time:
* assert(_built == null && old == null)
* 2) Re-building (because a dirty flag got set):
* assert(_built != null && old == null)
* 3) Syncing against an old version
* assert(_built == null && old != null)
*/
void _sync(Node old, sky.ParentNode host, sky.Node insertBefore) {
assert(!_defunct);
assert(_built == null || old == null);
Component oldComponent = old as Component;
var oldBuilt;
if (oldComponent == null) {
oldBuilt = _built;
} else {
assert(_built == null);
oldBuilt = oldComponent._built;
}
if (oldBuilt == null)
_enqueueDidMount(this);
int lastOrder = _currentOrder;
_currentOrder = _order;
_currentlyBuilding = this;
_built = build();
_currentlyBuilding = null;
_currentOrder = lastOrder;
_built = _syncChild(_built, oldBuilt, host, insertBefore);
_dirty = false;
_root = _built._root;
}
void _buildIfDirty() {
if (!_dirty || _defunct)
return;
assert(_host != null);
_trace('$_key rebuilding...');
_sync(null, _host, _insertionPoint);
}
void scheduleBuild() {
setState(() {});
}
void setState(Function fn()) {
_stateful = true;
fn();
if (_isBuilding || _dirty || _defunct)
return;
_dirty = true;
_scheduleComponentForRender(this);
}
Node build();
}
abstract class App extends Component {
sky.Node _host;
App() : super(stateful: true) {
_host = sky.document.createElement('div');
sky.document.appendChild(_host);
_scheduleComponentForRender(this);
}
}