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 /// Provides notifications whenever a Javascript Custom Elements is created. | |
6 /// This will not work unless `created_watcher.js` is loaded. | |
7 library web_components.created_watcher; | |
8 | |
9 import 'dart:async' show Stream, StreamController; | |
10 import 'dart:html' show document, Element; | |
11 import 'dart:js' show JsObject, JsFunction; | |
12 | |
13 final _doc = new JsObject.fromBrowserObject(document); | |
14 | |
15 /// Returns whether [watch] is supported, which requires to have | |
16 /// `created_watcher.js` already loaded in the page. | |
Siggi Cherem (dart-lang)
2014/06/06 22:31:47
one difference is that for now I'm keeping it simp
justinfagnani
2014/06/06 23:33:05
sounds good, i'll be a hybrid page anyway, if some
| |
17 bool get isSupported => _doc.hasProperty('registerElementCreatedWatcher'); | |
18 | |
19 /// Watches when Javascript custom elements named [tagName] are created and | |
20 /// provides a reference to the created element in the returned stream. | |
justinfagnani
2014/06/06 23:33:05
You might want to mention that it's a single-subsc
Siggi Cherem (dart-lang)
2014/06/09 17:10:27
I switched to broadcast, but clarified in the comm
| |
21 Stream<Element> watch(String tagName, {String extendsTag}) { | |
22 if (!isSupported) { | |
23 throw new UnsupportedError("Couldn't find `registerElementCreatedWatcher`. " | |
24 "Please make sure that `packages/web_components/created_watcher.js` is " | |
25 "loaded and available before calling this function."); | |
26 } | |
27 | |
28 var controller = new StreamController<Element>(sync: true); | |
29 var params = {'createdCallback': new JsFunction.withThis(controller.add)}; | |
30 if (extendsTag != null) params['extends'] = extendsTag; | |
31 _doc.callMethod('registerElementCreatedWatcher', | |
32 [tagName, new JsObject.jsify(params)]); | |
33 // TODO(sigmund): remove watchers when susbcriptions are cancelled. | |
34 return controller.stream; | |
35 } | |
OLD | NEW |