Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 library fn; | 5 library fn; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:sky' as sky; | 9 import 'dart:sky' as sky; |
| 10 import 'reflect.dart' as reflect; | 10 import 'reflect.dart' as reflect; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 sky.Node node, | 75 sky.Node node, |
| 76 sky.Node ref) { | 76 sky.Node ref) { |
| 77 if (ref != null) { | 77 if (ref != null) { |
| 78 ref.insertBefore([node]); | 78 ref.insertBefore([node]); |
| 79 } else { | 79 } else { |
| 80 parent.appendChild(node); | 80 parent.appendChild(node); |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 | 83 |
| 84 abstract class Node { | 84 abstract class Node { |
| 85 String _key = null; | 85 String _key; |
| 86 sky.Node _root = null; | 86 Node _parent; |
| 87 sky.Node _root; | |
| 88 bool _defunct = false; | |
| 87 | 89 |
| 88 // TODO(abarth): Both Elements and Components have |events| but |Text| | 90 // TODO(abarth): Both Elements and Components have |events| but |Text| |
| 89 // doesn't. Should we add a common base class to contain |events|? | 91 // doesn't. Should we add a common base class to contain |events|? |
| 90 final EventMap events = new EventMap(); | 92 final EventMap events = new EventMap(); |
| 91 | 93 |
| 92 Node({ Object key }) { | 94 Node({ Object key }) { |
| 93 _key = key == null ? "$runtimeType" : "$runtimeType-$key"; | 95 _key = key == null ? "$runtimeType" : "$runtimeType-$key"; |
| 94 } | 96 } |
| 95 | 97 |
| 98 Node get _emptyNode; | |
| 99 | |
| 100 sky.Node _createNode(); | |
| 101 | |
| 102 void _mount(Node parent, sky.ParentNode host, sky.Node insertBefore) { | |
| 103 var node = _emptyNode; | |
| 104 node._parent = parent; | |
| 105 | |
| 106 node._root = _createNode(); | |
| 107 assert(node._root != null); | |
| 108 | |
| 109 _parentInsertBefore(host, node._root, insertBefore); | |
| 110 | |
| 111 _syncNode(node); | |
| 112 } | |
| 113 | |
| 114 bool _sync(Node old, Node parent, sky.ParentNode host, | |
| 115 sky.Node insertBefore) { | |
| 116 | |
| 117 if (old == null) { | |
| 118 _mount(parent, host, insertBefore); | |
| 119 return false; | |
| 120 } | |
| 121 | |
| 122 return _syncNode(old); | |
| 123 } | |
| 124 | |
| 96 // Return true IFF the old node has *become* the new node (should be | 125 // Return true IFF the old node has *become* the new node (should be |
| 97 // retained because it is stateful) | 126 // retained because it is stateful) |
| 98 bool _sync(Node old, sky.ParentNode host, sky.Node insertBefore); | 127 bool _syncNode(Node old) { |
| 128 assert(!old._defunct); | |
| 129 _root = old._root; | |
| 130 _parent = old._parent; | |
| 131 return false; | |
| 132 } | |
| 99 | 133 |
| 100 void _remove() { | 134 void _unmount() { |
| 101 assert(_root != null); | 135 assert(_root != null); |
| 136 _parent = null; | |
| 102 _root.remove(); | 137 _root.remove(); |
| 138 _defunct = true; | |
| 103 _root = null; | 139 _root = null; |
| 104 } | 140 } |
| 105 } | 141 } |
| 106 | 142 |
| 107 class Text extends Node { | 143 class Text extends Node { |
| 108 String data; | 144 final String data; |
| 109 | 145 |
| 110 // Text nodes are special cases of having non-unique keys (which don't need | 146 // Text nodes are special cases of having non-unique keys (which don't need |
| 111 // to be assigned as part of the API). Since they are unique in not having | 147 // to be assigned as part of the API). Since they are unique in not having |
| 112 // children, there's little point to reordering, so we always just re-assign | 148 // children, there's little point to reordering, so we always just re-assign |
| 113 // the data. | 149 // the data. |
| 114 Text(this.data) : super(key:'*text*'); | 150 Text(this.data) : super(key:'*text*'); |
| 115 | 151 |
| 116 bool _sync(Node old, sky.ParentNode host, sky.Node insertBefore) { | 152 static Text _emptyText = new Text(null); |
|
abarth-chromium
2015/03/17 03:23:40
final
rafaelw
2015/03/18 19:08:32
fixed in https://codereview.chromium.org/100954300
| |
| 117 if (old == null) { | |
| 118 _root = new sky.Text(data); | |
| 119 _parentInsertBefore(host, _root, insertBefore); | |
| 120 return false; | |
| 121 } | |
| 122 | 153 |
| 123 _root = old._root; | 154 Node get _emptyNode => _emptyText; |
| 155 | |
| 156 sky.Node _createNode() { | |
| 157 return new sky.Text(data); | |
| 158 } | |
| 159 | |
| 160 bool _syncNode(Node old) { | |
| 161 super._syncNode(old); | |
| 162 if (old == _emptyText) | |
| 163 return false; // we set inside _createNode(); | |
| 164 | |
| 124 (_root as sky.Text).data = data; | 165 (_root as sky.Text).data = data; |
| 125 return false; | 166 return false; |
| 126 } | 167 } |
| 127 } | 168 } |
| 128 | 169 |
| 129 final List<Node> _emptyList = new List<Node>(); | 170 final List<Node> _emptyList = new List<Node>(); |
| 130 | 171 |
| 131 abstract class Element extends Node { | 172 abstract class Element extends Node { |
| 132 | 173 |
| 133 String get _tagName; | 174 String get _tagName; |
| 134 | 175 |
| 135 Element get _emptyElement; | 176 sky.Node _createNode() => sky.document.createElement(_tagName); |
| 136 | 177 |
| 137 String inlineStyle; | 178 final String inlineStyle; |
| 138 | 179 |
| 139 List<Node> _children = null; | 180 final List<Node> _children; |
| 140 String _class = ''; | 181 final String _class; |
| 141 | 182 |
| 142 Element({ | 183 Element({ |
| 143 Object key, | 184 Object key, |
| 144 List<Node> children, | 185 List<Node> children, |
| 145 Style style, | 186 Style style, |
| 146 | |
| 147 this.inlineStyle | 187 this.inlineStyle |
| 148 }) : super(key:key) { | 188 }) : _class = style == null ? '' : style._className, |
| 149 _class = style == null ? '' : style._className; | 189 _children = children == null ? _emptyList : children, |
| 150 _children = children == null ? _emptyList : children; | 190 super(key:key) { |
| 151 | 191 |
| 152 if (_isInCheckedMode) { | 192 if (_isInCheckedMode) { |
| 153 _debugReportDuplicateIds(); | 193 _debugReportDuplicateIds(); |
| 154 } | 194 } |
| 155 } | 195 } |
| 156 | 196 |
| 157 void _remove() { | 197 void _unmount() { |
| 158 super._remove(); | 198 super._unmount(); |
| 159 if (_children != null) { | 199 if (_children != null) { |
| 160 for (var child in _children) { | 200 for (var child in _children) { |
| 161 child._remove(); | 201 child._unmount(); |
| 162 } | 202 } |
| 163 } | 203 } |
| 164 _children = null; | |
| 165 } | 204 } |
| 166 | 205 |
| 167 void _debugReportDuplicateIds() { | 206 void _debugReportDuplicateIds() { |
| 168 var idSet = new HashSet<String>(); | 207 var idSet = new HashSet<String>(); |
| 169 for (var child in _children) { | 208 for (var child in _children) { |
| 170 if (child is Text) { | 209 if (child is Text) { |
| 171 continue; // Text nodes all have the same key and are never reordered. | 210 continue; // Text nodes all have the same key and are never reordered. |
| 172 } | 211 } |
| 173 | 212 |
| 174 if (!idSet.add(child._key)) { | 213 if (!idSet.add(child._key)) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 EventHandler oldHandler = oldHandlers[i]; | 254 EventHandler oldHandler = oldHandlers[i]; |
| 216 root.removeEventListener(oldHandler.type, oldHandler.listener); | 255 root.removeEventListener(oldHandler.type, oldHandler.listener); |
| 217 } | 256 } |
| 218 | 257 |
| 219 for (int i = newStartIndex; i < newEndIndex; ++i) { | 258 for (int i = newStartIndex; i < newEndIndex; ++i) { |
| 220 EventHandler newHandler = newHandlers[i]; | 259 EventHandler newHandler = newHandlers[i]; |
| 221 root.addEventListener(newHandler.type, newHandler.listener); | 260 root.addEventListener(newHandler.type, newHandler.listener); |
| 222 } | 261 } |
| 223 } | 262 } |
| 224 | 263 |
| 225 void _syncNode([Element old]) { | 264 bool _syncNode(Node old) { |
| 226 if (old == null) { | 265 super._syncNode(old); |
| 227 old = _emptyElement; | |
| 228 } | |
| 229 | 266 |
| 230 _syncEvents(old); | 267 Element oldElement = old as Element; |
| 268 sky.Element root = _root as sky.Element; | |
| 231 | 269 |
| 232 sky.Element root = _root as sky.Element; | 270 _syncEvents(oldElement); |
| 233 if (_class != old._class) { | 271 |
| 272 if (_class != oldElement._class) | |
| 234 root.setAttribute('class', _class); | 273 root.setAttribute('class', _class); |
| 235 } | |
| 236 | 274 |
| 237 if (inlineStyle != old.inlineStyle) { | 275 if (inlineStyle != oldElement.inlineStyle) |
| 238 root.setAttribute('style', inlineStyle); | 276 root.setAttribute('style', inlineStyle); |
| 239 } | 277 |
| 278 _syncChildren(oldElement); | |
| 279 | |
| 280 return false; | |
| 240 } | 281 } |
| 241 | 282 |
| 242 bool _sync(Node old, sky.ParentNode host, sky.Node insertBefore) { | 283 void _syncChildren(Element oldElement) { |
| 243 // print("---Syncing children of $_key"); | 284 // print("---Syncing children of $_key"); |
|
abarth-chromium
2015/03/17 03:23:40
Do we want to leave these prints in?
rafaelw
2015/03/18 19:08:32
fixed in https://codereview.chromium.org/100954300
| |
| 244 | 285 sky.Element root = _root as sky.Element; |
| 245 Element oldElement = old as Element; | 286 assert(root != null); |
| 246 | |
| 247 if (oldElement == null) { | |
| 248 // print("...no oldElement, initial build"); | |
| 249 | |
| 250 _root = sky.document.createElement(_tagName); | |
| 251 _parentInsertBefore(host, _root, insertBefore); | |
| 252 _syncNode(); | |
| 253 | |
| 254 for (var child in _children) { | |
| 255 child._sync(null, _root, null); | |
| 256 assert(child._root is sky.Node); | |
| 257 } | |
| 258 | |
| 259 return false; | |
| 260 } | |
| 261 | |
| 262 if (this == oldElement) | |
| 263 return false; | |
| 264 | |
| 265 _root = oldElement._root; | |
| 266 oldElement._root = null; | |
| 267 sky.Element root = (_root as sky.Element); | |
| 268 | |
| 269 _syncNode(oldElement); | |
| 270 | 287 |
| 271 var startIndex = 0; | 288 var startIndex = 0; |
| 272 var endIndex = _children.length; | 289 var endIndex = _children.length; |
| 273 | 290 |
| 274 var oldChildren = oldElement._children; | 291 var oldChildren = oldElement._children; |
| 275 var oldStartIndex = 0; | 292 var oldStartIndex = 0; |
| 276 var oldEndIndex = oldChildren.length; | 293 var oldEndIndex = oldChildren.length; |
| 277 | 294 |
| 278 sky.Node nextSibling = null; | 295 sky.Node nextSibling = null; |
| 279 Node currentNode = null; | 296 Node currentNode = null; |
| 280 Node oldNode = null; | 297 Node oldNode = null; |
| 281 | 298 |
| 282 void sync(int atIndex) { | 299 void sync(int atIndex) { |
| 283 if (currentNode._sync(oldNode, root, nextSibling)) { | 300 if (currentNode._sync(oldNode, this, _root, nextSibling)) { |
| 284 // oldNode was stateful and must be retained. | 301 // oldNode was stateful and must be retained. |
| 285 assert(oldNode != null); | |
| 286 currentNode = oldNode; | 302 currentNode = oldNode; |
| 287 _children[atIndex] = currentNode; | 303 _children[atIndex] = currentNode; |
| 288 } | 304 } |
| 305 | |
| 289 assert(currentNode._root is sky.Node); | 306 assert(currentNode._root is sky.Node); |
| 290 } | 307 } |
| 291 | 308 |
| 292 // Scan backwards from end of list while nodes can be directly synced | 309 // Scan backwards from end of list while nodes can be directly synced |
| 293 // without reordering. | 310 // without reordering. |
| 294 // print("...scanning backwards"); | 311 // print("...scanning backwards"); |
| 295 while (endIndex > startIndex && oldEndIndex > oldStartIndex) { | 312 while (endIndex > startIndex && oldEndIndex > oldStartIndex) { |
| 296 currentNode = _children[endIndex - 1]; | 313 currentNode = _children[endIndex - 1]; |
| 297 oldNode = oldChildren[oldEndIndex - 1]; | 314 oldNode = oldChildren[oldEndIndex - 1]; |
| 298 | 315 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 388 sync(startIndex); | 405 sync(startIndex); |
| 389 startIndex++; | 406 startIndex++; |
| 390 } | 407 } |
| 391 | 408 |
| 392 // Removals | 409 // Removals |
| 393 // print('...processing remaining removals'); | 410 // print('...processing remaining removals'); |
| 394 currentNode = null; | 411 currentNode = null; |
| 395 while (oldStartIndex < oldEndIndex) { | 412 while (oldStartIndex < oldEndIndex) { |
| 396 oldNode = oldChildren[oldStartIndex]; | 413 oldNode = oldChildren[oldStartIndex]; |
| 397 // print('> ${oldNode._key} removing from $oldEndIndex'); | 414 // print('> ${oldNode._key} removing from $oldEndIndex'); |
| 398 oldNode._remove(); | 415 oldNode._unmount(); |
| 399 advanceOldStartIndex(); | 416 advanceOldStartIndex(); |
| 400 } | 417 } |
| 401 | |
| 402 oldElement._children = null; | |
| 403 return false; | |
| 404 } | 418 } |
| 405 } | 419 } |
| 406 | 420 |
| 407 class Container extends Element { | 421 class Container extends Element { |
| 408 | 422 |
| 409 String get _tagName => 'div'; | 423 String get _tagName => 'div'; |
| 410 | 424 |
| 411 static final Container _emptyContainer = new Container(); | 425 static final Container _emptyContainer = new Container(); |
| 412 Element get _emptyElement => _emptyContainer; | 426 |
| 427 Node get _emptyNode => _emptyContainer; | |
| 413 | 428 |
| 414 Container({ | 429 Container({ |
| 415 Object key, | 430 Object key, |
| 416 List<Node> children, | 431 List<Node> children, |
| 417 Style style, | 432 Style style, |
| 418 String inlineStyle | 433 String inlineStyle |
| 419 }) : super( | 434 }) : super( |
| 420 key: key, | 435 key: key, |
| 421 children: children, | 436 children: children, |
| 422 style: style, | 437 style: style, |
| 423 inlineStyle: inlineStyle | 438 inlineStyle: inlineStyle |
| 424 ); | 439 ); |
| 425 } | 440 } |
| 426 | 441 |
| 427 class Image extends Element { | 442 class Image extends Element { |
| 428 | 443 |
| 429 String get _tagName => 'img'; | 444 String get _tagName => 'img'; |
| 430 | 445 |
| 431 static final Image _emptyImage = new Image(); | 446 static final Image _emptyImage = new Image(); |
| 432 Element get _emptyElement => _emptyImage; | 447 |
| 448 Node get _emptyNode => _emptyImage; | |
| 433 | 449 |
| 434 String src; | 450 String src; |
| 435 int width; | 451 int width; |
| 436 int height; | 452 int height; |
| 437 | 453 |
| 438 Image({ | 454 Image({ |
| 439 Object key, | 455 Object key, |
| 440 List<Node> children, | 456 List<Node> children, |
| 441 Style style, | 457 Style style, |
| 442 String inlineStyle, | 458 String inlineStyle, |
| 443 this.width, | 459 this.width, |
| 444 this.height, | 460 this.height, |
| 445 this.src | 461 this.src |
| 446 }) : super( | 462 }) : super( |
| 447 key: key, | 463 key: key, |
| 448 children: children, | 464 children: children, |
| 449 style: style, | 465 style: style, |
| 450 inlineStyle: inlineStyle | 466 inlineStyle: inlineStyle |
| 451 ); | 467 ); |
| 452 | 468 |
| 453 void _syncNode([Element old]) { | 469 bool _syncNode(Node old) { |
| 454 super._syncNode(old); | 470 super._syncNode(old); |
| 455 | 471 |
| 456 Image oldImage = old != null ? old : _emptyImage; | 472 Image oldImage = old as Image; |
| 457 sky.HTMLImageElement skyImage = _root as sky.HTMLImageElement; | 473 sky.HTMLImageElement skyImage = _root as sky.HTMLImageElement; |
| 458 if (src != oldImage.src) { | 474 |
| 475 if (src != oldImage.src) | |
| 459 skyImage.src = src; | 476 skyImage.src = src; |
| 460 } | |
| 461 | 477 |
| 462 if (width != oldImage.width) { | 478 if (width != oldImage.width) |
| 463 skyImage.style['width'] = '${width}px'; | 479 skyImage.style['width'] = '${width}px'; |
| 464 } | 480 |
| 465 if (height != oldImage.height) { | 481 if (height != oldImage.height) |
| 466 skyImage.style['height'] = '${height}px'; | 482 skyImage.style['height'] = '${height}px'; |
| 467 } | 483 |
| 484 return false; | |
| 468 } | 485 } |
| 469 } | 486 } |
| 470 | 487 |
| 471 class Anchor extends Element { | 488 class Anchor extends Element { |
| 472 | 489 |
| 473 String get _tagName => 'a'; | 490 String get _tagName => 'a'; |
| 474 | 491 |
| 475 static final Anchor _emptyAnchor = new Anchor(); | 492 static final Anchor _emptyAnchor = new Anchor(); |
| 476 Element get _emptyElement => _emptyAnchor; | 493 |
| 494 Node get _emptyNode => _emptyAnchor; | |
| 477 | 495 |
| 478 String href; | 496 String href; |
| 479 int width; | 497 int width; |
| 480 int height; | 498 int height; |
| 481 | 499 |
| 482 Anchor({ | 500 Anchor({ |
| 483 Object key, | 501 Object key, |
| 484 List<Node> children, | 502 List<Node> children, |
| 485 Style style, | 503 Style style, |
| 486 String inlineStyle, | 504 String inlineStyle, |
| 487 this.width, | 505 this.width, |
| 488 this.height, | 506 this.height, |
| 489 this.href | 507 this.href |
| 490 }) : super( | 508 }) : super( |
| 491 key: key, | 509 key: key, |
| 492 children: children, | 510 children: children, |
| 493 style: style, | 511 style: style, |
| 494 inlineStyle: inlineStyle | 512 inlineStyle: inlineStyle |
| 495 ); | 513 ); |
| 496 | 514 |
| 497 void _syncNode([Element old]) { | 515 bool _syncNode(Node old) { |
| 498 Anchor oldAnchor = old != null ? old as Anchor : _emptyAnchor; | 516 super._syncNode(old); |
| 499 super._syncNode(oldAnchor); | |
| 500 | 517 |
| 518 Anchor oldAnchor = old as Anchor; | |
| 501 sky.HTMLAnchorElement skyAnchor = _root as sky.HTMLAnchorElement; | 519 sky.HTMLAnchorElement skyAnchor = _root as sky.HTMLAnchorElement; |
| 502 if (href != oldAnchor.href) { | 520 |
| 521 if (href != oldAnchor.href) | |
| 503 skyAnchor.href = href; | 522 skyAnchor.href = href; |
| 504 } | 523 |
| 524 return false; | |
| 505 } | 525 } |
| 506 } | 526 } |
| 507 | 527 |
| 508 List<Component> _dirtyComponents = new List<Component>(); | 528 List<Component> _dirtyComponents = new List<Component>(); |
| 509 bool _buildScheduled = false; | 529 bool _buildScheduled = false; |
| 510 bool _inRenderDirtyComponents = false; | 530 bool _inRenderDirtyComponents = false; |
| 511 | 531 |
| 512 void _buildDirtyComponents() { | 532 void _buildDirtyComponents() { |
| 513 try { | 533 try { |
| 514 _inRenderDirtyComponents = true; | 534 _inRenderDirtyComponents = true; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 536 | 556 |
| 537 if (!_buildScheduled) { | 557 if (!_buildScheduled) { |
| 538 _buildScheduled = true; | 558 _buildScheduled = true; |
| 539 new Future.microtask(_buildDirtyComponents); | 559 new Future.microtask(_buildDirtyComponents); |
| 540 } | 560 } |
| 541 } | 561 } |
| 542 | 562 |
| 543 abstract class Component extends Node { | 563 abstract class Component extends Node { |
| 544 bool _dirty = true; // components begin dirty because they haven't built. | 564 bool _dirty = true; // components begin dirty because they haven't built. |
| 545 Node _vdom = null; | 565 Node _vdom = null; |
| 546 bool _removed = false; | |
| 547 final int _order; | 566 final int _order; |
| 548 static int _currentOrder = 0; | 567 static int _currentOrder = 0; |
| 549 bool _stateful; | 568 bool _stateful; |
| 550 static Component _currentlyRendering; | 569 static Component _currentlyRendering; |
| 551 | 570 |
| 552 Component({ Object key, bool stateful }) | 571 Component({ Object key, bool stateful }) |
| 553 : _stateful = stateful != null ? stateful : false, | 572 : _stateful = stateful != null ? stateful : false, |
| 554 _order = _currentOrder + 1, | 573 _order = _currentOrder + 1, |
| 555 super(key:key); | 574 super(key:key); |
| 556 | 575 |
| 557 void didMount() {} | 576 void didMount() {} |
| 558 void didUnmount() {} | 577 void didUnmount() {} |
| 559 | 578 |
| 560 void _remove() { | |
| 561 assert(_vdom != null); | |
| 562 assert(_root != null); | |
| 563 _vdom._remove(); | |
| 564 _vdom = null; | |
| 565 _root = null; | |
| 566 _removed = true; | |
| 567 didUnmount(); | |
| 568 } | |
| 569 | |
| 570 // TODO(rafaelw): It seems wrong to expose DOM at all. This is presently | 579 // TODO(rafaelw): It seems wrong to expose DOM at all. This is presently |
| 571 // needed to get sizing info. | 580 // needed to get sizing info. |
| 572 sky.Node getRoot() => _root; | 581 sky.Node getRoot() => _root; |
| 573 | 582 |
| 574 bool _sync(Node old, sky.Node host, sky.Node insertBefore) { | 583 void _mount(Node parent, sky.ParentNode host, sky.Node insertBefore) { |
| 584 _parent = parent; | |
| 585 | |
| 586 _syncInternal(host, insertBefore); | |
| 587 | |
| 588 didMount(); | |
| 589 } | |
| 590 | |
| 591 void _unmount() { | |
| 592 assert(_vdom != null); | |
| 593 assert(_root != null); | |
| 594 _vdom._unmount(); | |
| 595 _vdom = null; | |
| 596 _root = null; | |
| 597 _parent = null; | |
| 598 _defunct = true; | |
| 599 didUnmount(); | |
| 600 } | |
| 601 | |
| 602 bool _syncNode(Node old) { | |
| 575 Component oldComponent = old as Component; | 603 Component oldComponent = old as Component; |
| 576 | 604 |
| 577 if (oldComponent == null || oldComponent == this) { | 605 if (!oldComponent._stateful) { |
| 578 _buildInternal(host, insertBefore); | 606 _vdom = oldComponent._vdom; |
| 607 _syncInternal(); | |
| 608 | |
| 579 return false; | 609 return false; |
| 580 } | 610 } |
| 581 | 611 |
| 582 assert(oldComponent != null); | 612 _stateful = false; // prevent iloop from _syncInternal below. |
| 583 assert(_dirty); | |
| 584 assert(_vdom == null); | |
| 585 | 613 |
| 586 if (oldComponent._stateful) { | 614 reflect.copyPublicFields(this, oldComponent); |
| 587 _stateful = false; // prevent iloop from _buildInternal below. | |
| 588 | 615 |
| 589 reflect.copyPublicFields(this, oldComponent); | 616 oldComponent._dirty = true; |
| 617 _dirty = false; | |
| 590 | 618 |
| 591 oldComponent._dirty = true; | 619 oldComponent._syncInternal(); |
| 592 _dirty = false; | 620 return true; // Retain old component |
| 593 | |
| 594 oldComponent._buildInternal(host, insertBefore); | |
| 595 return true; // Must retain old component | |
| 596 } | |
| 597 | |
| 598 _vdom = oldComponent._vdom; | |
| 599 _buildInternal(host, insertBefore); | |
| 600 return false; | |
| 601 } | 621 } |
| 602 | 622 |
| 603 void _buildInternal(sky.Node host, sky.Node insertBefore) { | 623 void _syncInternal([sky.Node host, sky.Node insertBefore]) { |
| 604 if (!_dirty) { | 624 if (!_dirty) { |
| 605 assert(_vdom != null); | 625 assert(_vdom != null); |
| 606 return; | 626 return; |
| 607 } | 627 } |
| 608 | 628 |
| 609 var oldRendered = _vdom; | 629 var oldRendered = _vdom; |
| 610 bool mounting = oldRendered == null; | 630 bool mounting = oldRendered == null; |
| 631 | |
| 611 int lastOrder = _currentOrder; | 632 int lastOrder = _currentOrder; |
| 612 _currentOrder = _order; | 633 _currentOrder = _order; |
| 613 _currentlyRendering = this; | 634 _currentlyRendering = this; |
| 614 _vdom = build(); | 635 _vdom = build(); |
| 615 _currentlyRendering = null; | 636 _currentlyRendering = null; |
| 616 _currentOrder = lastOrder; | 637 _currentOrder = lastOrder; |
| 617 | 638 |
| 618 _vdom.events.addAll(events); | 639 _vdom.events.addAll(events); |
| 619 | 640 |
| 620 _dirty = false; | 641 _dirty = false; |
| 621 | 642 |
| 622 // TODO(rafaelw): This eagerly removes the old DOM. It may be that a | 643 // TODO(rafaelw): This eagerly removes the old DOM. It may be that a |
| 623 // new component was built that could re-use some of it. Consider | 644 // new component was built that could re-use some of it. Consider |
| 624 // syncing the new VDOM against the old one. | 645 // syncing the new VDOM against the old one. |
| 625 if (oldRendered != null && | 646 if (oldRendered != null && |
| 626 _vdom.runtimeType != oldRendered.runtimeType) { | 647 _vdom.runtimeType != oldRendered.runtimeType) { |
| 627 oldRendered._remove(); | 648 var oldRoot = oldRendered._root; |
| 649 host = oldRoot.parentNode; | |
| 650 insertBefore = oldRoot.nextSibling; | |
| 651 oldRendered._unmount(); | |
| 628 oldRendered = null; | 652 oldRendered = null; |
| 629 } | 653 } |
| 630 | 654 |
| 631 if (_vdom._sync(oldRendered, host, insertBefore)) { | 655 if (_vdom._sync(oldRendered, this, host, insertBefore)) { |
| 632 _vdom = oldRendered; // retain stateful component | 656 _vdom = oldRendered; // retain stateful component |
| 633 } | 657 } |
| 658 | |
| 634 _root = _vdom._root; | 659 _root = _vdom._root; |
| 635 assert(_vdom._root is sky.Node); | 660 assert(_vdom._root is sky.Node); |
| 636 | 661 |
| 637 if (mounting) { | 662 if (mounting) { |
| 638 didMount(); | 663 didMount(); |
| 639 } | 664 } |
| 640 } | 665 } |
| 641 | 666 |
| 642 void _buildIfDirty() { | 667 void _buildIfDirty() { |
| 643 if (_removed) | 668 if (_defunct) |
| 644 return; | 669 return; |
| 645 | 670 |
| 646 assert(_vdom != null); | 671 assert(_vdom != null); |
| 647 | 672 |
| 648 var vdom = _vdom; | 673 var vdom = _vdom; |
| 649 while (vdom is Component) { | 674 while (vdom is Component) { |
| 650 vdom = vdom._vdom; | 675 vdom = vdom._vdom; |
| 651 } | 676 } |
| 652 | 677 |
| 653 assert(vdom._root != null); | 678 assert(vdom._root != null); |
| 654 sky.Node root = vdom._root; | 679 _syncInternal(); |
| 655 | |
| 656 _buildInternal(root.parentNode, root.nextSibling); | |
| 657 } | 680 } |
| 658 | 681 |
| 659 void setState(Function fn()) { | 682 void setState(Function fn()) { |
| 660 assert(_vdom != null || _removed); // cannot setState before mounting. | 683 assert(_vdom != null || _defunct); // cannot setState before mounting. |
| 661 _stateful = true; | 684 _stateful = true; |
| 662 fn(); | 685 fn(); |
| 663 if (!_removed && _currentlyRendering != this) { | 686 if (!_defunct && _currentlyRendering != this) { |
| 664 _dirty = true; | 687 _dirty = true; |
| 665 _scheduleComponentForRender(this); | 688 _scheduleComponentForRender(this); |
| 666 } | 689 } |
| 667 } | 690 } |
| 668 | 691 |
| 669 Node build(); | 692 Node build(); |
| 670 } | 693 } |
| 671 | 694 |
| 672 abstract class App extends Component { | 695 abstract class App extends Component { |
| 673 sky.Node _host = null; | 696 sky.Node _host = null; |
| 674 App() : super(stateful: true) { | 697 App() : super(stateful: true) { |
| 675 _host = sky.document.createElement('div'); | 698 _host = sky.document.createElement('div'); |
| 676 sky.document.appendChild(_host); | 699 sky.document.appendChild(_host); |
| 677 | 700 |
| 678 new Future.microtask(() { | 701 new Future.microtask(() { |
| 679 Stopwatch sw = new Stopwatch()..start(); | 702 Stopwatch sw = new Stopwatch()..start(); |
| 680 | 703 |
| 681 _sync(null, _host, null); | 704 _mount(null, _host, null); |
| 682 assert(_root is sky.Node); | 705 assert(_root is sky.Node); |
| 683 | 706 |
| 684 sw.stop(); | 707 sw.stop(); |
| 685 if (_shouldLogRenderDuration) | 708 if (_shouldLogRenderDuration) |
| 686 print("Initial build: ${sw.elapsedMicroseconds} microseconds"); | 709 print("Initial build: ${sw.elapsedMicroseconds} microseconds"); |
| 687 }); | 710 }); |
| 688 } | 711 } |
| 689 } | 712 } |
| OLD | NEW |