Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the second 'invalidate' argument to Readable.subscribe?

Tags:

svelte

The docs only say: invalidate cleanup callback. Not very informative. https://svelte.dev/docs/svelte-store#types-readable

Anyone got a little bit more info on this? Maybe a tiny example?

like image 522
Sámal Rasmussen Avatar asked Dec 14 '25 06:12

Sámal Rasmussen


1 Answers

There is an open issue about this.

I dug through the commit history and found that the function was added in this commit.

glitch-free reactive stores

The invalidate callback is used internally by derived to update a local pending state, which, from the looks of it, is used to prevent subscribers from being called until all dependencies have "settled".

The change was added between versions 3.0.0-alpha6 and 3.0.0-alpha7.

The test case added with the commit looks like this:

it('prevents glitches', () => {
    const lastname = writable('Jekyll');
    const firstname = derive(lastname, n => n === 'Jekyll' ? 'Henry' : 'Edward');

    const fullname = derive([firstname, lastname], names => names.join(' '));

    const values = [];

    const unsubscribe = fullname.subscribe(value => {
        values.push(value);
    });

    lastname.set('Hyde');

    assert.deepEqual(values, [
        'Henry Jekyll',
        'Edward Hyde'
    ]);

    unsubscribe();
});

Before the fix, the values array ends up as:

[
  'Henry Jekyll',
  'Edward Jekyll', // <- Should not be here
  'Edward Hyde',
]

Since the function is not documented, it probably is intended only for internal use.

like image 200
H.B. Avatar answered Dec 16 '25 22:12

H.B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!