Skip to content

Commit

Permalink
fix: prevent warning when called with undefined or null during develo…
Browse files Browse the repository at this point in the history
…pment
  • Loading branch information
chrishelgert committed Mar 17, 2023
1 parent e2eb788 commit 78496c6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './styles.css';
import { FieldTagging } from './field-tagging';
import { LiveUpdates } from './live-updates';
import { Entity, LivePreviewProps, SubscribeCallback, TagAttributes } from './types';
import { Argument, LivePreviewProps, SubscribeCallback, TagAttributes } from './types';
import { sendMessageToEditor } from './utils';

export class ContentfulLivePreview {
Expand Down Expand Up @@ -37,7 +37,7 @@ export class ContentfulLivePreview {
}
}

static subscribe(data: Entity, locale: string, callback: SubscribeCallback): VoidFunction {
static subscribe(data: Argument, locale: string, callback: SubscribeCallback): VoidFunction {
if (!this.liveUpdates) {
throw new Error(
'Live Updates are not initialized, please call `ContentfulLivePreview.init()` first.'
Expand Down
12 changes: 6 additions & 6 deletions src/react.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { useState } from 'react';

import useDeepCompareEffect from 'use-deep-compare-effect';
import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect';

import { ContentfulLivePreview } from '.';
import { Entity } from './types';
import { Argument } from './types';
import { debounce } from './utils';

export function useContentfulLiveUpdates<T extends Entity | null | undefined>(
export function useContentfulLiveUpdates<T extends Argument | null | undefined>(
data: T,
locale: string
): T {
const [state, setState] = useState(data);

useDeepCompareEffect(() => {
useDeepCompareEffectNoCheck(() => {
// update content from external
setState(data);
// nothing to merge if there are no data
if (!data) {
// nothing to merge if there is no data
if (!data || (Array.isArray(data) && !data.length) || !Object.keys(data).length) {
return;
}
// or update content through live updates
Expand Down
13 changes: 13 additions & 0 deletions src/tests/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { act, renderHook } from '@testing-library/react';

import { useContentfulLiveUpdates } from '../react';
import { ContentfulLivePreview } from '..';
import { Argument } from '../types';

describe('useContentfulLiveUpdates', () => {
const unsubscribe = vi.fn();
Expand Down Expand Up @@ -100,4 +101,16 @@ describe('useContentfulLiveUpdates', () => {
expect(result.current).toEqual(updatedData);
expect(counter).toEqual(2);
});

it('shouldnt listen to changes if the initial data is empty', () => {
const { rerender } = renderHook((data) => useContentfulLiveUpdates(data, locale), {
initialProps: undefined as Argument | null | undefined,
});

rerender(null);
rerender([]);
rerender({});

expect(subscribe).not.toHaveBeenCalled();
});
});

0 comments on commit 78496c6

Please sign in to comment.