Chromium Code Reviews| 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 $LIBRARY; | |
| 6 | |
| 7 $(ANNOTATIONS)class $CLASSNAME$EXTENDS$NATIVESPEC { | |
| 8 Stream<AudioProcessingEvent> _eventStream; | |
|
Anton Muhin
2013/03/14 08:27:39
please, no: we agreed that we're going to implemen
Anton Muhin
2013/03/14 09:30:52
My bad, please, disregard.
Emily Fortuna
2013/03/14 18:11:50
I made this change because Pavel asked for it agai
| |
| 9 | |
| 10 /** | |
| 11 * Get a Stream that fires events when AudioProcessingEvents occur. | |
| 12 * This particular stream is special in that it only allows one listener to a | |
| 13 * given stream. Subsequent calls to [listen] after the first call will throw | |
| 14 */ | |
| 15 Stream<AudioProcessingEvent> get onAudioProcess { | |
| 16 if (_eventStream == null) { | |
| 17 _eventStream = new _AudioEventStream(this); | |
| 18 } | |
| 19 return _eventStream; | |
| 20 } | |
| 21 $!MEMBERS | |
| 22 } | |
| 23 | |
| 24 class _AudioEventStreamSubscription<AudioProcessingEvent> extends | |
|
Anton Muhin
2013/03/14 09:30:52
why this generic argument? looks like a typo.
Emily Fortuna
2013/03/14 18:11:50
removed. it was left over from the general EventSt
| |
| 25 StreamSubscription<AudioProcessingEvent> { | |
| 26 EventListener _callback; | |
|
Anton Muhin
2013/03/14 09:30:52
if we prohibit null _callback, is it true _callbac
Emily Fortuna
2013/03/14 18:11:50
True. Fixed up. I had the paused variable to disti
| |
| 27 ScriptProcessorNode _target; | |
| 28 bool _paused; | |
| 29 | |
| 30 _AudioEventStreamSubscription(this._target, this._callback) { | |
| 31 _paused = false; | |
|
Anton Muhin
2013/03/14 09:30:52
maybe move into field declaration?
Emily Fortuna
2013/03/14 18:11:50
Done.
| |
| 32 _tryResume(); | |
| 33 } | |
| 34 | |
| 35 void cancel() { | |
|
Anton Muhin
2013/03/14 09:30:52
shouldn't it also clear _AudioEventStream._streamS
Anton Muhin
2013/03/14 09:32:22
cannot we have:
void cancel() { pause(); } ?
Emily Fortuna
2013/03/14 18:11:50
Done.
Emily Fortuna
2013/03/14 18:11:50
No, a StreamSubscription can only be associated wi
| |
| 36 if (_callback != null) { | |
| 37 throw new StateError("Subscription has already been canceled."); | |
| 38 } | |
| 39 _unlisten(); | |
| 40 } | |
| 41 | |
| 42 void onData(void callback(AudioProcessingEvent)) { | |
| 43 if (_callback != null) { | |
| 44 throw new StateError("Subscription has been canceled."); | |
| 45 } | |
| 46 _setCallback(callback); | |
| 47 } | |
| 48 | |
| 49 /// Has no effect. | |
| 50 void onError(void handleError(AsyncError error)) {} | |
| 51 | |
| 52 /// Has no effect. | |
| 53 void onDone(void handleDone()) {} | |
| 54 | |
| 55 void pause([Future resumeSignal]) { | |
| 56 if (_callback != null) { | |
| 57 throw new StateError("Subscription has been canceled."); | |
| 58 } | |
| 59 _unlisten(); | |
| 60 _paused = true; | |
| 61 | |
| 62 if (resumeSignal != null) { | |
| 63 resumeSignal.whenComplete(() { _paused = false; resume(); }); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 void resume() { | |
| 68 if (_callback != null) { | |
|
Anton Muhin
2013/03/14 09:30:52
if (_callback == null) ? and has been resumed?
Emily Fortuna
2013/03/14 18:11:50
Done.
| |
| 69 throw new StateError("Subscription has been canceled."); | |
| 70 } | |
| 71 if (!_paused) { | |
| 72 throw new StateError("Subscription is not paused."); | |
| 73 } | |
| 74 _tryResume(); | |
| 75 } | |
| 76 | |
| 77 void _tryResume() { | |
| 78 if (_callback != null) { | |
| 79 _setCallback(_callback); | |
| 80 _callback = null; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void _unlisten() { | |
| 85 if (_callback == null) { | |
| 86 var noop = (event) {}; | |
|
Anton Muhin
2013/03/14 09:30:52
why not null?
Emily Fortuna
2013/03/14 18:11:50
Done.
| |
| 87 $if DART2JS | |
|
Anton Muhin
2013/03/14 09:30:52
Why have conditional code here? Why not have _onA
Emily Fortuna
2013/03/14 18:11:50
Because in this version we're returning a custom S
| |
| 88 _callback = JS('Function', '#.onaudioprocess', _target); | |
| 89 $else | |
| 90 // TODO(podivilov): Implement on Dartium. | |
| 91 $endif | |
| 92 _setCallback(noop); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 void _setCallback(callbackFunc) { | |
| 97 $if DART2JS | |
| 98 JS('void', '#.onaudioprocess = #', _target, | |
| 99 convertDartClosureToJS(_callback, 1)); | |
| 100 $else | |
| 101 // TODO(podivilov): Implement on Dartium. | |
| 102 $endif | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 /** | |
| 107 * Adapter for exposing DOM events as Dart streams. | |
| 108 */ | |
| 109 class _AudioEventStream<AudioProcessingEvent> | |
|
Anton Muhin
2013/03/14 09:30:52
why generic argument here?
Emily Fortuna
2013/03/14 18:11:50
removed
| |
| 110 extends Stream<AudioProcessingEvent> { | |
| 111 static Set _createdTargets = new Set(); | |
|
Anton Muhin
2013/03/14 09:30:52
nit: do not we name static _CREATED_TARGETS? And
Emily Fortuna
2013/03/14 18:11:50
I thought all caps were for constants. This isn't
| |
| 112 ScriptProcessorNode _target; | |
| 113 StreamSubscription _streamSubscription; | |
| 114 | |
| 115 _AudioEventStream(target) { | |
| 116 if (_createdTargets.contains(target)) { | |
|
Anton Muhin
2013/03/14 09:30:52
why we need this check?
Note as well, that you ne
Emily Fortuna
2013/03/14 18:11:50
This was to enforce the "one listener per ScriptPr
| |
| 117 throw new ArgumentError('This ScriptProcessorNode already has an event ' | |
|
Anton Muhin
2013/03/14 09:30:52
nit: there is a mix of " and ' for string literals
Emily Fortuna
2013/03/14 18:11:50
Done.
| |
| 118 'stream associated with it. There can be only one listener per ' | |
| 119 'ScriptProcessorNode'); | |
| 120 } else { | |
|
Anton Muhin
2013/03/14 09:30:52
do we need else clause as then class always throws
Emily Fortuna
2013/03/14 18:11:50
removed else clause
| |
| 121 _target = target; | |
| 122 _createdTargets.add(_target); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 bool get isBroadcast => true; | |
| 127 | |
| 128 StreamSubscription<AudioProcessingEvent> listen( | |
| 129 void onData(AudioProcessingEvent event), | |
|
Anton Muhin
2013/03/14 09:30:52
is onData null allowed?
Emily Fortuna
2013/03/14 18:11:50
no longer. I could have it convert to the no-op fu
| |
| 130 { void onError(AsyncError error), | |
|
Anton Muhin
2013/03/14 09:30:52
nit: no space after {
Emily Fortuna
2013/03/14 18:11:50
Done.
| |
| 131 void onDone(), | |
| 132 bool unsubscribeOnError}) { | |
| 133 if (_streamSubscription != null) { | |
| 134 throw new StateError('Only one listener is allowed for this particular ' | |
| 135 'stream.'); | |
| 136 } | |
| 137 _streamSubscription = | |
| 138 new _AudioEventStreamSubscription<AudioProcessingEvent>( | |
| 139 this._target, onData); | |
| 140 return _streamSubscription; | |
| 141 } | |
| 142 } | |
| 143 | |
| OLD | NEW |