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 part of repositories; |
| 6 |
| 7 class TargetChangeEvent implements M.TargetChangeEvent { |
| 8 final TargetRepository repository; |
| 9 TargetChangeEvent(this.repository); |
| 10 } |
| 11 |
| 12 class TargetRepository implements M.TargetRepository { |
| 13 |
| 14 static const _historyKey = 'history'; |
| 15 |
| 16 final StreamController<TargetChangeEvent> _onChange; |
| 17 final Stream<TargetChangeEvent> onChange; |
| 18 final SettingsRepository _settings = new SettingsRepository('targetManager'); |
| 19 |
| 20 final List<SC.WebSocketVMTarget> _list = <SC.WebSocketVMTarget>[]; |
| 21 SC.WebSocketVMTarget current; |
| 22 |
| 23 factory TargetRepository() { |
| 24 var controller = new StreamController<TargetChangeEvent>(); |
| 25 var stream = controller.stream.asBroadcastStream(); |
| 26 return new TargetRepository._(controller, stream); |
| 27 } |
| 28 |
| 29 TargetRepository._(this._onChange, this.onChange) { |
| 30 _restore(); |
| 31 if (_list.isEmpty) { |
| 32 _list.add(new SC.WebSocketVMTarget(_networkAddressOfDefaultTarget())); |
| 33 } |
| 34 current = _list.first; |
| 35 } |
| 36 |
| 37 void add(String address) { |
| 38 if (_find(address) != null) return; |
| 39 _list.insert(0, new SC.WebSocketVMTarget(address)); |
| 40 _onChange.add(new TargetChangeEvent(this)); |
| 41 _store(); |
| 42 } |
| 43 |
| 44 Iterable<SC.WebSocketVMTarget> list() => _list; |
| 45 |
| 46 void setCurrent(M.Target t) { |
| 47 SC.WebSocketVMTarget target = t as SC.WebSocketVMTarget; |
| 48 if (!_list.contains(target)) return; |
| 49 current = target; |
| 50 _onChange.add(new TargetChangeEvent(this)); |
| 51 } |
| 52 |
| 53 void delete(o) { |
| 54 if (_list.remove(o)) { |
| 55 if (o == current) { |
| 56 current = null; |
| 57 } |
| 58 _onChange.add(new TargetChangeEvent(this)); |
| 59 _store(); |
| 60 } |
| 61 } |
| 62 |
| 63 /// Read settings from data store. |
| 64 void _restore() { |
| 65 _list.clear(); |
| 66 var loaded = _settings.get(_historyKey); |
| 67 if (loaded == null) { |
| 68 return; |
| 69 } |
| 70 _list.addAll(loaded.map((i) => new SC.WebSocketVMTarget.fromMap(i))); |
| 71 _list.sort((SC.WebSocketVMTarget a, SC.WebSocketVMTarget b) { |
| 72 return b.lastConnectionTime.compareTo(a.lastConnectionTime); |
| 73 }); |
| 74 } |
| 75 |
| 76 /// After making a change, update settings. |
| 77 void _store() { |
| 78 _settings.set(_historyKey, _list); |
| 79 } |
| 80 |
| 81 /// Find by networkAddress. |
| 82 SC.WebSocketVMTarget _find(String networkAddress) { |
| 83 for (SC.WebSocketVMTarget item in _list) { |
| 84 if (item.networkAddress == networkAddress) { |
| 85 return item; |
| 86 } |
| 87 } |
| 88 return null; |
| 89 } |
| 90 |
| 91 static String _networkAddressOfDefaultTarget() { |
| 92 if (Utils.runningInJavaScript()) { |
| 93 // We are running as JavaScript, use the same host that Observatory has |
| 94 // been loaded from. |
| 95 return 'ws://${window.location.host}/ws'; |
| 96 } else { |
| 97 // Otherwise, assume we are running from Dart Editor and want to connect |
| 98 // to the default host. |
| 99 return 'ws://localhost:8181/ws'; |
| 100 } |
| 101 } |
| 102 } |
OLD | NEW |