-
Notifications
You must be signed in to change notification settings - Fork 80
fix(alert): properly form a queue of alerts #10032
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
Merged
Merged
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
042e866
WIP
driskull 12009f5
feat(alert): add urgent property to prioritize alert queue. #8705
driskull a6e416d
cleanup
driskull 4479751
cleanup
driskull ea1f450
stories
driskull 82669cc
cleanup story
driskull 78ab672
remove comments
driskull 6f74d0b
fix(alert): properly form a queue of alerts. #9340
driskull 1b75efe
cleanup
driskull 6a8956a
spec test
driskull f213cbc
cleanup
driskull 68a0e06
cleanup
driskull f75a4ae
cleanup
driskull 1ea9c1c
review fixes
driskull 207e7cf
review fixes
driskull 3ad4857
doc update
driskull fc673c6
Merge branch 'dris0000/urgent-alert' into dris0000/alert-manager
driskull 24e1a28
moar tests
driskull b9e7826
Merge branch 'dris0000/urgent-alert' into dris0000/alert-manager
driskull 61438f3
Merge branch 'dev' into dris0000/alert-manager
driskull 9cb2a2e
review fixes; clean up tests.
driskull 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 hidden or 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
54 changes: 54 additions & 0 deletions
54
packages/calcite-components/src/components/alert/AlertManager.spec.ts
This file contains hidden or 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,54 @@ | ||
import AlertManager, { alertQueueTimeoutMs } from "./AlertManager"; | ||
|
||
describe("AlertManager", () => { | ||
let alertManager: AlertManager; | ||
let mockAlert1: HTMLCalciteAlertElement; | ||
let mockAlert2: HTMLCalciteAlertElement; | ||
|
||
beforeEach(() => { | ||
alertManager = new AlertManager(); | ||
|
||
mockAlert1 = { | ||
active: false, | ||
queue: "last", | ||
openAlertCount: 0, | ||
} as HTMLCalciteAlertElement; | ||
|
||
mockAlert2 = { | ||
active: false, | ||
queue: "last", | ||
openAlertCount: 0, | ||
} as HTMLCalciteAlertElement; | ||
}); | ||
|
||
it("should activate the first alert after a timeout", async () => { | ||
alertManager.registerElement(mockAlert1); | ||
|
||
expect(mockAlert1.active).toBe(false); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, alertQueueTimeoutMs)); | ||
|
||
expect(mockAlert1.active).toBe(true); | ||
}); | ||
|
||
it("should deactivate subsequent alerts", async () => { | ||
alertManager.registerElement(mockAlert1); | ||
alertManager.registerElement(mockAlert2); | ||
|
||
expect(mockAlert1.active).toBe(false); | ||
expect(mockAlert2.active).toBe(false); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, alertQueueTimeoutMs)); | ||
|
||
expect(mockAlert1.active).toBe(true); | ||
expect(mockAlert2.active).toBe(false); | ||
}); | ||
|
||
it("should set the openAlertCount correctly", () => { | ||
alertManager.registerElement(mockAlert1); | ||
alertManager.registerElement(mockAlert2); | ||
|
||
expect(mockAlert1.openAlertCount).toBe(2); | ||
expect(mockAlert2.openAlertCount).toBe(2); | ||
}); | ||
}); |
73 changes: 73 additions & 0 deletions
73
packages/calcite-components/src/components/alert/AlertManager.ts
This file contains hidden or 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,73 @@ | ||
export const alertQueueTimeoutMs = 300; | ||
|
||
export default class AlertManager { | ||
// -------------------------------------------------------------------------- | ||
// | ||
// Private Properties | ||
// | ||
// -------------------------------------------------------------------------- | ||
|
||
private registeredElements: HTMLCalciteAlertElement[] = []; | ||
|
||
private queueTimeoutId: number = null; | ||
|
||
// -------------------------------------------------------------------------- | ||
// | ||
// Public Methods | ||
// | ||
// -------------------------------------------------------------------------- | ||
|
||
registerElement(alert: HTMLCalciteAlertElement): void { | ||
const { registeredElements } = this; | ||
|
||
if (!registeredElements.includes(alert)) { | ||
switch (alert.queue) { | ||
case "immediate": | ||
registeredElements.unshift(alert); | ||
break; | ||
case "next": | ||
registeredElements.splice(1, 0, alert); | ||
break; | ||
case "last": | ||
registeredElements.push(alert); | ||
break; | ||
} | ||
|
||
this.updateAlerts(); | ||
} | ||
} | ||
|
||
unregisterElement(alert: HTMLCalciteAlertElement): void { | ||
const { registeredElements } = this; | ||
|
||
const index = registeredElements.indexOf(alert); | ||
|
||
if (index !== -1) { | ||
registeredElements.splice(index, 1); | ||
} | ||
|
||
alert.active = false; | ||
this.updateAlerts(); | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// | ||
// Private Methods | ||
// | ||
// -------------------------------------------------------------------------- | ||
|
||
private updateAlerts(): void { | ||
window.clearTimeout(this.queueTimeoutId); | ||
this.queueTimeoutId = null; | ||
|
||
this.registeredElements.forEach((alert, index) => { | ||
alert.openAlertCount = this.registeredElements.length; | ||
|
||
if (index === 0) { | ||
this.queueTimeoutId = window.setTimeout(() => (alert.active = true), alertQueueTimeoutMs); | ||
} else { | ||
alert.active = false; | ||
} | ||
}); | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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.
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.
In the future, this could be a map
This would allow us to have alert
groups
by adding agroup
property toAlert
. Then only certain alerts would be grouped with other ones.