Skip to content

Commit

Permalink
🐛 Assert that values are never returned from vsync callbacks (#20836)
Browse files Browse the repository at this point in the history
* Assert that values are never returned from vsync callbacks

Vsync does not propogate those values in any way. This is specifically targetting code like:

```js
vsync.measure(() => {
  return el.getBoundingClientRect();
});

// or
vsync.mutatePromise(() => {
  return someOtherPromise;
});
```

* Fix dev call

* Lint

* Don't use devAssert so this stays in production

* Type checks
  • Loading branch information
jridgewell authored Feb 14, 2019
1 parent fe27d76 commit 35d6030
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
4 changes: 3 additions & 1 deletion extensions/amp-embedly-card/0.1/amp-embedly-card-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ export class AmpEmbedlyCard extends AMP.BaseElement {
}, opt_is3P);

this.applyFillContent(iframe);
this.getVsync().mutate(() => this.element.appendChild(iframe));
this.getVsync().mutate(() => {
this.element.appendChild(iframe);
});

this.iframe_ = iframe;

Expand Down
1 change: 1 addition & 0 deletions extensions/amp-subscriptions/0.1/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export class Dialog {
setImportantStyles(this.wrapper_, {
transform: 'translateY(100%)',
});
}).then(() => {
return this.timer_.promise(300);
}).then(() => {
return this.vsync_.mutatePromise(() => {
Expand Down
18 changes: 11 additions & 7 deletions src/service/vsync-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ let VsyncStateDef;

/**
* @typedef {{
* measure: (function(!VsyncStateDef)|undefined),
* mutate: (function(!VsyncStateDef)|undefined)
* measure: (function(!VsyncStateDef):undefined|undefined),
* mutate: (function(!VsyncStateDef):undefined|undefined)
* }}
*/
let VsyncTaskSpecDef;
Expand Down Expand Up @@ -208,7 +208,7 @@ export class Vsync {

/**
* Runs the mutate operation via vsync.
* @param {function()} mutator
* @param {function():undefined} mutator
*/
mutate(mutator) {
this.run({
Expand All @@ -219,7 +219,7 @@ export class Vsync {

/**
* Runs `mutate` wrapped in a promise.
* @param {function()} mutator
* @param {function():undefined} mutator
* @return {!Promise}
*/
mutatePromise(mutator) {
Expand All @@ -231,7 +231,7 @@ export class Vsync {

/**
* Runs the measure operation via vsync.
* @param {function()} measurer
* @param {function():undefined} measurer
*/
measure(measurer) {
this.run({
Expand Down Expand Up @@ -449,13 +449,17 @@ export class Vsync {

/**
* For optimization reasons to stop try/catch from blocking optimization.
* @param {function(!VsyncStateDef)|undefined} callback
* @param {function(!VsyncStateDef):undefined|undefined} callback
* @param {!VsyncStateDef} state
*/
function callTaskNoInline(callback, state) {
devAssert(callback);
try {
callback(state);
const ret = callback(state);
if (ret !== undefined) {
dev().error('VSYNC', 'callback returned a value but vsync cannot ' +
'propogate it: %s', callback.toString());
}
} catch (e) {
rethrowAsync(e);
return false;
Expand Down

0 comments on commit 35d6030

Please sign in to comment.