Skip to content

Commit

Permalink
refactor: add isServer to be compatible with node env
Browse files Browse the repository at this point in the history
  • Loading branch information
John60676 committed Dec 31, 2020
1 parent ec7dc30 commit 4f1c797
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/__tests__/utils-node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @jest-environment node
*/

import { isServer } from '../core/utils';

describe('utils-node-env', () => {
test('isServer should work', async () => {
expect(isServer).toBe(true);
});
});
6 changes: 4 additions & 2 deletions src/core/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ export const isFunction = (fn: unknown): fn is Function =>

export const isNil = (val: unknown) => val === null || val === undefined;

export const isServer = typeof window === 'undefined';

export const isDocumentVisibility = () =>
window?.document?.visibilityState === 'visible';
!isServer && window?.document?.visibilityState === 'visible';

export const isOnline = () => window?.navigator?.onLine ?? true;
export const isOnline = () => (!isServer && window?.navigator?.onLine) ?? true;

export const unRefObject = <T extends RefObject>(val: T) => {
const obj = {};
Expand Down
4 changes: 2 additions & 2 deletions src/core/utils/listener.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDocumentVisibility } from './index';
import { isDocumentVisibility, isServer } from './index';

type EventFunc = () => void;
type ListenersSet = Set<EventFunc>;
Expand Down Expand Up @@ -40,7 +40,7 @@ const observer = (listeners: ListenersSet) => {
};

/* istanbul ignore else */
if (window?.addEventListener) {
if (!isServer && window?.addEventListener) {
window.addEventListener(
'visibilitychange',
() => {
Expand Down

0 comments on commit 4f1c797

Please sign in to comment.