-
Notifications
You must be signed in to change notification settings - Fork 6
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
Adding package to support user activity going forward #766
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1213b58
feat(user-activity-broadcaster): adding package EB-719
1607004
feat(user-activity-broadcaster): adding changelog file EB-719
b66e9b8
feat(user-activity-broadcaster): adding ignore because we're not usin…
6e9519d
chore(user-activity-broadcaster): release version 0.1.0-alpha.0 [skip…
be9d03e
feat(user-activity-broadcaster): fixing commit message EB-719
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Changelog | ||
|
||
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). | ||
|
||
# 0.1.0-alpha.0 (2024-10-22) | ||
|
||
|
||
### Features | ||
|
||
* **user-activity-broadcaster:** adding changelog file EB-719 ([1607004](https://github.com/Availity/sdk-js/commit/16070049522d8db8401d71f7d5d074dc0bc213c4)) | ||
* **user-activity-broadcaster:** adding ignore because we're not using typescript EB-719 ([b66e9b8](https://github.com/Availity/sdk-js/commit/b66e9b87c2dcd305cd1ccbba5980e7550cc1bd70)) | ||
* **user-activity-broadcaster:** adding package EB-719 ([1213b58](https://github.com/Availity/sdk-js/commit/1213b58c37fcec8f5f6298e6e66103150a9db61e)) | ||
|
||
|
||
|
||
# 0.1.0-alpha.0 (2024-10-22) | ||
|
||
|
||
### Features | ||
|
||
* **user-activity-broadcaster:** adding changelog file EB-719 ([1607004](https://github.com/Availity/sdk-js/commit/16070049522d8db8401d71f7d5d074dc0bc213c4)) | ||
* **user-activity-broadcaster:** adding ignore because we're not using typescript EB-719 ([b66e9b8](https://github.com/Availity/sdk-js/commit/b66e9b87c2dcd305cd1ccbba5980e7550cc1bd70)) | ||
* **user-activity-broadcaster:** adding package EB-719 ([1213b58](https://github.com/Availity/sdk-js/commit/1213b58c37fcec8f5f6298e6e66103150a9db61e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# @availity/user-activity-broadcaster | ||
|
||
> This package broadcasts user activity to the navigation. | ||
|
||
[![Version](https://img.shields.io/npm/v/@availity/user-activity-broadcaster.svg?style=for-the-badge)](https://www.npmjs.com/package/@availity/user-activity-broadcaster) | ||
[![NPM Downloads](https://img.shields.io/npm/dt/@availity/user-activity-broadcaster.svg?style=for-the-badge)](https://www.npmjs.com/package/@availity/user-activity-broadcaster) | ||
[![Dependecy Status](https://img.shields.io/librariesio/release/npm/@availity/user-activity-broadcaster?style=for-the-badge)](https://github.com/Availity/sdk-js/blob/master/packages/user-activity-broadcaster/package.json) | ||
|
||
## Installation | ||
|
||
### NPM | ||
|
||
```bash | ||
npm install @availity/user-activity-broadcaster | ||
``` | ||
|
||
### Yarn | ||
|
||
```bash | ||
yarn add @availity/user-activity-broadcaster | ||
``` | ||
|
||
## Usage | ||
|
||
> All you have to do is include this as a dependency and import it into the root of your web application. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const global = require('../../jest.config'); | ||
|
||
module.exports = { | ||
...global, | ||
displayName: 'user-activity-broadcaster', | ||
coverageDirectory: '../../coverage/user-activity-broadcaster', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@availity/user-activity-broadcaster", | ||
"version": "0.1.0-alpha.0", | ||
"description": "This package broacasts user activity to the navigation.", | ||
"main": "src/user-activity-broadcaster.js", | ||
"keywords": [], | ||
"author": "Joe Spanbauer <joseph.spanbauer@availity.com>", | ||
"engines": { | ||
"node": "^18.0.0 || ^20.0.0" | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.js --format esm,cjs --dts", | ||
"dev": "tsup src/index.js --format esm,cjs --watch --dts", | ||
"lint": "eslint src", | ||
"lint:fix": "eslint src --fix", | ||
"clean": "rm -rf node_modules && rm -rf dist", | ||
"publish": "yarn npm publish --tolerate-republish --access public" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"tsup": "^7.2.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "@availity/user-activity-broadcaster", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"projectType": "library", | ||
"targets": { | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/resolve-url"], | ||
"options": { | ||
"jestConfig": "packages/user-activity-broadcaster/jest.config.js" | ||
} | ||
}, | ||
"version": { | ||
"executor": "@jscutlery/semver:version", | ||
"options": { | ||
"preset": "angular", | ||
"commitMessageFormat": "chore(user-activity-broadcaster): release version ${version} [skip ci]", | ||
"tagPrefix": "{projectName}@", | ||
"baseBranch": "master", | ||
"trackDeps": true | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/eslint:lint", | ||
"options": { | ||
"eslintConfig": ".eslintrc.yaml", | ||
"silent": false, | ||
"fix": false, | ||
"cache": true, | ||
"cacheLocation": "./node_modules/.cache/user-activity-broadcaster/.eslintcache", | ||
"maxWarnings": -1, | ||
"quiet": false, | ||
"noEslintrc": false, | ||
"hasTypeAwareRules": true, | ||
"cacheStrategy": "metadata" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* eslint-disable unicorn/no-empty-file */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const MINUTE = 60 * 1000 | ||
const interval = MINUTE * 5 | ||
export const eventName = 'user_activity' | ||
|
||
// Must be a dynamic object to test this | ||
export const lastActivity = {} | ||
|
||
export const getTargetOrigin = (origin = window.location.origin) => { | ||
// Setup targetOrigin to alternate origin (because the same origin already works) | ||
if (origin) { | ||
if (origin.includes('apps')) { | ||
return origin.replace('apps', 'essentials') | ||
} | ||
|
||
if (origin.includes('essentials')) { | ||
return origin.replace('essentials', 'apps') | ||
} | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
const targetOrigin = getTargetOrigin() | ||
|
||
// PostMessage Logic | ||
export const handleActivityUpdate = () => { | ||
window.top.postMessage({ | ||
event: eventName, | ||
time: lastActivity.time | ||
}, targetOrigin) | ||
} | ||
|
||
// Debounce Logic | ||
let activityIntervalId = setInterval(handleActivityUpdate, interval) | ||
// Re-assignable for testing | ||
export const updateInterval = (newInterval) => { | ||
clearInterval(activityIntervalId) | ||
activityIntervalId = setInterval(handleActivityUpdate, newInterval) | ||
} | ||
|
||
// Event Handlers | ||
export const handleActivity = () => { | ||
lastActivity.time = Date.now().toString() | ||
} | ||
|
||
// Add ability to test handleActivity and events | ||
export const addEventListeners = () => { | ||
document.addEventListener('mousedown', handleActivity) | ||
document.addEventListener('keydown', handleActivity) | ||
} | ||
addEventListeners() |
76 changes: 76 additions & 0 deletions
76
packages/user-activity-broadcaster/src/tests/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const userActivityBroadcaster = require('../index') | ||
|
||
const { | ||
getTargetOrigin, | ||
handleActivity, | ||
updateInterval, | ||
addEventListeners, | ||
lastActivity | ||
} = userActivityBroadcaster | ||
|
||
describe('user-activity-broadcaster', () => { | ||
describe('targetOrigin', () => { | ||
test('essentials.availity.com origin should have targetOrigion of apps', () => { | ||
const testOrigin = 'essentials.availity.com' | ||
const expected = 'apps.availity.com' | ||
|
||
const targetOrigin = getTargetOrigin(testOrigin) | ||
|
||
expect(targetOrigin).toBe(expected) | ||
}) | ||
|
||
test('apps.availity.com origin should have targetOrigion of essentials', () => { | ||
const testOrigin = 'apps.availity.com' | ||
const expected = 'essentials.availity.com' | ||
|
||
const targetOrigin = getTargetOrigin(testOrigin) | ||
|
||
expect(targetOrigin).toBe(expected) | ||
}) | ||
}) | ||
|
||
test('should call handleActivityUpdate every interval', async () => { | ||
const testInterval = 1000 | ||
const waitTime = 2999 | ||
const expectedCallCount = 2 | ||
|
||
window.top.postMessage = () => {} | ||
|
||
const spy = jest.spyOn(userActivityBroadcaster, 'handleActivityUpdate') | ||
|
||
updateInterval(testInterval) | ||
|
||
await new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve() | ||
}, waitTime) | ||
}) | ||
|
||
expect(spy).toHaveBeenCalledTimes(expectedCallCount) | ||
}) | ||
|
||
test('keydown events call handleActivity', async () => { | ||
const spy = jest.spyOn(userActivityBroadcaster, 'handleActivity') | ||
|
||
addEventListeners() | ||
|
||
document.dispatchEvent(new MouseEvent('keydown')) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
}) | ||
|
||
test('mousedown events call handleActivity', async () => { | ||
const spy = jest.spyOn(userActivityBroadcaster, 'handleActivity') | ||
|
||
addEventListeners() | ||
|
||
document.dispatchEvent(new MouseEvent('mousedown')) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
}) | ||
|
||
test('handleActivity should update handleActivity', () => { | ||
handleActivity() | ||
expect(lastActivity.time).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../dist/out-tsc", | ||
"module": "commonjs", | ||
"types": ["jest", "node"], | ||
"allowJs": true | ||
}, | ||
"include": ["**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we return '*' here as a fallback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really only want this to work for our website. I did consider it, though.