diff --git a/packages/relay-experimental/ExecutionEnvironment.js b/packages/relay-experimental/ExecutionEnvironment.js new file mode 100644 index 0000000000000..67d9e14974f39 --- /dev/null +++ b/packages/relay-experimental/ExecutionEnvironment.js @@ -0,0 +1,20 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @emails oncall+relay + * @flow strict-local + * @format + */ + +// flowlint ambiguous-object-type:error + +'use strict'; + +const ExecutionEnvironment = { + isServer: typeof window === 'undefined', +}; + +module.exports = ExecutionEnvironment; diff --git a/packages/relay-experimental/QueryResource.js b/packages/relay-experimental/QueryResource.js index 7a162565079a6..2857c1af76e30 100644 --- a/packages/relay-experimental/QueryResource.js +++ b/packages/relay-experimental/QueryResource.js @@ -13,7 +13,7 @@ 'use strict'; -const ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment'); +const ExecutionEnvironment = require('./ExecutionEnvironment'); const LRUCache = require('./LRUCache'); const invariant = require('invariant'); @@ -152,7 +152,7 @@ function createCacheEntry( temporaryRetain(environment: IEnvironment): Disposable { // NOTE: If we're executing in a server environment, there's no need // to create temporary retains, since the component will never commit. - if (!ExecutionEnvironment.canUseDOM) { + if (ExecutionEnvironment.isServer) { return {dispose: () => {}}; } diff --git a/packages/relay-experimental/__tests__/EntryPointContainer-test.js b/packages/relay-experimental/__tests__/EntryPointContainer-test.js index 0f296fe0fdf6d..37b7ba9d66971 100644 --- a/packages/relay-experimental/__tests__/EntryPointContainer-test.js +++ b/packages/relay-experimental/__tests__/EntryPointContainer-test.js @@ -13,8 +13,8 @@ 'use strict'; -jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: true, +jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); const EntryPointContainer = require('../EntryPointContainer.react'); diff --git a/packages/relay-experimental/__tests__/LazyLoadEntryPointContainer_DEEPRECATED-test.js b/packages/relay-experimental/__tests__/LazyLoadEntryPointContainer_DEEPRECATED-test.js index 934a421ee7de1..e55fa6b09c6dd 100644 --- a/packages/relay-experimental/__tests__/LazyLoadEntryPointContainer_DEEPRECATED-test.js +++ b/packages/relay-experimental/__tests__/LazyLoadEntryPointContainer_DEEPRECATED-test.js @@ -13,8 +13,8 @@ 'use strict'; -jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: true, +jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); const LazyLoadEntryPointContainer_DEPRECATED = require('../LazyLoadEntryPointContainer_DEPRECATED.react'); diff --git a/packages/relay-experimental/__tests__/QueryResource-test.js b/packages/relay-experimental/__tests__/QueryResource-test.js index ab51dca68b29f..95513f79745f0 100644 --- a/packages/relay-experimental/__tests__/QueryResource-test.js +++ b/packages/relay-experimental/__tests__/QueryResource-test.js @@ -47,8 +47,8 @@ describe('QueryResource', () => { }; beforeEach(() => { - jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: () => true, + jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); store = new Store(new RecordSource()); environment = createMockEnvironment({store}); diff --git a/packages/relay-experimental/__tests__/fetchQuery-test.js b/packages/relay-experimental/__tests__/fetchQuery-test.js index 677a6f22d94f3..90fed9ed8f760 100644 --- a/packages/relay-experimental/__tests__/fetchQuery-test.js +++ b/packages/relay-experimental/__tests__/fetchQuery-test.js @@ -36,8 +36,8 @@ describe('fetchQuery', () => { let retained = []; beforeEach(() => { retained = []; - jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: () => true, + jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); environment = createMockEnvironment(); environment.retain.mockImplementation(obj => { diff --git a/packages/relay-experimental/__tests__/preloadQuery_DEPRECATED-test.js b/packages/relay-experimental/__tests__/preloadQuery_DEPRECATED-test.js index 5572597df0962..cf69bf612324d 100644 --- a/packages/relay-experimental/__tests__/preloadQuery_DEPRECATED-test.js +++ b/packages/relay-experimental/__tests__/preloadQuery_DEPRECATED-test.js @@ -13,8 +13,8 @@ 'use strict'; -jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: true, +jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); const preloadQuery_DEPRECATED = require('../preloadQuery_DEPRECATED'); diff --git a/packages/relay-experimental/__tests__/prepareEntryPoint-test.js b/packages/relay-experimental/__tests__/prepareEntryPoint-test.js index 31498dfd37158..f53194899fd10 100644 --- a/packages/relay-experimental/__tests__/prepareEntryPoint-test.js +++ b/packages/relay-experimental/__tests__/prepareEntryPoint-test.js @@ -13,8 +13,8 @@ 'use strict'; -jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: true, +jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); const prepareEntryPoint = require('../prepareEntryPoint'); diff --git a/packages/relay-experimental/__tests__/useBlockingPaginationFragment-test.js b/packages/relay-experimental/__tests__/useBlockingPaginationFragment-test.js index 38dc987329540..72385ddd29d38 100644 --- a/packages/relay-experimental/__tests__/useBlockingPaginationFragment-test.js +++ b/packages/relay-experimental/__tests__/useBlockingPaginationFragment-test.js @@ -130,8 +130,8 @@ describe('useBlockingPaginationFragment', () => { jest.resetModules(); jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); jest.mock('warning'); - jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: () => true, + jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); renderSpy = jest.fn(); diff --git a/packages/relay-experimental/__tests__/useBlockingPaginationFragment-with-suspense-transition-test.js b/packages/relay-experimental/__tests__/useBlockingPaginationFragment-with-suspense-transition-test.js index 9ca8e9bea8340..b54b6ee2b9231 100644 --- a/packages/relay-experimental/__tests__/useBlockingPaginationFragment-with-suspense-transition-test.js +++ b/packages/relay-experimental/__tests__/useBlockingPaginationFragment-with-suspense-transition-test.js @@ -173,8 +173,8 @@ describe('useBlockingPaginationFragment with useSuspenseTransition', () => { jest.resetModules(); jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); jest.mock('warning'); - jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: () => true, + jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); renderSpy = jest.fn(); diff --git a/packages/relay-experimental/__tests__/useFragmentNode-test.js b/packages/relay-experimental/__tests__/useFragmentNode-test.js index 37199c332ac78..88cb3ed5e02e2 100644 --- a/packages/relay-experimental/__tests__/useFragmentNode-test.js +++ b/packages/relay-experimental/__tests__/useFragmentNode-test.js @@ -137,8 +137,8 @@ beforeEach(() => { jest.mock('scheduler', () => { return jest.requireActual('scheduler/unstable_mock'); }); - jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: () => true, + jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); renderSpy = jest.fn(); diff --git a/packages/relay-experimental/__tests__/useLazyLoadQueryNode-test.js b/packages/relay-experimental/__tests__/useLazyLoadQueryNode-test.js index 96e7c0d5a8341..a2566738217ab 100644 --- a/packages/relay-experimental/__tests__/useLazyLoadQueryNode-test.js +++ b/packages/relay-experimental/__tests__/useLazyLoadQueryNode-test.js @@ -68,8 +68,8 @@ describe('useLazyLoadQueryNode', () => { beforeEach(() => { jest.resetModules(); jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); - jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: () => true, + jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); ({ diff --git a/packages/relay-experimental/__tests__/useLegacyPaginationFragment-test.js b/packages/relay-experimental/__tests__/useLegacyPaginationFragment-test.js index 07adcd6859e9b..4db3730ae4a47 100644 --- a/packages/relay-experimental/__tests__/useLegacyPaginationFragment-test.js +++ b/packages/relay-experimental/__tests__/useLegacyPaginationFragment-test.js @@ -663,8 +663,8 @@ describe('useLegacyPaginationFragment', () => { beforeEach(() => { jest.resetModules(); - jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: () => true, + jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); jest.doMock('scheduler', () => { const original = jest.requireActual('scheduler/unstable_mock'); diff --git a/packages/relay-experimental/__tests__/usePreloadedQuery-test.js b/packages/relay-experimental/__tests__/usePreloadedQuery-test.js index a5ba047cbd367..b1372dd5093bd 100644 --- a/packages/relay-experimental/__tests__/usePreloadedQuery-test.js +++ b/packages/relay-experimental/__tests__/usePreloadedQuery-test.js @@ -13,8 +13,8 @@ 'use strict'; -jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: true, +jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); const React = require('react'); diff --git a/packages/relay-experimental/__tests__/useRefetchableFragmentNode-test.js b/packages/relay-experimental/__tests__/useRefetchableFragmentNode-test.js index e308913c4872e..0d2b2133d0a02 100644 --- a/packages/relay-experimental/__tests__/useRefetchableFragmentNode-test.js +++ b/packages/relay-experimental/__tests__/useRefetchableFragmentNode-test.js @@ -119,8 +119,8 @@ describe('useRefetchableFragmentNode', () => { jest.mock('scheduler', () => { return jest.requireActual('scheduler/unstable_mock'); }); - jest.mock('fbjs/lib/ExecutionEnvironment', () => ({ - canUseDOM: () => true, + jest.mock('../ExecutionEnvironment', () => ({ + isServer: false, })); renderSpy = jest.fn(); diff --git a/packages/relay-experimental/preloadQuery_DEPRECATED.js b/packages/relay-experimental/preloadQuery_DEPRECATED.js index a8480e3dc67da..33c8e676b12a3 100644 --- a/packages/relay-experimental/preloadQuery_DEPRECATED.js +++ b/packages/relay-experimental/preloadQuery_DEPRECATED.js @@ -13,7 +13,7 @@ 'use strict'; -const ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment'); +const ExecutionEnvironment = require('./ExecutionEnvironment'); const invariant = require('invariant'); @@ -159,7 +159,7 @@ function preloadQueryDeduped( kind: 'cache', name: params.name, }; - if (ExecutionEnvironment.canUseDOM && prevQueryEntry == null) { + if (!ExecutionEnvironment.isServer && prevQueryEntry == null) { setTimeout(() => { // Clear the cache entry after the default timeout // null-check for Flow @@ -191,7 +191,7 @@ function preloadQueryDeduped( subject, subscription: source .finally(() => { - if (!ExecutionEnvironment.canUseDOM) { + if (ExecutionEnvironment.isServer) { return; } setTimeout(() => {