From 4ab6aaec868cff3055373db26945e4ffcbc61965 Mon Sep 17 00:00:00 2001 From: Coltin Kifer Date: Sun, 8 Sep 2024 04:05:37 +0000 Subject: [PATCH] refactor/test: convert setRafTimeout to ts, add tests --- .eslintrc | 26 +++++++++++++++++++++++++- src/setRafTimeout.js | 22 ---------------------- src/setRafTimeout.ts | 27 +++++++++++++++++++++++++++ test/setRafTimeout.spec.ts | 22 ++++++++++++++++++++++ tsconfig.json | 1 + 5 files changed, 75 insertions(+), 23 deletions(-) delete mode 100644 src/setRafTimeout.js create mode 100644 src/setRafTimeout.ts create mode 100644 test/setRafTimeout.spec.ts diff --git a/.eslintrc b/.eslintrc index b0f9732..f36fc9c 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,5 +1,11 @@ { - "extends": ["eslint-config-airbnb", "plugin:prettier/recommended", "prettier"], + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/recommended", + "eslint-config-airbnb", + "plugin:prettier/recommended", + "prettier" + ], "env": { "browser": true, "node": true, @@ -7,6 +13,13 @@ }, "parser": "@typescript-eslint/parser", "plugins": ["react", "@typescript-eslint"], + "settings": { + "import/resolver": { + "node": { + "extensions": [".js", ".jsx", ".ts", ".tsx"] + } + } + }, "rules": { "no-restricted-globals": [1, "isFinite"], "valid-jsdoc": [ @@ -16,6 +29,16 @@ "requireParamType": false } ], + "import/extensions": [ + "error", + "ignorePackages", + { + "js": "never", + "jsx": "never", + "ts": "never", + "tsx": "never" + } + ], "react/jsx-uses-react": 2, "react/jsx-uses-vars": 2, "react/react-in-jsx-scope": 2, @@ -25,6 +48,7 @@ "no-mixed-operators": "off", "no-plusplus": "off", "no-continue": "off", + "no-unused-vars": "off", "react/require-default-props": "off", "react/jsx-filename-extension": "off", "react/forbid-prop-types": "off", diff --git a/src/setRafTimeout.js b/src/setRafTimeout.js deleted file mode 100644 index d5dc766..0000000 --- a/src/setRafTimeout.js +++ /dev/null @@ -1,22 +0,0 @@ -function safeRequestAnimationFrame(callback) { - if (typeof requestAnimationFrame !== 'undefined') requestAnimationFrame(callback); -} - -export default function setRafTimeout(callback, timeout = 0) { - let currTime = -1; - - const shouldUpdate = now => { - if (currTime < 0) { - currTime = now; - } - - if (now - currTime > timeout) { - callback(now); - currTime = -1; - } else { - safeRequestAnimationFrame(shouldUpdate); - } - }; - - requestAnimationFrame(shouldUpdate); -} diff --git a/src/setRafTimeout.ts b/src/setRafTimeout.ts new file mode 100644 index 0000000..64d3953 --- /dev/null +++ b/src/setRafTimeout.ts @@ -0,0 +1,27 @@ +type CallbackType = (now: number) => void; + +/** + * Calls requestAnimationFrame with a timeout delay + * @param {Function} callback cb function + * @param {number} timeout timeout in ms + * @returns {void} void + */ +export default function setRafTimeout(callback: CallbackType, timeout: number = 0): void { + let currTime = -1; + + const shouldUpdate = (now: number) => { + if (currTime < 0) { + currTime = now; + } + + if (now - currTime > timeout) { + callback(now); + currTime = -1; + } else { + // requestAnimationFrame is availble across most browsers + requestAnimationFrame(shouldUpdate); + } + }; + + requestAnimationFrame(shouldUpdate); +} diff --git a/test/setRafTimeout.spec.ts b/test/setRafTimeout.spec.ts new file mode 100644 index 0000000..1410b74 --- /dev/null +++ b/test/setRafTimeout.spec.ts @@ -0,0 +1,22 @@ +import setRafTimeout from '../src/setRafTimeout'; + +describe('setRafTimeout', () => { + it('should call setRafTimeout', () => { + vi.useFakeTimers(); + const spy = vi.spyOn(window, 'requestAnimationFrame'); + const cb = vi.fn(); + setRafTimeout(cb); + vi.runAllTimers(); + expect(cb).toHaveBeenCalled(); + expect(spy).toHaveBeenCalled(); + }); + + it('should not call requestAnimationFrame with a 1 second delay', () => { + vi.useFakeTimers(); + const spy = vi.spyOn(window, 'requestAnimationFrame'); + const cb = vi.fn(); + setRafTimeout(cb, 1000); + expect(cb).not.toHaveBeenCalled(); + expect(spy).toHaveBeenCalled(); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 10f5ec3..9829e4d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,6 +15,7 @@ "sourceMap": true, "target": "es6", "types": ["vitest/globals"], + "lib": ["Dom"], "allowJs": true }, "include": ["./src/**/*"]