effector / effector/patronum

Add `leading` and `trailing` for `throttle` like in lodash

Open
#37 1 comment 3 reactions 0 assignees View on GitHub
good first issue RFC
Dominant language
TypeScript
Stars
312
Forks
48
PR merge metrics
No merged PRs in 30d

Description

Following [this comment](https://github.com/sergeysova/patronum/pull/31#issuecomment-643653720)

To have more _industry-like_ behaviour ([lodash](https://lodash.com/docs/4.17.15#throttle)), `throttle` should provide options to indicate whether `target` should be triggered on the _leading_ and/or _trailing_ edge of the timeout.

> `leading [= true]` (boolean): Specify triggering on the leading edge of the timeout
> `trailing [= true]` (boolean): Specify triggering on the trailing edge of the timeout

If `leading` and `trailing` options are `true`, `target` is triggered on the trailing edge of the timeout only if the throttled `source` is triggered more than once during the timeout.

If `timeout` is `0` and `leading` is `false`, `target` triggering is deferred until to the next tick, similar to `setTimeout` with a timeout of `0`.

---

```javascript
const trigger = createEvent()

// by default `leading` is `true` and `trailing` is `true`
const throttled = throttle({ source: trigger, timeout: 100 })

trigger(1)
```
`throttled` should be triggered immediately, one time

---

```javascript
const trigger = createEvent()

// by default `leading` is `true` and `trailing` is `true`
const throttled = throttle({ source: trigger, timeout: 100 })

trigger(1)
trigger(2)
trigger(3)
```
`throttled` should be triggered immediately with payload of `1` and second time after 100ms with payload of `3`

---

```javascript
const trigger = createEvent()
const throttled = throttle({ source: trigger, timeout: 100, leading: false })
trigger(1)
```
`throttled` should be triggered after 100ms, one time

---

```javascript
const trigger = createEvent()
const throttled = throttle({ source: trigger, timeout: 100, leading: false })
trigger(1)
trigger(2)
trigger(3)
```
`throttled` should be triggered after 100ms, one time, with payload of `3` (just like current behaviour)

---

```javascript
const trigger = createEvent()
const throttled = throttle({ source: trigger, timeout: 0, leading: false })
trigger(1)
```
`throttled` should be triggered on the next tick, one time (just like current behaviour)

---

```javascript
const trigger = createEvent()
const throttled = throttle({ source: trigger, timeout: 100, trailing: false })

trigger(1)
await wait(75)
trigger(2)
await wait(75)
trigger(3)
```

`throttled` should be triggered immediately with payload of `1` and second time after **150ms** with payload of `3`

---

With combination `leading: false, trailing: false` — `throttled` should _not be triggered_ at all

---

[Playground with lodash's throttle](https://share.effector.dev/GQiIn5GF)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.