Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(698)

Unified Diff: sdk/lib/web_audio/dart2js/web_audio_dart2js.dart

Issue 11510013: Making ScriptProcessorNode not an EventTarget. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/web_audio/dart2js/web_audio_dart2js.dart
diff --git a/sdk/lib/web_audio/dart2js/web_audio_dart2js.dart b/sdk/lib/web_audio/dart2js/web_audio_dart2js.dart
index 13d8b3645d7625c5fcef55be734e8ccd5602da34..269ca0692a505287290b068d747d0ad24bc07efd 100644
--- a/sdk/lib/web_audio/dart2js/web_audio_dart2js.dart
+++ b/sdk/lib/web_audio/dart2js/web_audio_dart2js.dart
@@ -4,7 +4,7 @@ import 'dart:async';
import 'dart:collection';
import 'dart:html';
import 'dart:html_common';
-import 'dart:_js_helper' show Creates, Returns;
+import 'dart:_js_helper' show Creates, Returns, convertDartClosureToJS;
import 'dart:_foreign_helper' show JS;
// DO NOT EDIT - unless you are editing documentation as per:
// https://code.google.com/p/dart/wiki/ContributingHTMLDocumentation
@@ -184,6 +184,9 @@ class AudioContext extends EventTarget native "*AudioContext" {
@DocsEditable
static const EventStreamProvider<Event> completeEvent = const EventStreamProvider<Event>('complete');
+ /// Checks if this type is supported on the current platform.
+ static bool get supported => JS('bool', '!!(window.AudioContex || window.webkitAudioContext)');
+
@DomName('AudioContext.activeSourceCount')
@DocsEditable
final int activeSourceCount;
@@ -813,19 +816,145 @@ class PannerNode extends AudioNode native "*PannerNode" {
@DocsEditable
void setVelocity(num x, num y, num z) native;
}
-// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// 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.
-@DocsEditable
@DomName('ScriptProcessorNode')
-class ScriptProcessorNode extends AudioNode implements EventTarget native "*ScriptProcessorNode" {
+class ScriptProcessorNode extends AudioNode native "*ScriptProcessorNode" {
+ Stream<AudioProcessingEvent> _eventStream;
+
+ /**
+ * Get a Stream that fires events when AudioProcessingEvents occur.
+ * This particular stream is special in that it only allows one listener to a
+ * given stream. Subsequent calls to [listen] after the first call will throw
+ */
+ Stream<AudioProcessingEvent> get onAudioProcess {
+ if (_eventStream == null) {
+ _eventStream = new _AudioEventStream(this);
+ }
+ return _eventStream;
+ }
@DomName('ScriptProcessorNode.bufferSize')
@DocsEditable
final int bufferSize;
+
}
+
+class _AudioEventStreamSubscription<AudioProcessingEvent> extends
+ StreamSubscription<AudioProcessingEvent> {
+ EventListener _callback;
+ ScriptProcessorNode _target;
+ bool _paused;
+
+ _AudioEventStreamSubscription(this._target, this._callback) {
+ _paused = false;
+ _tryResume();
+ }
+
+ void cancel() {
+ if (_callback != null) {
+ throw new StateError("Subscription has already been canceled.");
+ }
+ _unlisten();
+ }
+
+ void onData(void callback(AudioProcessingEvent)) {
+ if (_callback != null) {
+ throw new StateError("Subscription has been canceled.");
+ }
+ _setCallback(callback);
+ }
+
+ /// Has no effect.
+ void onError(void handleError(AsyncError error)) {}
+
+ /// Has no effect.
+ void onDone(void handleDone()) {}
+
+ void pause([Future resumeSignal]) {
+ if (_callback != null) {
+ throw new StateError("Subscription has been canceled.");
+ }
+ _unlisten();
+ _paused = true;
+
+ if (resumeSignal != null) {
+ resumeSignal.whenComplete(() { _paused = false; resume(); });
+ }
+ }
+
+ void resume() {
+ if (_callback != null) {
+ throw new StateError("Subscription has been canceled.");
+ }
+ if (!_paused) {
+ throw new StateError("Subscription is not paused.");
+ }
+ _tryResume();
+ }
+
+ void _tryResume() {
+ if (_callback != null) {
+ _setCallback(_callback);
+ _callback = null;
+ }
+ }
+
+ void _unlisten() {
+ if (_callback == null) {
+ var noop = (event) {};
+ _callback = JS('Function', '#.onaudioprocess', _target);
+ _setCallback(noop);
+ }
+ }
+
+ void _setCallback(callbackFunc) {
+ JS('void', '#.onaudioprocess = #', _target,
+ convertDartClosureToJS(_callback, 1));
+ }
+}
+
+/**
+ * Adapter for exposing DOM events as Dart streams.
+ */
+class _AudioEventStream<AudioProcessingEvent>
+ extends Stream<AudioProcessingEvent> {
+ static Set _createdTargets = new Set();
+ ScriptProcessorNode _target;
+ StreamSubscription _streamSubscription;
+
+ _AudioEventStream(target) {
+ if (_createdTargets.contains(target)) {
+ throw new ArgumentError('This ScriptProcessorNode already has an event '
+ 'stream associated with it. There can be only one listener per '
+ 'ScriptProcessorNode');
+ } else {
+ _target = target;
+ _createdTargets.add(_target);
+ }
+ }
+
+ bool get isBroadcast => true;
+
+ StreamSubscription<AudioProcessingEvent> listen(
+ void onData(AudioProcessingEvent event),
+ { void onError(AsyncError error),
+ void onDone(),
+ bool unsubscribeOnError}) {
+ if (_streamSubscription != null) {
+ throw new StateError('Only one listener is allowed for this particular '
+ 'stream.');
+ }
+ _streamSubscription =
+ new _AudioEventStreamSubscription<AudioProcessingEvent>(
+ this._target, onData);
+ return _streamSubscription;
+ }
+}
+
// Copyright (c) 2012, 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.

Powered by Google App Engine
This is Rietveld 408576698