Skip to content

fix(blaze): await async bindings when expanding views in HTML attributes - #537

Merged
nachocodoner merged 4 commits into
meteor:release-3.1.0from
dupontbertrand:fix/async-expand-view
Nov 12, 2026
Merged

nachocodoner merged 4 commits into
meteor:release-3.1.0from
dupontbertrand:fix/async-expand-view

Conversation

@dupontbertrand

@dupontbertrand dupontbertrand commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Rebased on release-3.1.0 after #534: with the attribute views awaiting their bindings, an error thrown while reading the resolved data rejects the attribute promise chain instead of surfacing through the updater computation, so it relies on the single-report fix of #534 to be reported once. The last commit covers that case.

Summary

Fixes #470.

When a built-in block helper (#if, #unless, #with, #each) inside an HTML attribute (e.g. <div class="base {{#if myProp}}active{{/if}}">) evaluated an async helper or an async template argument ({{> Child myProp=asyncHelper}}), Blaze._expandView ran view._render() synchronously before the binding's Promise settled.

Because view._render() read undefined from the binding's ReactiveVar inside the attribute updater's computation (updaterComputation) and Blaze._expandView registered Tracker.onInvalidate(() => Blaze._destroyView(view)), the Promise settling on the next microtask invalidated updaterComputation, immediately destroying the subview and creating a fresh one with an unresolved binding on every flush (leaving the attribute stuck and re-running the helper in an infinite loop).

Change

Background. Blaze renders a view in two ways. The usual way materializes it: the view gets a render computation of its own, so a helper that returns a Promise can update the DOM once the Promise settles. The other way expands it to plain htmljs, with no computation of its own: that is what Blaze.toHTML does, and what the attribute updater does for an attribute value that contains a block helper, like class="base {{#if myProp}}active{{/if}}". The attribute updater is an autorun (updaterComputation in materializer.js): on every run it expands the views of the attribute and writes the resulting strings on the element.

The bug. When the #if inside the attribute got a Promise, the expanded view rendered at once with its binding still pending, so the attribute was computed from undefined. When the Promise settled, the binding's ReactiveVar changed and invalidated the updater. The updater destroys and re-creates its expanded views on every run, so the new #if called the helper again, got a new pending Promise, and the cycle started over: the attribute never received its value and the helper ran forever.

The fix, step by step.

  1. The updater asks for the wait. materializer.js now calls Blaze._expandAttributes(rawAttrs, parentView, true /*awaitBindings*/). The flag travels with the expander (Blaze._HTMLJSExpander) down to every view found in the attribute, nested ones included ({{#if a}}{{#if b}}…), through Blaze._expand and Blaze._expandView.

  2. The view collects its pending bindings. When a view is expanded with the flag, Blaze._expandView marks it (view._awaitBindings) and remembers the updater's computation (view._expandComputation) before the created callbacks run. In those callbacks, _createBinding (behind #if, #unless, #with, #let, #each and template arguments) stores on view._bindingPromises the Promise of each binding that returned one. _setBindingValue now returns that Promise, which settles once the value is written in the ReactiveVar.

  3. The render waits for them. If the view has pending bindings, Blaze._expandView returns a Promise: it waits for all of them, then renders inside Tracker.withComputation(updaterComputation, …), so the reactive reads made during the render still register as dependencies of the updater, exactly as a synchronous render would. The promises left in the result by nested views are unwrapped (waitForExpanded) before the result reaches waitForAllAttributes, which already knew how to await attribute values.

  4. A later change re-expands instead of re-rendering. An expanded view cannot re-render itself. So when a binding's autorun re-runs and returns a new Promise, it invalidates the updater, which drops the view and expands a fresh one that waits again. A binding that returns a plain value needs nothing special: the updater already depends on its ReactiveVar and re-runs only when the value changes, as in Blaze 3.0.

  5. #each waits before observing. Blaze.Each sets up ObserveSequence.observe only once its sequence binding has settled, so the items (or the {{else}} block) come from the real sequence, not from undefined.

  6. A replaced run does not write. A run of the updater can be invalidated while its attribute Promises are still pending: its bindings re-ran, or the element was torn down. The updater records that with Tracker.onInvalidate inside the run and skips the DOM write when the result settles, so a slower Promise from an earlier run never overwrites the result of the run that replaced it.

What does not change. Without the flag, Blaze._expandView behaves as before: Blaze.toHTML, Blaze.toHTMLWithData and Blaze._toText render synchronously, a pending binding renders as an empty block, and the result is never a Promise. The materialized path (Blaze.render) is not touched: its views were never expanded.

Tests. packages/spacebars-tests/async_tests.{html,js}: 18 tests covering #if, #unless, #let, #with, #each..in, template inclusion with an async keyword argument ({{> child x=asyncHelper}}), a reactive update without an infinite loop, a binding that settles after a macrotask, two overlapping updates whose promises settle in reverse order (and a synchronous value replacing a pending promise), and Blaze.toHTML with a pending #if, #each and attribute block.

Verification

Full headless Tinytest suite in test-app, without jQuery (native DOM backend) and with --extra-packages=jquery:

  • Native DOM mode: 461 / 0 / 0 / 461 passed
  • jQuery mode: 465 / 0 / 0 / 465 passed

The three Blaze.toHTML tests fail on the previous commit of this branch with Unexpected object in htmljs in toHTML: [object Promise], and pass with the scoped waiting.

Changelog

@dupontbertrand
dupontbertrand marked this pull request as draft November 11, 2026 01:48
@dupontbertrand dupontbertrand mentioned this pull request Nov 11, 2026
31 of 33 tasks
@dupontbertrand
dupontbertrand marked this pull request as ready for review November 11, 2026 10:51
@jankapunkt jankapunkt added this to the 3.1 milestone Nov 11, 2026
… bindings

Scope the waiting for async bindings to the expansion driven by the
attribute updater of the materializer, the only caller that can await the
settled values (meteor#470). `Blaze.toHTML`, `Blaze.toHTMLWithData` and
`Blaze._toText` render synchronously as before, with a pending binding
rendered as an empty block, instead of leaving a `Promise` in the htmljs
that `HTML.toHTML` rejects with "Unexpected object in htmljs".

A binding re-run invalidates the updater only when it returned a
`Promise`; a synchronous binding keeps re-expanding only when its value
changes, as in 3.0.

Tests: a binding that settles after a macrotask, and `Blaze.toHTML` with a
pending `#if`, `#each` and attribute block.
…k once

With the attribute views awaiting their bindings, an error thrown while
the resolved data is read, for example by a getter, no longer surfaces
through the updater computation: it rejects the attribute promise chain.
Cover that path with the single-report tests of the attribute chain, so
the chain reports it once. This needs the fix of the last `then()` from
the previous commits.
Comment thread packages/blaze/view.js Resolved
…hile pending

A run of the attribute updater whose attribute views await async bindings
settles later. When a binding re-ran meanwhile, the updater was invalidated
and re-run, but the older run's chain still reached `attrUpdater.update()`
once its promises settled, writing the result of a destroyed view over the
newer one (review of meteor#537).

The run records its own invalidation with `Tracker.onInvalidate` and skips
the write. Regression: two overlapping updates whose promises settle in
reverse order, and a synchronous value replacing a pending promise.
@nachocodoner
nachocodoner merged commit 667e6d5 into meteor:release-3.1.0 Nov 12, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants