Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling of leading/trailing edges #283

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

Dema
Copy link

@Dema Dema commented Apr 27, 2023

Description

Fixes #37

Checklist for a new method

  • Create a directory for the new method in the src directory in param-case
  • Place the source code to src/method-name/index.ts in ESModules export in camelCase named export
  • Add tests to src/method-name/method-name.test.ts
  • Add fork tests to src/method-name/method-name.fork.test.ts
  • Add type tests to test-typings/method-name.ts
    • Use // @ts-expect-error to mark expected type error
    • import { expectType } from 'tsd' to check expected return type
  • Add documentation in src/method-name/readme.md
    • Add header Patronum/MethodName
    • Add section with overloads, if have
    • Add Motivation, Formulae, Arguments and Return sections for each overload
    • Add useful examples in Example section for each overload
  • Add section to README.md in the repository root
    • Add method to the table of contents into correct category - [MethodName](#methodname) - description.
    • Add section ## MethodName
    • Add [Method documentation & API](/src/method-name) into section
    • Add simple example

@stackblitz
Copy link

stackblitz bot commented Apr 27, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@github-actions
Copy link

github-actions bot commented Apr 27, 2023

🚛 size-compare report

Comparing 786f7452...786f7452

Files wasn't changed
File +/- Base Current +/- gzip Base gzip Current gzip
dist/and/index.cjs = 397 B 397 B = 251 B 251 B
dist/and/index.js = 413 B 413 B = 245 B 245 B
dist/babel-preset.cjs = 434 B 434 B = 249 B 249 B
dist/combine-events/index.cjs = 2.46 kB 2.46 kB = 841 B 841 B
dist/combine-events/index.js = 3.95 kB 3.95 kB = 1.35 kB 1.35 kB
dist/condition/index.cjs = 1.37 kB 1.37 kB = 505 B 505 B
dist/condition/index.js = 1.29 kB 1.29 kB = 461 B 461 B
dist/debounce/index.cjs = 3.99 kB 3.99 kB = 1.31 kB 1.31 kB
dist/debounce/index.js = 3.84 kB 3.84 kB = 1.29 kB 1.29 kB
dist/debug/index.cjs = 11.3 kB 11.3 kB = 3.16 kB 3.16 kB
dist/debug/index.js = 13.3 kB 13.3 kB = 3.79 kB 3.79 kB
dist/delay/index.cjs = 1.79 kB 1.79 kB = 727 B 727 B
dist/delay/index.js = 1.84 kB 1.84 kB = 719 B 719 B
dist/either/index.cjs = 600 B 600 B = 334 B 334 B
dist/either/index.js = 477 B 477 B = 270 B 270 B
dist/empty/index.cjs = 175 B 175 B = 151 B 151 B
dist/empty/index.js = 79 B 79 B = 89 B 89 B
dist/equals/index.cjs = 336 B 336 B = 249 B 249 B
dist/equals/index.js = 221 B 221 B = 179 B 179 B
dist/every/index.cjs = 1.22 kB 1.22 kB = 513 B 513 B
dist/every/index.js = 1.08 kB 1.08 kB = 442 B 442 B
dist/format/index.cjs = 642 B 642 B = 366 B 366 B
dist/format/index.js = 687 B 687 B = 368 B 368 B
dist/in-flight/index.cjs = 641 B 641 B = 357 B 357 B
dist/in-flight/index.js = 546 B 546 B = 305 B 305 B
dist/index.cjs = 1.57 kB 1.57 kB = 359 B 359 B
dist/index.js = 1.09 kB 1.09 kB = 279 B 279 B
dist/interval/index.cjs = 3.88 kB 3.88 kB = 1.14 kB 1.14 kB
dist/interval/index.js = 3.74 kB 3.74 kB = 1.12 kB 1.12 kB
dist/macro.cjs = 1.91 kB 1.91 kB = 808 B 808 B
dist/not/index.cjs = 161 B 161 B = 148 B 148 B
dist/not/index.js = 69 B 69 B = 81 B 81 B
dist/or/index.cjs = 393 B 393 B = 249 B 249 B
dist/or/index.js = 411 B 411 B = 245 B 245 B
dist/patronum.cjs = 18.8 kB 18.8 kB = 5.97 kB 5.97 kB
dist/patronum.js = 17.8 kB 17.8 kB = 6.04 kB 6.04 kB
dist/patronum.umd.js = 19.9 kB 19.9 kB = 6.07 kB 6.07 kB
dist/pending/index.cjs = 909 B 909 B = 495 B 495 B
dist/pending/index.js = 828 B 828 B = 444 B 444 B
dist/reset/index.cjs = 526 B 526 B = 312 B 312 B
dist/reset/index.js = 439 B 439 B = 256 B 256 B
dist/reshape/index.cjs = 419 B 419 B = 242 B 242 B
dist/reshape/index.js = 379 B 379 B = 201 B 201 B
dist/snapshot/index.cjs = 763 B 763 B = 350 B 350 B
dist/snapshot/index.js = 648 B 648 B = 292 B 292 B
dist/some/index.cjs = 1.16 kB 1.16 kB = 474 B 474 B
dist/some/index.js = 1.02 kB 1.02 kB = 407 B 407 B
dist/split-map/index.cjs = 628 B 628 B = 359 B 359 B
dist/split-map/index.js = 575 B 575 B = 318 B 318 B
dist/spread/index.cjs = 1.26 kB 1.26 kB = 534 B 534 B
dist/spread/index.js = 1.28 kB 1.28 kB = 516 B 516 B
dist/status/index.cjs = 426 B 426 B = 265 B 265 B
dist/status/index.js = 339 B 339 B = 208 B 208 B
dist/throttle/index.cjs = 2.1 kB 2.1 kB = 814 B 814 B
dist/throttle/index.js = 1.99 kB 1.99 kB = 775 B 775 B
dist/time/index.cjs = 719 B 719 B = 376 B 376 B
dist/time/index.js = 621 B 621 B = 323 B 323 B

@AlexandrHoroshih
Copy link
Member


const triggerTick = createEvent<T>();

const $leading = toStoreBoolean(leading, '$leading', false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In lodash both leading and trailing are true by default. Why not make the same?

Copy link
Author

@Dema Dema Apr 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to change the default behavior making it breaking change, but I agree, I'd rather made them both true by default

const $leading = toStoreBoolean(leading, '$leading', false);
const $trailing = toStoreBoolean(trailing, '$trailing', true);

const $neverCalled = createStore(true).on(target, () => false);
Copy link

@Toleckk Toleckk May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should be reset every time when timeout finishes, I guess

const trigger = createEvent()

const throttled = throttle({ source: trigger, timeout: 100, leading: true })

trigger(1)
trigger(2)
trigger(3)
await wait(110)
trigger(4)

throttled should be called with 4 immediately

@sergeysova sergeysova changed the title feat(throttle): add handling of leading/trailing edges similar to lodash. Fixes #37 Add handling of leading/trailing edges May 25, 2023
@sergeysova sergeysova added enhancement Improvement in existing feature feature New functionality labels May 25, 2023

const triggerTick = createEvent<T>();

const $leading = toStoreBoolean(leading, '$leading', false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const $leading = toStoreBoolean(leading, '$leading', false);
const $leading = toStoreBoolean(leading ?? false, '$leading');


const triggerTick = createEvent<T>();

const $leading = toStoreBoolean(leading, '$leading', false);
const $trailing = toStoreBoolean(trailing, '$trailing', true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const $trailing = toStoreBoolean(trailing, '$trailing', true);
const $trailing = toStoreBoolean(trailing ?? true, '$trailing');

Comment on lines +122 to +133
function toStoreBoolean(
value: boolean | Store<boolean> | undefined,
name: string,
defaultValue: boolean,
): Store<boolean> {
if (is.store(value)) return value;
if (typeof value === 'boolean') {
return createStore(value, { name });
} else {
return createStore(defaultValue, { name });
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function toStoreBoolean(
value: boolean | Store<boolean> | undefined,
name: string,
defaultValue: boolean,
): Store<boolean> {
if (is.store(value)) return value;
if (typeof value === 'boolean') {
return createStore(value, { name });
} else {
return createStore(defaultValue, { name });
}
}
function toStoreBoolean(value: boolean | Store<boolean> | undefined, name: string): Store<boolean> {
if (is.store(value)) return value;
return createStore(value, { name });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement in existing feature feature New functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add leading and trailing for throttle like in lodash
5 participants