Skip to content

Commit

Permalink
Add check for Window in deepFreeze (#1571)
Browse files Browse the repository at this point in the history
Summary:
Fixes facebookexperimental/Recoil#904

Pull Request resolved: facebookexperimental/Recoil#1571

Reviewed By: drarmstr

Differential Revision: D33829687

Pulled By: mondaychen

fbshipit-source-id: 93a891d87d956c83eda2c102f84868b0a6cffae3
  • Loading branch information
jackwolfskin0302 authored and facebook-github-bot committed Feb 2, 2022
1 parent 9d89b4f commit c6dc384
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

**_Add new changes here as they land_**

- `shouldNotBeFrozen` now works in JS environment without `Window` interface. (#1571)

## 0.6.1 (2022-01-29)

- Fix postInstall script (#1577)
Expand Down
12 changes: 11 additions & 1 deletion packages/shared/util/Recoil_Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@
*/
'use strict';

const isSSR: boolean = typeof window === 'undefined';
const isSSR: boolean =
// $FlowFixMe(site=recoil) Window does not have a FlowType definition https://github.com/facebook/flow/issues/6709
// eslint-disable-next-line fb-www/typeof-undefined
typeof Window === 'undefined' || typeof window === 'undefined';

const isWindow = (value: mixed): boolean =>
!isSSR &&
// $FlowFixMe(site=recoil) Window does not have a FlowType definition https://github.com/facebook/flow/issues/6709
(value === window || value instanceof Window);

const isReactNative: boolean =
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'; // eslint-disable-line fb-www/typeof-undefined

module.exports = {
isSSR,
isReactNative,
isWindow,
};
9 changes: 2 additions & 7 deletions packages/shared/util/Recoil_deepFreezeValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
'use strict';

const {isReactNative, isSSR} = require('./Recoil_Environment');
const {isReactNative, isWindow} = require('./Recoil_Environment');
const isNode = require('./Recoil_isNode');
const isPromise = require('./Recoil_isPromise');

Expand Down Expand Up @@ -61,12 +61,7 @@ function shouldNotBeFrozen(value: mixed): boolean {
}

// Some environments, just as Jest, don't work with the instanceof check
if (
!isSSR &&
!isReactNative &&
// $FlowFixMe(site=recoil) Window does not have a FlowType definition https://github.com/facebook/flow/issues/6709
(value === window || value instanceof Window)
) {
if (!isReactNative && isWindow(value)) {
return true;
}

Expand Down

0 comments on commit c6dc384

Please sign in to comment.