-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathcanUse.ts
35 lines (28 loc) · 1.59 KB
/
canUse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { maybe } from "../globals";
export const canUseWeakMap =
typeof WeakMap === 'function' &&
maybe(() => navigator.product) !== 'ReactNative';
export const canUseWeakSet = typeof WeakSet === 'function';
export const canUseSymbol =
typeof Symbol === 'function' &&
typeof Symbol.for === 'function';
export const canUseAsyncIteratorSymbol = canUseSymbol && Symbol.asyncIterator;
export const canUseDOM =
typeof maybe(() => window.document.createElement) === "function";
const usingJSDOM: boolean =
// Following advice found in this comment from @domenic (maintainer of jsdom):
// https://github.com/jsdom/jsdom/issues/1537#issuecomment-229405327
//
// Since we control the version of Jest and jsdom used when running Apollo
// Client tests, and that version is recent enought to include " jsdom/x.y.z"
// at the end of the user agent string, I believe this case is all we need to
// check. Testing for "Node.js" was recommended for backwards compatibility
// with older version of jsdom, but we don't have that problem.
maybe(() => navigator.userAgent.indexOf("jsdom") >= 0) || false;
// Our tests should all continue to pass if we remove this !usingJSDOM
// condition, thereby allowing useLayoutEffect when using jsdom. Unfortunately,
// if we allow useLayoutEffect, then useSyncExternalStore generates many
// warnings about useLayoutEffect doing nothing on the server. While these
// warnings are harmless, this !usingJSDOM condition seems to be the best way to
// prevent them (i.e. skipping useLayoutEffect when using jsdom).
export const canUseLayoutEffect = canUseDOM && !usingJSDOM;