Skip to content

Support Higher Order Stores with runes #9651

@mrh1997

Description

@mrh1997

Describe the problem

In Svelte 4 Higher Order stores are supported. This is a great feature that allows to separation of concerns for stores.

But in svelte 5 I see now way to accomplish this feature:

The following modification of the svelte 5 tutorial shows the problem.

<script>
	let count = $state(0);
	function increment() {
		count += 1;
	}

        // this would be a very generic function which could be reused with any store (if higher order stores would word):
	function delayed(baseStore, delayLen) {
          let resultStore = $state(baseStore);
	  const buffer = []
	  $effect(() => {
		  buffer.push(count);
		  if (buffer.length > delayLen) resultStore = buffer.shift()
	  })
          return {
             get val() { return resultStore; }
          }
	}

	let delayedCount = delayed(count, 5);
</script>

<button on:click={increment}>
	clicks: {delayedCount.val}
</button>

Describe the proposed solution

When passing a rune/signal as parameter I would intuitively expect, that it is handled as if the parameter is also a rune/signal WITHIN the function.

The example given above would then work out of the box.

Alternatives considered

If automatic detection of runes and passing them into function is hard, this could be done with an explicit wrapper like:

let delayedCount = delayed($use(count), 5)

But this is much more unintuitive and probably error prone, as devs will forget to add the $use

Importance

i cannot use svelte without it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions