| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE.md file. | |
| 4 | |
| 5 import 'model.dart'; | |
| 6 import 'dart/todomvc_presenter.dart'; | |
| 7 import 'dart/todomvc_presenter_model.dart'; | |
| 8 | |
| 9 import 'dart/presentation_graph.dart' as node; | |
| 10 | |
| 11 class ItemPair { | |
| 12 final Item item; | |
| 13 final Immutable presentation; | |
| 14 ItemPair(this.item, this.presentation); | |
| 15 } | |
| 16 | |
| 17 class TodoMVCImpl extends TodoMVCPresenter { | |
| 18 | |
| 19 Model _model = new Model(); | |
| 20 | |
| 21 TodoMVCImpl() { | |
| 22 _model.createItem("My default todo"); | |
| 23 _model.createItem("Some other todo"); | |
| 24 } | |
| 25 | |
| 26 void createItem(title) { | |
| 27 _model.createItem(title.str); | |
| 28 } | |
| 29 | |
| 30 void clearItems() { | |
| 31 _model.clearItems(); | |
| 32 } | |
| 33 | |
| 34 // Cache of previously drawn items. | |
| 35 // TODO(zerny): use a map once we have a proper implementation. | |
| 36 List<ItemPair> _cachePrevious = new List(); | |
| 37 List<ItemPair> _cacheCurrent = new List(); | |
| 38 | |
| 39 Immutable _lookup(Item item, int index) { | |
| 40 // Look for a cached item in the range [index; index + 1] | |
| 41 for (var i = 0; i < 2; ++i) { | |
| 42 var j = index + i; | |
| 43 if (j >= _cachePrevious.length) return null; | |
| 44 ItemPair cached = _cachePrevious[j]; | |
| 45 if (cached.item == item) return cached.presentation; | |
| 46 } | |
| 47 return null; | |
| 48 } | |
| 49 | |
| 50 void _add(int index, Item item, Immutable presentation) { | |
| 51 _cacheCurrent[index] = new ItemPair(item, presentation); | |
| 52 } | |
| 53 | |
| 54 Immutable render(previous) { | |
| 55 // Swap previous and current cache and resize current to model-item count. | |
| 56 var tmp = _cachePrevious; | |
| 57 tmp.length = _model.todos.length; | |
| 58 _cachePrevious = _cacheCurrent; | |
| 59 _cacheCurrent = tmp; | |
| 60 return _renderList(0, previous); | |
| 61 } | |
| 62 | |
| 63 Immutable _renderList(int index, Immutable previous) => | |
| 64 (index == _model.todos.length) | |
| 65 ? node.nil(previous) | |
| 66 : node.cons( | |
| 67 _renderItemFromCache(index, node.getConsFst(previous)), | |
| 68 _renderList(index + 1, node.getConsSnd(previous)), | |
| 69 null, | |
| 70 null, | |
| 71 null, | |
| 72 previous); | |
| 73 | |
| 74 Immutable _renderItemFromCache(int index, Immutable previous) { | |
| 75 Item item = _model.todos[index]; | |
| 76 Cons cached = _lookup(item, index); | |
| 77 Immutable current = (cached == null) | |
| 78 ? _renderItem(item, previous) | |
| 79 : node.cons( | |
| 80 node.str(item.title, cached.fst), | |
| 81 node.truth(item.done, cached.snd), | |
| 82 // Reuse the handlers since we know that item is the same as the | |
| 83 // item used for the creating the cached handlers. | |
| 84 cached.deleteEvent, | |
| 85 cached.completeEvent, | |
| 86 cached.uncompleteEvent, | |
| 87 cached); | |
| 88 _add(index, item, current); | |
| 89 return current; | |
| 90 } | |
| 91 | |
| 92 Immutable _renderItem(Item item, Immutable previous) => | |
| 93 node.cons( | |
| 94 node.str(item.title, node.getConsFst(previous)), | |
| 95 node.truth(item.done, node.getConsSnd(previous)), | |
| 96 new EventHandler(() { _model.todos.remove(item); }), | |
| 97 new EventHandler(item.complete), | |
| 98 new EventHandler(item.uncomplete)); | |
| 99 } | |
| OLD | NEW |