Skip to content

Debounce not bound to state => may yield surprising results #6

@kju2

Description

@kju2

Hi! I really like the simplicity and expressiveness of Svelte-FSM.

While implementing a FSM I tripped over the fact that debounce calls are not bound to the event where they were called from.

<script>
	import fsm from "svelte-fsm";

	const switchWithDelayedAction = fsm("off", {
		off: {
			click: "on",
			doStuff() {
				console.log("Surprise!");
			},
		},
		on: {
			_enter() {
				this.doStuff.debounce(1500);
			},
			click: "off",
			doStuff() {
				console.log("Do stuff in ON state.");
			},
		},
	});
$: console.log("state: ", $switchWithDelayedAction);
</script>

<button on:click={switchWithDelayedAction.click}>Click me</button>

https://svelte.dev/repl/56d96df27fea4d68a179084edc22a3ab?version=3.46.4

If I double click on the button (second click less than 1500 ms after the first 🙂 ), then "doStuff" is called in the "off" state. I expected the "doStuff" method in the "on" state to be called.

A workaround is to call "this.doStuff.debounce(null)" in the "_exit" method of the "on" state. (https://svelte.dev/repl/1b041923ec1e48258f8858c1fd597f46?version=3.46.4)

I see two "simple" solutions (hopefully there are better ones out there 🙂)

  • Document this behaviour and keep the implementation simple.
  • Clear all timers of the state when exiting the state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions