Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(in-app-messaging): Add web support for session cap #9308

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,71 @@
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

import { ConsoleLogger as Logger } from '@aws-amplify/core';
import noop from 'lodash/noop';
import { SessionStateChangeHandler, SessionTrackerInterface } from './types';
import {
SessionState,
SessionStateChangeHandler,
SessionTrackerInterface,
} from './types';

// Per https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
let hidden: string;
let visibilityChange: string;

if (document) {
if (typeof document.hidden !== 'undefined') {
hidden = 'hidden';
visibilityChange = 'visibilitychange';
} else if (typeof document['msHidden'] !== 'undefined') {
hidden = 'msHidden';
visibilityChange = 'msvisibilitychange';
} else if (typeof document['webkitHidden'] !== 'undefined') {
hidden = 'webkitHidden';
visibilityChange = 'webkitvisibilitychange';
}
}

const logger = new Logger('InAppMessagingSessionTracker');

export default class SessionTracker implements SessionTrackerInterface {
constructor(sessionStateChangeHandler: SessionStateChangeHandler = noop) {}
start = noop as SessionTrackerInterface['start'];
end = noop as SessionTrackerInterface['end'];
private sessionStateChangeHandler: SessionStateChangeHandler;

constructor(sessionStateChangeHandler: SessionStateChangeHandler = noop) {
this.sessionStateChangeHandler = sessionStateChangeHandler;
}

start = (): SessionState => {
document?.addEventListener(visibilityChange, this.visibilityChangeHandler);
return this.getSessionState();
};

end = (): SessionState => {
document?.removeEventListener(
visibilityChange,
this.visibilityChangeHandler
);
return this.getSessionState();
};

private getSessionState = (): SessionState => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Observation: return type is SessionState but this function returns a string.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah the typing for SessionState is as follows:

export type SessionState = 'started' | 'ended';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! right 👍

if (document && !document[hidden]) {
return 'started';
}
// If, for any reason, document is undefined the session will never start
return 'ended';
};

private visibilityChangeHandler = () => {
if (!document) {
return;
}
if (document[hidden]) {
logger.debug('App is now hidden');
this.sessionStateChangeHandler('ended');
} else {
logger.debug('App is now visible');
this.sessionStateChangeHandler('started');
}
};
}