Chromium Code Reviews| Index: pkg/web_components/lib/created_watcher.dart |
| diff --git a/pkg/web_components/lib/created_watcher.dart b/pkg/web_components/lib/created_watcher.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..36a7e6fb4a8450143eed7f7faf1e1164d7baf96e |
| --- /dev/null |
| +++ b/pkg/web_components/lib/created_watcher.dart |
| @@ -0,0 +1,35 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/// Provides notifications whenever a Javascript Custom Elements is created. |
| +/// This will not work unless `created_watcher.js` is loaded. |
| +library web_components.created_watcher; |
| + |
| +import 'dart:async' show Stream, StreamController; |
| +import 'dart:html' show document, Element; |
| +import 'dart:js' show JsObject, JsFunction; |
| + |
| +final _doc = new JsObject.fromBrowserObject(document); |
| + |
| +/// Returns whether [watch] is supported, which requires to have |
| +/// `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
|
| +bool get isSupported => _doc.hasProperty('registerElementCreatedWatcher'); |
| + |
| +/// Watches when Javascript custom elements named [tagName] are created and |
| +/// 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
|
| +Stream<Element> watch(String tagName, {String extendsTag}) { |
| + if (!isSupported) { |
| + throw new UnsupportedError("Couldn't find `registerElementCreatedWatcher`. " |
| + "Please make sure that `packages/web_components/created_watcher.js` is " |
| + "loaded and available before calling this function."); |
| + } |
| + |
| + var controller = new StreamController<Element>(sync: true); |
| + var params = {'createdCallback': new JsFunction.withThis(controller.add)}; |
| + if (extendsTag != null) params['extends'] = extendsTag; |
| + _doc.callMethod('registerElementCreatedWatcher', |
| + [tagName, new JsObject.jsify(params)]); |
| + // TODO(sigmund): remove watchers when susbcriptions are cancelled. |
| + return controller.stream; |
| +} |