-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: dont spam IPFS_INIT_FAILED events to countly (#2133)
* fix: dont spam IPFS_INIT_FAILED events to countly * fix(hof/functions): jsdoc types are valid * fix(hofs/functions): types and add debounce function * chore: cleanup code * fix: typecheck see https://github.com/ipfs/ipfs-webui/actions/runs/5418218369/jobs/9850182385\?pr\=2133 * test(hofs/functions): add tests * fix: typecheck succeeds * feat: fn first hofs, add param guards * chore: wrap check for limited addEvent
- Loading branch information
Showing
8 changed files
with
385 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* | ||
* @param {any} value | ||
* @param {boolean} [throwOnFalse] | ||
* @returns {value is Function} | ||
*/ | ||
export function isFunction (value, throwOnFalse = true) { | ||
if (typeof value === 'function') { return true } | ||
if (throwOnFalse) { throw new TypeError('Expected a function') } | ||
return false | ||
} | ||
|
||
/** | ||
* | ||
* @param {any} value | ||
* @param {boolean} [throwOnFalse] | ||
* @returns {value is number} | ||
*/ | ||
export function isNumber (value, throwOnFalse = true) { | ||
if (typeof value === 'number') { return true } | ||
if (throwOnFalse) { throw new TypeError('Expected a number') } | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { isFunction, isNumber } from './guards.js' | ||
|
||
describe('lib/guards', function () { | ||
describe('isFunction', function () { | ||
it('should return true if the passed value is a function', function () { | ||
expect(isFunction(() => {})).toBe(true) | ||
}) | ||
|
||
it('should throw an error if the passed value is not a function', function () { | ||
expect(() => isFunction('not a function')).toThrow(TypeError) | ||
}) | ||
|
||
it('should return false if the passed value is not a function and throwOnFalse is false', function () { | ||
expect(isFunction('not a function', false)).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('isNumber', function () { | ||
it('should return true if the passed value is a function', function () { | ||
expect(isNumber(1)).toBe(true) | ||
}) | ||
|
||
it('should throw an error if the passed value is not a function', function () { | ||
expect(() => isNumber('not a number')).toThrow(TypeError) | ||
}) | ||
|
||
it('should return false if the passed value is not a function and throwOnFalse is false', function () { | ||
expect(isNumber('not a number', false)).toBe(false) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { isFunction, isNumber } from '../guards.js' | ||
|
||
/** | ||
* This method creates a function that invokes func once it’s called n or more times. | ||
* @see https://youmightnotneed.com/lodash#after | ||
* @template A | ||
* @template R | ||
* @param {number} times | ||
* @param {(...args: A[]) => R} fn | ||
* @returns {(...args: A[]) => void | R} | ||
*/ | ||
export const after = (fn, times) => { | ||
isFunction(fn) && isNumber(times) | ||
let counter = 0 | ||
/** | ||
* @type {(...args: A[]) => void | R} | ||
*/ | ||
return (...args) => { | ||
counter++ | ||
if (counter >= times) { | ||
return fn(...args) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @see https://youmightnotneed.com/lodash#once | ||
* @template A | ||
* @template R | ||
* @param {(...args: A[]) => R} fn | ||
* @returns {(...args: A[]) => R} | ||
*/ | ||
export const once = (fn) => { | ||
isFunction(fn) | ||
let called = false | ||
/** | ||
* @type {R} | ||
*/ | ||
let result | ||
|
||
/** | ||
* @type {(...args: A[]) => R} | ||
*/ | ||
return (...args) => { | ||
if (!called) { | ||
result = fn(...args) | ||
called = true | ||
} | ||
return result | ||
} | ||
} | ||
|
||
/** | ||
* @see https://youmightnotneed.com/lodash#debounce | ||
* | ||
* @template A | ||
* @template R | ||
* @param {(...args: A[]) => R} fn - The function to debounce. | ||
* @param {number} delay - The number of milliseconds to delay. | ||
* @param {Object} options | ||
* @param {boolean} [options.leading] | ||
* @returns {(...args: A[]) => void} | ||
*/ | ||
export const debounce = (fn, delay, { leading = false } = {}) => { | ||
isFunction(fn) && isNumber(delay) | ||
/** | ||
* @type {NodeJS.Timeout} | ||
*/ | ||
let timerId | ||
|
||
return (...args) => { | ||
if (!timerId && leading) { | ||
fn(...args) | ||
} | ||
clearTimeout(timerId) | ||
|
||
timerId = setTimeout(() => fn(...args), delay) | ||
} | ||
} | ||
|
||
/** | ||
* Call a function only once on the nth time it was called | ||
* @template A | ||
* @template R | ||
* @param {number} nth - The nth time the function should be called when it is actually invoked. | ||
* @param {(...args: A[]) => R} fn - The function to call. | ||
* @returns {(...args: A[]) => void | R} | ||
*/ | ||
export const onlyOnceAfter = (fn, nth) => { | ||
isFunction(fn) && isNumber(nth) | ||
return after(once(fn), nth) | ||
} |
Oops, something went wrong.