OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart 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 file. |
| 4 |
| 5 import 'dart:html'; |
| 6 import 'dart:async'; |
| 7 import 'package:observatory/models.dart' as M; |
| 8 import 'package:observatory/src/elements/curly_block.dart'; |
| 9 import 'package:observatory/src/elements/instance_ref.dart'; |
| 10 import 'package:observatory/src/elements/helpers/any_ref.dart'; |
| 11 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; |
| 12 import 'package:observatory/src/elements/helpers/tag.dart'; |
| 13 |
| 14 class StronglyReachableInstancesElement extends HtmlElement |
| 15 implements Renderable { |
| 16 static const tag = const Tag<StronglyReachableInstancesElement>( |
| 17 'strongly-reachable-instances', |
| 18 dependencies: const [ |
| 19 CurlyBlockElement.tag, |
| 20 InstanceRefElement.tag |
| 21 ]); |
| 22 |
| 23 RenderingScheduler<StronglyReachableInstancesElement> _r; |
| 24 |
| 25 Stream<RenderedEvent<StronglyReachableInstancesElement>> get onRendered => |
| 26 _r.onRendered; |
| 27 |
| 28 M.IsolateRef _isolate; |
| 29 M.ClassRef _cls; |
| 30 M.StronglyReachableInstancesRepository _stronglyReachableInstances; |
| 31 M.InstanceRepository _instances; |
| 32 M.InstanceSet _result; |
| 33 bool _expanded = false; |
| 34 |
| 35 M.IsolateRef get isolate => _isolate; |
| 36 M.ClassRef get cls => _cls; |
| 37 |
| 38 factory StronglyReachableInstancesElement(M.IsolateRef isolate, |
| 39 M.ClassRef cls, |
| 40 M.StronglyReachableInstancesRepository stronglyReachable, |
| 41 M.InstanceRepository instances, |
| 42 {RenderingQueue queue}) { |
| 43 assert(isolate != null); |
| 44 assert(cls != null); |
| 45 assert(stronglyReachable != null); |
| 46 assert(instances != null); |
| 47 StronglyReachableInstancesElement e = document.createElement(tag.name); |
| 48 e._r = new RenderingScheduler(e, queue: queue); |
| 49 e._isolate = isolate; |
| 50 e._cls = cls; |
| 51 e._stronglyReachableInstances = stronglyReachable; |
| 52 e._instances = instances; |
| 53 return e; |
| 54 } |
| 55 |
| 56 StronglyReachableInstancesElement.created() : super.created(); |
| 57 |
| 58 @override |
| 59 void attached() { |
| 60 super.attached(); |
| 61 _r.enable(); |
| 62 } |
| 63 |
| 64 @override |
| 65 void detached() { |
| 66 super.detached(); |
| 67 children = []; |
| 68 _r.disable(notify: true); |
| 69 } |
| 70 |
| 71 void render() { |
| 72 children = [ |
| 73 new CurlyBlockElement(expanded: _expanded, queue: _r.queue) |
| 74 ..children = _createContent() |
| 75 ..onToggle.listen((e) async { |
| 76 _expanded = e.control.expanded; |
| 77 e.control.disabled = true; |
| 78 await _refresh(); |
| 79 e.control.disabled = false; |
| 80 }) |
| 81 ]; |
| 82 } |
| 83 |
| 84 Future _refresh() async { |
| 85 _result = null; |
| 86 _result = await _stronglyReachableInstances.get(_isolate, _cls); |
| 87 _r.dirty(); |
| 88 } |
| 89 |
| 90 List<Element> _createContent() { |
| 91 if (_result == null) { |
| 92 return [ |
| 93 new SpanElement()..text = 'Loading...' |
| 94 ]; |
| 95 } |
| 96 final content = _result.samples.map((sample) => |
| 97 new DivElement() |
| 98 ..children = [ |
| 99 anyRef(_isolate, sample, _instances, queue: _r.queue) |
| 100 ] |
| 101 ).toList(); |
| 102 content.add(new DivElement() |
| 103 ..children = ([] |
| 104 ..addAll(_createShowMoreButton()) |
| 105 ..add(new SpanElement()..text = ' of total ${_result.count}')) |
| 106 ); |
| 107 return content; |
| 108 } |
| 109 |
| 110 List<Element> _createShowMoreButton() { |
| 111 final samples = _result.samples.toList(); |
| 112 if (samples.length == _result.count) { |
| 113 return []; |
| 114 } |
| 115 final count = samples.length; |
| 116 final button = new ButtonElement() |
| 117 ..text = 'show next ${count}'; |
| 118 button.onClick.listen((_) async { |
| 119 button.disabled = true; |
| 120 _result = await _stronglyReachableInstances.get(_isolate, _cls, |
| 121 limit: count * 2); |
| 122 _r.dirty(); |
| 123 }); |
| 124 return [button]; |
| 125 } |
| 126 } |
OLD | NEW |