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

Side by Side Diff: tools/dom/templates/html/impl/impl_Window.darttemplate

Issue 2039963003: Add zone task support for request-anim. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 6 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 unified diff | Download patch
« no previous file with comments | « tests/html/request_animation_task_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of $LIBRARYNAME; 5 part of $LIBRARYNAME;
6 6
7 typedef void RemoveFrameRequestMapping(int id);
8
9 /**
10 * The task object representing animation-frame requests.
11 *
12 * For historical reasons, [Window.requestAnimationFrame] returns an integer
13 * to users. However, zone-tasks must be unique objects, and an integer thus
14 * doesn't work. The [Window] class thus keeps a mapping from the id to the
Lasse Reichstein Nielsen 2016/06/07 12:57:34 second "thus" in two lines. Consider rewording (ma
Lasse Reichstein Nielsen 2016/06/07 12:57:34 id -> integer ID
floitsch 2016/06/10 15:45:29 reworded.
floitsch 2016/06/10 15:45:29 Done.
15 * corresponding task object. All zone related operations work on this task
16 * object, whereas users of [Window.requestAnimationFrame] only see the integer
17 * id.
Lasse Reichstein Nielsen 2016/06/07 12:57:34 id -> ID
floitsch 2016/06/10 15:45:28 Done.
18 *
19 * Since this mapping takes space, it must be removed as soon as the animation-
Lasse Reichstein Nielsen 2016/06/07 12:57:34 takes -> takes up
floitsch 2016/06/10 15:45:29 Done.
20 * frame task triggered. The default-implementations do this automatically, but
Lasse Reichstein Nielsen 2016/06/07 12:57:34 default implementations (no '-') Should probably
Lasse Reichstein Nielsen 2016/06/07 12:57:34 triggered -> has triggered (or: is triggered, or:
floitsch 2016/06/10 15:45:29 Done.
floitsch 2016/06/10 15:45:29 Done.
21 * intercepted implementations of `requestAnimationFrame` must ensure to
22 * call the [AnimationFrameRequestSpecification.removeMapping] function that is
23 * provided in the task specification.
24 */
25 abstract class AnimationFrameTask {
26 /** The id that is returned to users. */
Lasse Reichstein Nielsen 2016/06/07 12:57:34 id -> ID
floitsch 2016/06/10 15:45:28 Done.
27 int get id;
28
29 /** The function that is invoked when the user cancels the request. */
30 void cancel(Window window);
31
32 /** The zone in which the task should run (and cancel). */
Lasse Reichstein Nielsen 2016/06/07 12:57:34 (and cancel) -> (or be cancelled)
floitsch 2016/06/10 15:45:28 No cancel anymore. -> removed.
33 Zone get zone;
34 }
35
36 class _AnimationFrameTask implements AnimationFrameTask {
37 final int id;
38 final Zone zone;
39 final FrameRequestCallback _callback;
40
41 _AnimationFrameTask(this.id, this.zone, this._callback);
42
43 void cancel(Window window) {
44 window._cancelAnimationFrame(this.id);
45 }
46 }
47
48 /**
49 * The task specification for an animation-frame request.
50 *
51 * The task specification of animation-frame requests include a `removeMapping`
52 * function. This function must be called by the created task before it
Lasse Reichstein Nielsen 2016/06/07 12:57:34 include->includes (or specification -> specificati
floitsch 2016/06/10 15:45:29 Done.
53 * runs the frame-request callback.
Lasse Reichstein Nielsen 2016/06/07 12:57:34 Sentence is hard to parse (what does "it" refer to
floitsch 2016/06/10 15:45:29 Removed it.
54 */
55 class AnimationFrameRequestSpecification implements TaskSpecification {
56 final Window window;
Lasse Reichstein Nielsen 2016/06/07 12:57:34 Document.
floitsch 2016/06/10 15:45:29 Done.
57 /**
58 * The callback that should be executed when the animation-frame is ready.
59 *
60 * Note that the callback hasn't been registered in any zone yet.
61 */
62 final FrameRequestCallback callback;
63
64 /**
65 * The function that should be invoked by the task before running [callback].
66 *
67 * See [AnimationFrame].
68 */
69 final RemoveFrameRequestMapping removeMapping;
Lasse Reichstein Nielsen 2016/06/07 12:57:34 Does it make sense to override the removeMapping f
floitsch 2016/06/10 15:45:29 Moved it as a static function on AnimationFrameTas
70
71 AnimationFrameRequestSpecification(
72 this.window, this.callback, this.removeMapping);
73
74 String get name => "dart.html.request-animation-frame";
75 bool get isOneShot => true;
76 }
77
7 @DocsEditable() 78 @DocsEditable()
8 $if DART2JS 79 $if DART2JS
9 $(ANNOTATIONS)@Native("Window,DOMWindow") 80 $(ANNOTATIONS)@Native("Window,DOMWindow")
10 $(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS { 81 $(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS {
11 $else 82 $else
12 $(ANNOTATIONS)$(NATIVESPEC)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS { 83 $(ANNOTATIONS)$(NATIVESPEC)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS {
13 $endif 84 $endif
14 85
15 /** 86 /**
16 * Returns a Future that completes just before the window is about to 87 * Returns a Future that completes just before the window is about to
17 * repaint so the user can draw an animation frame. 88 * repaint so the user can draw an animation frame.
18 * 89 *
19 * If you need to later cancel this animation, use [requestAnimationFrame] 90 * If you need to later cancel this animation, use [requestAnimationFrame]
20 * instead. 91 * instead.
21 * 92 *
22 * The [Future] completes to a timestamp that represents a floating 93 * The [Future] completes to a timestamp that represents a floating
23 * point value of the number of milliseconds that have elapsed since the page 94 * point value of the number of milliseconds that have elapsed since the page
24 * started to load (which is also the timestamp at this call to 95 * started to load (which is also the timestamp at this call to
25 * animationFrame). 96 * animationFrame).
26 * 97 *
27 * Note: The code that runs when the future completes should call 98 * Note: The code that runs when the future completes should call
28 * [animationFrame] again for the animation to continue. 99 * [animationFrame] again for the animation to continue.
29 */ 100 */
30 Future<num> get animationFrame { 101 Future<num> get animationFrame {
31 var completer = new Completer<num>.sync(); 102 var completer = new Completer<num>.sync();
32 requestAnimationFrame((time) { 103 requestAnimationFrame(completer.complete);
33 completer.complete(time);
34 });
35 return completer.future; 104 return completer.future;
36 } 105 }
37 106
38 $if DART2JS 107 $if DART2JS
39 /** 108 /**
40 * The newest document in this window. 109 * The newest document in this window.
41 * 110 *
42 * ## Other resources 111 * ## Other resources
43 * 112 *
44 * * [Loading web 113 * * [Loading web
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 _location = value; 159 _location = value;
91 } 160 }
92 161
93 // Native getter and setter to access raw Location object. 162 // Native getter and setter to access raw Location object.
94 dynamic get _location => JS('Location|Null', '#.location', this); 163 dynamic get _location => JS('Location|Null', '#.location', this);
95 set _location(value) { 164 set _location(value) {
96 JS('void', '#.location = #', this, value); 165 JS('void', '#.location = #', this, value);
97 } 166 }
98 167
99 /** 168 /**
169 * Maps animation-frame request ids to their task objects.
Lasse Reichstein Nielsen 2016/06/07 12:57:34 ids -> IDs
floitsch 2016/06/10 15:45:28 Done.
170 */
171 static final Map<int, _AnimationFrameTask> _animationFrameTasks =
172 <int, _AnimationFrameTask>{};
173
174 /**
100 * Called to draw an animation frame and then request the window to repaint 175 * Called to draw an animation frame and then request the window to repaint
101 * after [callback] has finished (creating the animation). 176 * after [callback] has finished (creating the animation).
102 * 177 *
103 * Use this method only if you need to later call [cancelAnimationFrame]. If 178 * Use this method only if you need to later call [cancelAnimationFrame]. If
104 * not, the preferred Dart idiom is to set animation frames by calling 179 * not, the preferred Dart idiom is to set animation frames by calling
105 * [animationFrame], which returns a Future. 180 * [animationFrame], which returns a Future.
106 * 181 *
107 * Returns a non-zero valued integer to represent the request id for this 182 * Returns a non-zero valued integer to represent the request id for this
108 * request. This value only needs to be saved if you intend to call 183 * request. This value only needs to be saved if you intend to call
109 * [cancelAnimationFrame] so you can specify the particular animation to 184 * [cancelAnimationFrame] so you can specify the particular animation to
110 * cancel. 185 * cancel.
111 * 186 *
112 * Note: The supplied [callback] needs to call [requestAnimationFrame] again 187 * Note: The supplied [callback] needs to call [requestAnimationFrame] again
113 * for the animation to continue. 188 * for the animation to continue.
114 */ 189 */
115 @DomName('Window.requestAnimationFrame') 190 @DomName('Window.requestAnimationFrame')
116 int requestAnimationFrame(FrameRequestCallback callback) { 191 int requestAnimationFrame(FrameRequestCallback callback) {
117 _ensureRequestAnimationFrame(); 192 _ensureRequestAnimationFrame();
118 return _requestAnimationFrame(_wrapZone/*<dynamic, num>*/(callback)); 193 if (identical(Zone.current, Zone.ROOT)) {
194 return _requestAnimationFrame(callback);
195 }
196 var spec = new AnimationFrameRequestSpecification(
197 this, callback, _removeTaskMapping);
198 var task = Zone.current.createTask/*<AnimationFrameTask>*/(
199 _createAnimationFrameTask, spec);
200 _animationFrameTasks[task.id] = task;
201 return task.id;
202 }
203
204 static _AnimationFrameTask _createAnimationFrameTask(
205 AnimationFrameRequestSpecification spec, Zone zone) {
206 var task;
207 var id = spec.window._requestAnimationFrame((num time) {
208 spec.removeMapping(task.id);
209 zone.runTask(_runAnimationFrame, task, time);
210 });
211 var callback = zone.registerUnaryCallback(spec.callback);
212 task = new _AnimationFrameTask(id, zone, callback);
213 return task;
214 }
215
216 static void _removeTaskMapping(int id) {
217 _animationFrameTasks.remove(id);
218 }
219
220 static void _runAnimationFrame(_AnimationFrameTask task, num time) {
221 task._callback(time);
119 } 222 }
120 223
121 /** 224 /**
122 * Cancels an animation frame request. 225 * Cancels an animation frame request.
123 * 226 *
124 * ## Other resources 227 * ## Other resources
125 * 228 *
126 * * [Window.cancelAnimationFrame](https://developer.mozilla.org/en-US/docs/We b/API/Window.cancelAnimationFrame) 229 * * [Window.cancelAnimationFrame](https://developer.mozilla.org/en-US/docs/We b/API/Window.cancelAnimationFrame)
127 * from MDN. 230 * from MDN.
128 */ 231 */
129 void cancelAnimationFrame(int id) { 232 void cancelAnimationFrame(int id) {
130 _ensureRequestAnimationFrame(); 233 _ensureRequestAnimationFrame();
131 _cancelAnimationFrame(id); 234 var task = _animationFrameTasks[id];
Lasse Reichstein Nielsen 2016/06/07 12:57:35 Just do _animationFrameTasks.remove(id). That also
floitsch 2016/06/10 15:45:29 Done.
235 if (task == null) {
236 _cancelAnimationFrame(id);
Lasse Reichstein Nielsen 2016/06/07 12:57:34 and return? (otherwise you should reach `task.canc
floitsch 2016/06/10 15:45:29 Done.
237 }
238 _animationFrameTasks.remove(id);
239 task.cancel(this);
132 } 240 }
133 241
134 @JSName('requestAnimationFrame') 242 @JSName('requestAnimationFrame')
135 int _requestAnimationFrame(FrameRequestCallback callback) native; 243 int _requestAnimationFrame(FrameRequestCallback callback) native;
136 244
137 @JSName('cancelAnimationFrame') 245 @JSName('cancelAnimationFrame')
138 void _cancelAnimationFrame(int id) native; 246 void _cancelAnimationFrame(int id) native;
139 247
140 _ensureRequestAnimationFrame() { 248 _ensureRequestAnimationFrame() {
141 if (JS('bool', 249 if (JS('bool',
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 {bool useCapture: false}) { 483 {bool useCapture: false}) {
376 $if DART2JS 484 $if DART2JS
377 // Specify the generic type for _ElementEventStreamImpl only in dart2js to 485 // Specify the generic type for _ElementEventStreamImpl only in dart2js to
378 // avoid checked mode errors in dartium. 486 // avoid checked mode errors in dartium.
379 return new _ElementListEventStreamImpl<BeforeUnloadEvent>(e, _eventType, use Capture); 487 return new _ElementListEventStreamImpl<BeforeUnloadEvent>(e, _eventType, use Capture);
380 $else 488 $else
381 return new _ElementListEventStreamImpl(e, _eventType, useCapture); 489 return new _ElementListEventStreamImpl(e, _eventType, useCapture);
382 $endif 490 $endif
383 } 491 }
384 } 492 }
OLDNEW
« no previous file with comments | « tests/html/request_animation_task_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698