Skip to content

Commit 46c6e07

Browse files
committed
fix(delay): prevent infinite mode from throwing
1 parent 89adbf8 commit 46c6e07

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/core/utils/internal/hasRefCounted.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,24 @@ it('returns false for a non-refcounted object', () => {
1414
expect(hasRefCounted({ ref() {} })).toBe(false)
1515
expect(hasRefCounted({ unref() {} })).toBe(false)
1616
})
17+
18+
it('returns false for non-object values', () => {
19+
expect(
20+
hasRefCounted(
21+
// @ts-expect-error Runtime value.
22+
null,
23+
),
24+
).toBe(false)
25+
expect(
26+
hasRefCounted(
27+
// @ts-expect-error Runtime value.
28+
123,
29+
),
30+
).toBe(false)
31+
expect(
32+
hasRefCounted(
33+
// @ts-expect-error Runtime value.
34+
'invalid',
35+
),
36+
).toBe(false)
37+
})

src/core/utils/internal/hasRefCounted.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
import { isObject } from './isObject'
2+
13
export function hasRefCounted<T extends object>(
24
value: T,
35
): value is T & NodeJS.RefCounted {
46
return (
7+
/**
8+
* @note Guard against non-object values.
9+
* E.g. `setTimeout` returns an object in Node.js but a number in the browser.
10+
*/
11+
isObject(value) &&
512
typeof Reflect.get(value, 'ref') === 'function' &&
613
typeof Reflect.get(value, 'unref') === 'function'
714
)

0 commit comments

Comments
 (0)