diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 48f69210df..65b429e9f2 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -4554,9 +4554,6 @@ source.addEventListener('message', <_.LoDashImplicitObjectWrapper>_(fu 'maxWait': 1000 }), false); -var returnedDebounce = _.throttle(function (a: any) { return a * 5; }, 5); -returnedThrottled(4); - // _.defer module TestDefer { type SampleFunc = (a: number, b: string) => boolean; @@ -4671,9 +4668,6 @@ result = _.memoize(testMemoizeFn, te result = (_(testMemoizeFn).memoize().value()); result = (_(testMemoizeFn).memoize(testMemoizeResolverFn).value()); -var returnedMemoize = _.throttle(function (a: any) { return a * 5; }, 5); -returnedMemoize(4); - // _.modArgs module TestModArgs { type Func1 = (a: boolean) => boolean; @@ -4768,9 +4762,6 @@ module TestOnce { } } -var returnedOnce = _.throttle(function (a: any) { return a * 5; }, 5); -returnedOnce(4); - var greetPartial = function (greeting: string, name: string) { return greeting + ' ' + name; }; var hi = _.partial(greetPartial, 'hi'); hi('moe'); @@ -4835,15 +4826,49 @@ interface TestSpreadResultFn { result = (_.spread(testSpreadFn))(['fred', 'hello']); result = (_(testSpreadFn).spread().value())(['fred', 'hello']); -var throttled = _.throttle(function () { }, 100); -jQuery(window).on('scroll', throttled); +// _.throttle +module TestThrottle { + interface SampleFunc { + (n: number, s: string): boolean; + } -jQuery('.interactive').on('click', _.throttle(function () { }, 300000, { - 'trailing': false -})); + interface Options { + leading?: boolean; + trailing?: boolean; + } -var returnedThrottled = _.throttle(function (a: any) { return a * 5; }, 5); -returnedThrottled(4); + interface ResultFunc { + (n: number, s: string): boolean; + cancel(): void; + } + + let func: SampleFunc; + let options: Options; + + { + let result: ResultFunc; + + result = _.throttle(func); + result = _.throttle(func, 42); + result = _.throttle(func, 42, options); + } + + { + let result: _.LoDashImplicitObjectWrapper; + + result = _(func).throttle(); + result = _(func).throttle(42); + result = _(func).throttle(42, options); + } + + { + let result: _.LoDashExplicitObjectWrapper; + + result = _(func).chain().throttle(); + result = _(func).chain().throttle(42); + result = _(func).chain().throttle(42, options); + } +} var helloWrap = function (name: string) { return 'hello ' + name; }; var helloWrap2 = _.wrap(helloWrap, function (func) { diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 49de3691e2..91d7d410c5 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -8444,41 +8444,62 @@ declare module _ { //_.throttle - interface LoDashStatic { - /** - * Creates a function that, when executed, will only call the func function at most once per - * every wait milliseconds. Provide an options object to indicate that func should be invoked - * on the leading and/or trailing edge of the wait timeout. Subsequent calls to the throttled - * function will return the result of the last func call. - * - * Note: If leading and trailing options are true func will be called on the trailing edge of - * the timeout only if the the throttled function is invoked more than once during the wait timeout. - * @param func The function to throttle. - * @param wait The number of milliseconds to throttle executions to. - * @param options The options object. - * @param options.leading Specify execution on the leading edge of the timeout. - * @param options.trailing Specify execution on the trailing edge of the timeout. - * @return The new throttled function. - **/ - throttle( - func: T, - wait: number, - options?: ThrottleSettings): T; - } - interface ThrottleSettings { - /** - * If you'd like to disable the leading-edge call, pass this as false. - **/ + * If you'd like to disable the leading-edge call, pass this as false. + */ leading?: boolean; /** - * If you'd like to disable the execution on the trailing-edge, pass false. - **/ + * If you'd like to disable the execution on the trailing-edge, pass false. + */ trailing?: boolean; } + interface LoDashStatic { + /** + * Creates a throttled function that only invokes func at most once per every wait milliseconds. The throttled + * function comes with a cancel method to cancel delayed invocations. Provide an options object to indicate + * that func should be invoked on the leading and/or trailing edge of the wait timeout. Subsequent calls to + * the throttled function return the result of the last func call. + * + * Note: If leading and trailing options are true, func is invoked on the trailing edge of the timeout only if + * the the throttled function is invoked more than once during the wait timeout. + * + * @param func The function to throttle. + * @param wait The number of milliseconds to throttle invocations to. + * @param options The options object. + * @param options.leading Specify invoking on the leading edge of the timeout. + * @param options.trailing Specify invoking on the trailing edge of the timeout. + * @return Returns the new throttled function. + */ + throttle( + func: T, + wait?: number, + options?: ThrottleSettings + ): T & Cancelable; + } + + interface LoDashImplicitObjectWrapper { + /** + * @see _.throttle + */ + throttle( + wait?: number, + options?: ThrottleSettings + ): LoDashImplicitObjectWrapper; + } + + interface LoDashExplicitObjectWrapper { + /** + * @see _.throttle + */ + throttle( + wait?: number, + options?: ThrottleSettings + ): LoDashExplicitObjectWrapper; + } + //_.wrap interface LoDashStatic { /** @@ -13221,6 +13242,10 @@ declare module _ { interface StringRepresentable { toString(): string; } + + interface Cancelable { + cancel(): void; + } } declare module "lodash" {