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

Update notify.js #1

Merged
merged 1 commit into from
Jan 27, 2017
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
55 changes: 54 additions & 1 deletion src/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ function hideToast() {
/* Public functions */

/* Show Animated Toast Message */
/* Returns true if the toast was shown, or false if show failed due to an existing notification */
function show(text, type, timeout, color) {
if (!document.getElementById(notificationWrapperId).hasChildNodes()) {
let renderTimeout = timeout;
Expand All @@ -207,9 +208,60 @@ function show(text, type, timeout, color) {
setTimeout(function() {
hideToast();
}, renderTimeout + animationDuration);

return true;
}
return false;
}

/* Add to Animated Toast Message Queue */
/* Display immediately if no queue */
/* initialRecallDelay: If the call to show fails because of an existing notification, how long to wait until we retry (ms) */
/* recallDelayIncrement: Each time a successive call fails, the recall delay will be incremented by this (ms) */
function createShowQueue(initialRecallDelay = 500, recallDelayIncrement = 500) {

// Array to hold queued messages
this.msgs = [];

// Is the showNotify function in progress - used so we can call showNotify when a
// message is added to an empty queue.
this.isNotifying = false;

this.currentRecallDelay = initialRecallDelay;

// Retrieve the next message from the queue and try to show it
this.showNotify = () => {
// If there are no messages in the queue
if (this.msgs.length ===0) {
this.isNotifying = false;
return;
}

this.isNotifying = true;

const current = this.msgs.pop();

// show will now return true if it is able to send the message, or false if there is an existing message
if (show(current.text, current.type, current.timeout, current.color)) {
this.currentRecallDelay = initialRecallDelay;
if (current.timeout > 0) {
setTimeout(() => this.showNotify(), current.timeout + animationDuration);
}
} else {
// If message show failed, re-add the current message to the front of the queue
this.msgs.unshift(current);
setTimeout(() => this.showNotify(), this.currentRecallDelay);
this.currentRecallDelay += recallDelayIncrement;
}
}

return (text, type = '', timeout = defaultTimeout, color = colorWhite) => {
this.msgs.push({ text, type, timeout, color });
if (!this.isNotifying) {
this.showNotify();
}
}
}

/* Export notification container */
export default class extends React.Component {
Expand All @@ -222,5 +274,6 @@ export default class extends React.Component {

/* Export notification functions */
export let notify = {
show
show,
createShowQueue
};