Skip to content

Commit

Permalink
refactor toast timeout update logic to be more obvious (#3275)
Browse files Browse the repository at this point in the history
always clear timeout when starting to cancel any previous now-outdated active timer.
  • Loading branch information
giladgray authored Jan 11, 2019
1 parent d7b3425 commit aa6b21f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions packages/core/src/components/toast/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ export class Toast extends AbstractPureComponent<IToastProps, {}> {
}

public componentDidUpdate(prevProps: IToastProps) {
if (prevProps.timeout !== this.props.timeout && this.props.timeout > 0) {
this.startTimeout();
} else if (prevProps.timeout <= 0 && this.props.timeout > 0) {
this.startTimeout();
} else if (prevProps.timeout > 0 && this.props.timeout <= 0) {
this.clearTimeouts();
if (prevProps.timeout !== this.props.timeout) {
if (this.props.timeout > 0) {
this.startTimeout();
} else {
this.clearTimeouts();
}
}
}

Expand All @@ -109,11 +109,12 @@ export class Toast extends AbstractPureComponent<IToastProps, {}> {
private handleCloseClick = () => this.triggerDismiss(false);

private triggerDismiss(didTimeoutExpire: boolean) {
safeInvoke(this.props.onDismiss, didTimeoutExpire);
this.clearTimeouts();
safeInvoke(this.props.onDismiss, didTimeoutExpire);
}

private startTimeout = () => {
this.clearTimeouts();
if (this.props.timeout > 0) {
this.setTimeout(() => this.triggerDismiss(true), this.props.timeout);
}
Expand Down

1 comment on commit aa6b21f

@blueprint-bot
Copy link

Choose a reason for hiding this comment

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

refactor toast timeout update logic to be more obvious (#3275)

Previews: documentation | landing | table

Please sign in to comment.