Skip to content

Svelte doesn't subscribe to writables in :then blocks at the correct time #15211

Open
@ForrestFire0

Description

@ForrestFire0

Describe the bug

I have a variable d which extends Writable and also has a property which is a promise (.ready). If I await on .ready and have {$d} in the markdown, it doesn't subscribe and unsubscribe when I expect it to.

See repro (don't worry it's only 45 lines should be comprehendible), but general concept is:

I expect to see what happens the first time:

(console shown below)

WE ARE UNSUBSCRIBING to 0:battery
new datastream on channel0:battery
Function is about to resolve, soon we should now be subscribing to 0:battery
THE THEN BLOCK IS RENDERING
WE ARE SUBSCRIBING to 0:battery

However when the button is pressed, we see the following additional messages:

new datastream on channel1:battery
WE ARE UNSUBSCRIBING to 0:battery
THE THEN BLOCK IS RENDERING
WE ARE SUBSCRIBING to 1:battery
Function is about to resolve, soon we should now be subscribing to 1:battery
THE THEN BLOCK IS RENDERING

First, I expect the reactive function to kick off, which happens. The function datastream() is run with correct arguments.
Then I expect the function to return, d to be re-assigned, and the then block to deconstruct itself. Which it does and 0:battery is unsubscribed from.
However, it seems the then block is then ran before re-checking the d.ready promise. It seems it subscribes to the new $d.

Questions:

  • Shouldn't the then block not be re-rendered when d is replaced before checking d.ready?
  • Why is $d subscribed to before d.ready settles?
  • Why doesn't $d become re-subscribed to when the then block re-renders and "THE THEN BLOCK IS RENDERING" is printed for the 2nd time?
  • Also - I note that $d is subscribed to even when "THE D BLOCK IS VISIBLE" is not visible.

Reproduction

https://svelte.dev/playground/7568c659fdff4847aad9433ff8070bed?version=5.19.7

Logs

See above. But:

about:srcdoc:299 running Svelte compiler version 5.19.7
about:srcdoc:299 WE ARE UNSUBSCRIBING to 2:battery
about:srcdoc:299 new datastream on channel0:battery
about:srcdoc:299 Function is about to resolve, soon we should now be subscribing to 0:battery
about:srcdoc:299 THE THEN BLOCK IS RENDERING
about:srcdoc:299 WE ARE SUBSCRIBING to 0:battery
about:srcdoc:299 new datastream on channel1:battery
about:srcdoc:299 WE ARE UNSUBSCRIBING to 0:battery
about:srcdoc:299 THE THEN BLOCK IS RENDERING
about:srcdoc:299 WE ARE SUBSCRIBING to 1:battery
about:srcdoc:299 Function is about to resolve, soon we should now be subscribing to 1:battery
about:srcdoc:299 THE THEN BLOCK IS RENDERING

System Info

In the REPL!

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting submitterneeds a reproduction, or clarification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions