Skip to content

Commit

Permalink
Fix session data update
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbarbara committed Jul 12, 2021
1 parent 90a0df7 commit be6b15a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
3 changes: 3 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports = {
globals: {
'ts-jest': {
tsconfig: 'test/tsconfig.json',
diagnostics: {
ignoreCodes: ['TS151001'],
},
},
},
moduleDirectories: ['node_modules', 'src', './'],
Expand Down
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"react": "^16.8.0 || ^17.0.0"
},
"dependencies": {
"fast-deep-equal": "^3.1.3",
"luxon": "^1.28.0",
"web-session": "^0.2.2"
},
Expand Down
14 changes: 14 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DependencyList, EffectCallback, useEffect, useRef } from 'react';
import * as isDeepEqualReact from 'fast-deep-equal/react';

export function useDeepCompareEffect<T extends DependencyList>(effect: EffectCallback, deps: T) {
const ref = useRef<T | undefined>(undefined);

/* istanbul ignore else */
if (!ref.current || !isDeepEqualReact(deps, ref.current)) {
ref.current = deps;
}

// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(effect, ref.current);
}
21 changes: 14 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ReactNode, useEffect, useRef } from 'react';
import { ReactNode, useCallback, useEffect, useState } from 'react';
import { createBrowserHistory, History } from 'history';
import WebSession, { AnyObject, Options, Session } from 'web-session';

import { useDeepCompareEffect } from './helpers';

interface Props extends Partial<Options> {
children?: ReactNode | ((session: Session, history: History) => ReactNode);
data?: AnyObject;
Expand All @@ -22,10 +24,11 @@ function ReactWebSession(props: Props) {
name = 'WebSessionData',
timezone = 'UTC',
} = props;
const [sessionData, setSessionData] = useState(data);

const setData = useRef(() => {
webSession.update(data);
});
const setData = useCallback(() => {
updateSession(sessionData);
}, [sessionData]);

useEffect(() => {
webSession.init({
Expand All @@ -34,16 +37,20 @@ function ReactWebSession(props: Props) {
name,
timezone,
});
setData.current();
setData();

const removeListener = history.listen(() => {
setData.current();
setData();
});

return () => {
removeListener();
};
}, [callback, duration, history, name, timezone]);
}, [callback, duration, history, name, setData, timezone]);

useDeepCompareEffect(() => {
setSessionData(data);
}, [data]);

if (typeof children === 'function') {
return children(webSession.session, history);
Expand Down
1 change: 0 additions & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "../tsconfig",
"compilerOptions": {
"esModuleInterop": true,
"module": "esnext"
},
"include": ["**/*"]
Expand Down

0 comments on commit be6b15a

Please sign in to comment.