OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, 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 js.wrapping; | |
6 | |
7 class JsObjectToMapAdapter<V> extends TypedProxy implements Map<String,V> { | |
8 static JsObjectToMapAdapter cast(Proxy proxy, [Translator translator]) => | |
9 proxy == null ? null : | |
10 new JsObjectToMapAdapter.fromProxy(proxy, translator); | |
11 static JsObjectToMapAdapter castMapOfSerializables(Proxy proxy, | |
12 Mapper<dynamic, Serializable> fromJs, {mapOnlyNotNull: false}) => | |
13 proxy == null ? null : new JsObjectToMapAdapter.fromProxy(proxy, | |
14 new TranslatorForSerializable(fromJs, | |
15 mapOnlyNotNull: mapOnlyNotNull)); | |
16 | |
17 final Translator<V> _translator; | |
18 | |
19 JsObjectToMapAdapter.fromProxy(Proxy proxy, [Translator<V> translator]) : | |
20 super.fromProxy(proxy), this._translator = translator; | |
21 | |
22 @override V operator [](String key) => _fromJs($unsafe[key]); | |
23 @override void operator []=(String key, V value) { | |
24 $unsafe[key] = _toJs(value); | |
25 } | |
26 @override V remove(String key) { | |
27 final value = this[key]; | |
28 deleteProperty($unsafe, key); | |
29 return value; | |
30 } | |
31 @override Iterable<String> get keys => | |
32 JsArrayToListAdapter.cast(context['Object'].keys($unsafe)); | |
33 | |
34 // use Maps to implement functions | |
35 @override bool containsValue(V value) => Maps.containsValue(this, value); | |
36 @override bool containsKey(String key) => | |
37 context['Object'].keys($unsafe).indexOf(key) != -1; | |
38 @override V putIfAbsent(String key, V ifAbsent()) => | |
39 Maps.putIfAbsent(this, key, ifAbsent); | |
40 @override void addAll(Map<String, V> other) { | |
41 if (other != null) { | |
42 other.forEach((k,v) => this[k] = v); | |
43 } | |
44 } | |
45 @override void clear() => Maps.clear(this); | |
46 @override void forEach(void f(String key, V value)) => Maps.forEach(this, f); | |
47 @override Iterable<V> get values => Maps.getValues(this); | |
48 @override int get length => Maps.length(this); | |
49 @override bool get isEmpty => Maps.isEmpty(this); | |
50 @override bool get isNotEmpty => Maps.isNotEmpty(this); | |
51 | |
52 dynamic _toJs(V e) => _translator == null ? e : _translator.toJs(e); | |
53 V _fromJs(dynamic value) => _translator == null ? value : | |
54 _translator.fromJs(value); | |
55 } | |
OLD | NEW |