From 0bdd5147fdd32f554704c79df0b099bacf7d0b20 Mon Sep 17 00:00:00 2001 From: r1skz3ro Date: Wed, 7 Jul 2021 11:43:24 +0200 Subject: [PATCH 1/2] feat: Toast queue implemented --- src/index.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 3f14bb5e9..f452874db 100644 --- a/src/index.js +++ b/src/index.js @@ -109,7 +109,8 @@ class Toast extends Component { keyboardHeight: 0, keyboardVisible: false, - customProps: {} + customProps: {}, + toastQueue: [] }; this.panResponder = PanResponder.create({ @@ -125,6 +126,17 @@ class Toast extends Component { } }); } + + componentDidUpdate(_, prevState) { + const { inProgress, isVisible, toastQueue } = this.state; + const { inProgress: prevInProgress, isVisible: prevIsVisible } = prevState; + + if (!inProgress && !isVisible && prevInProgress && prevIsVisible && toastQueue.length > 0) { + toastQueue[toastQueue.length - 1](); + const toastQueueCopy = [...toastQueue].slice(0,-1); + this.setState({toastQueue: toastQueueCopy}) + } + } componentDidMount() { const noop = { @@ -212,10 +224,15 @@ class Toast extends Component { async show(options = {}) { const { inProgress, isVisible } = this.state; + if (inProgress || isVisible) { - await this.hide(); + this.setState({toastQueue: [...this.state.toastQueue, () => this.displayToast(options)]}) + } else { + this.displayToast(options) } + } + async displayToast(options = {}) { await this._setState((prevState) => ({ ...prevState, ...getInitialState(this.props), // Reset layout @@ -310,7 +327,7 @@ class Toast extends Component { ...config }; - const { type, customProps } = this.state; + const { type, customProps, inProgress, isVisible } = this.state; const toastComponent = componentsConfig[type]; if (!toastComponent) { @@ -321,6 +338,7 @@ class Toast extends Component { return null; } + return toastComponent({ ...includeKeys({ obj: this.state, From eb41ba5dd724683dd4823001ea5c5359de879449 Mon Sep 17 00:00:00 2001 From: r1skz3ro Date: Wed, 7 Jul 2021 15:52:35 +0200 Subject: [PATCH 2/2] chore: clean code --- src/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index f452874db..59abc636d 100644 --- a/src/index.js +++ b/src/index.js @@ -97,6 +97,7 @@ class Toast extends Component { this.startTimer = this.startTimer.bind(this); this.animate = this.animate.bind(this); this.show = this.show.bind(this); + this.displayToast = this.displayToast.bind(this); this.hide = this.hide.bind(this); this.onLayout = this.onLayout.bind(this); @@ -132,9 +133,10 @@ class Toast extends Component { const { inProgress: prevInProgress, isVisible: prevIsVisible } = prevState; if (!inProgress && !isVisible && prevInProgress && prevIsVisible && toastQueue.length > 0) { + const toastQueueUpdated = [...toastQueue].slice(0,-1); + toastQueue[toastQueue.length - 1](); - const toastQueueCopy = [...toastQueue].slice(0,-1); - this.setState({toastQueue: toastQueueCopy}) + this.setState({toastQueue: toastQueueUpdated}) } } @@ -226,7 +228,7 @@ class Toast extends Component { const { inProgress, isVisible } = this.state; if (inProgress || isVisible) { - this.setState({toastQueue: [...this.state.toastQueue, () => this.displayToast(options)]}) + this.setState({toastQueue: [...this.state.toastQueue, () => this.displayToast(options)]}) } else { this.displayToast(options) } @@ -327,7 +329,7 @@ class Toast extends Component { ...config }; - const { type, customProps, inProgress, isVisible } = this.state; + const { type, customProps} = this.state; const toastComponent = componentsConfig[type]; if (!toastComponent) { @@ -338,7 +340,6 @@ class Toast extends Component { return null; } - return toastComponent({ ...includeKeys({ obj: this.state,