I am looking to create a signal that will automatically switch to an underlying signal or memo after a specific delay, and reset immediately if the primary signal is cleared. The code snippet below illustrates my desired functionality.
import { render } from "solid-js/web";
import { For, createSignal, createEffect } from "solid-js";
function Counter() {
const [s, setS] = createSignal(undefined)
const [delayedS, delayedSetS] = createSignal(undefined)
setTimeout(() => {
setS(10)
}, 1000)
setTimeout(() => {
setS(undefined)
}, 3000)
createEffect(() => {
let res = s()
if (res !== undefined) {
setTimeout(() => delayedSetS(res), 1000)
} else {
delayedSetS(undefined)
}
})
return (<>
<span> S {s()} </span>
<span> Delayed S {delayedS()}</span>
</>)
}
render(() => <Counter />, document.getElementById("app"));
The above implementation works fine, but I'm wondering if this is the best approach. I haven't explored using createDeferred for this function as it involves a scheduler that I'm not familiar with.