Skip to content

Instantly share code, notes, and snippets.

@lokhmakov
Last active October 25, 2022 08:37
Show Gist options
  • Save lokhmakov/e6aab83b1d1b0561a1bd0350cf8ec84c to your computer and use it in GitHub Desktop.
Save lokhmakov/e6aab83b1d1b0561a1bd0350cf8ec84c to your computer and use it in GitHub Desktop.
effector - run effect once through event
const runQuery = createEffect({
handler(props) {
console.log(props)
},
})
const createOnceEvent = to => {
const from = createEvent()
const unsubscribe = forward({from, to})
from.watch(unsubscribe)
return from
}
const createOnceEffect = handler => {
const trigger = createEffect({handler})
trigger.watch(() => trigger.use(() => {}))
return trigger
}
const runQueryOnce = createOnceEffect(runQuery)
runQueryOnce(1)
runQueryOnce(2)
runQueryOnce(3)
const doQueryOnce = createOnceEvent(runQuery)
doQueryOnce(1)
doQueryOnce(2)
doQueryOnce(3)
@lokhmakov
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment