From f881944037453763e7b23ac9ab9997eeb64f16bb Mon Sep 17 00:00:00 2001 From: Agam Rafaeli Date: Sat, 8 Apr 2017 15:42:03 +0300 Subject: [PATCH 1/2] Move to use lodash's implementation of throttle --- src/Tabs/Tabs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tabs/Tabs.js b/src/Tabs/Tabs.js index 43dde8422f9ff1..70154be007c05b 100644 --- a/src/Tabs/Tabs.js +++ b/src/Tabs/Tabs.js @@ -4,8 +4,8 @@ import React, { Component, PropTypes, Children, cloneElement } from 'react'; import classNames from 'classnames'; import { createStyleSheet } from 'jss-theme-reactor'; import EventListener from 'react-event-listener'; +import throttle from 'lodash/throttle'; import customPropTypes from '../utils/customPropTypes'; -import { throttle } from '../utils/helpers'; import TabIndicator from './TabIndicator'; export const styleSheet = createStyleSheet('MuiTabs', () => { From ac79f8f6568e7d1fbd737206d621373121d98bb8 Mon Sep 17 00:00:00 2001 From: Agam Rafaeli Date: Sat, 8 Apr 2017 15:42:47 +0300 Subject: [PATCH 2/2] Remove throttle implementation in utils The only usage of throttle was moved to use lodash's implementation --- src/utils/helpers.js | 14 ---------- src/utils/helpers.spec.js | 58 --------------------------------------- 2 files changed, 72 deletions(-) diff --git a/src/utils/helpers.js b/src/utils/helpers.js index e1c2b2258fdf47..c465567026495d 100644 --- a/src/utils/helpers.js +++ b/src/utils/helpers.js @@ -34,20 +34,6 @@ export function find(arr, pred) { return index > -1 ? arr[index] : undefined; } -export function throttle(fn, limit) { - let wait = false; - return function throttledFn() { - if (!wait) { - fn.call(); - wait = true; - return setTimeout(() => { - wait = false; - }, limit); - } - return null; - }; -} - /** * Safe chained function * diff --git a/src/utils/helpers.spec.js b/src/utils/helpers.spec.js index e74e483f20caa3..6b9661b9ccaafc 100644 --- a/src/utils/helpers.spec.js +++ b/src/utils/helpers.spec.js @@ -1,12 +1,10 @@ // @flow weak import { assert } from 'chai'; -import { spy, useFakeTimers } from 'sinon'; import { transform, contains, find, - throttle, } from './helpers'; describe('utils//helpers.js', () => { @@ -55,60 +53,4 @@ describe('utils//helpers.js', () => { assert.strictEqual(contains(obj, failPred), false); }); }); - - describe('throttle(fn, limit)', () => { - let functionSpy; - let limit; - let throttledFunc; - - before(() => { - functionSpy = spy(); - limit = 2; - throttledFunc = throttle(functionSpy, limit); - }); - - it('should return a function', () => { - assert.strictEqual(typeof throttledFunc, 'function'); - }); - - it('should not call the given function', () => { - assert.strictEqual(functionSpy.callCount, 0); - }); - - describe('returned function', () => { - let clock; - - before(() => { - functionSpy.reset(); - throttledFunc = throttle(functionSpy, limit); - clock = useFakeTimers(); - throttledFunc(); - }); - - after(() => { - clock.restore(); - }); - - it('should call given function', () => { - assert.strictEqual(functionSpy.callCount, 1); - }); - - it('should not call given function if called again', () => { - throttledFunc(); - assert.strictEqual(functionSpy.callCount, 1); - }); - - it('should not call given function even if clock ticks', () => { - clock.tick(1); - throttledFunc(); - assert.strictEqual(functionSpy.callCount, 1); - }); - - it('should call given function when clock ticks past limit', () => { - clock.tick(1); - throttledFunc(); - assert.strictEqual(functionSpy.callCount, 2); - }); - }); - }); });