From 236dc028aae6c63f7803dd1669a35ba127f386d8 Mon Sep 17 00:00:00 2001 From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com> Date: Fri, 20 Aug 2021 09:13:18 +0200 Subject: [PATCH] Fix double polls (#18) --- packages/core/src/index.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 8a5d6cd..20b1138 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -38,6 +38,7 @@ class WakuVoting { private timedPollInitMessages: PollInitMsg[] = [] private timedPollVotesMessages: TimedPollVoteMsg[] = [] + private asyncUpdating = false private static async createWaku() { const waku = await Waku.create() @@ -135,17 +136,23 @@ class WakuVoting { } public async getDetailedTimedPolls() { - const { polls, updatedPolls } = await this.getTimedPolls() - const { votes, updatedVotes } = await this.getTimedPollsVotes() + let updated = false + if (!this.asyncUpdating) { + this.asyncUpdating = true + const { polls, updatedPolls } = await this.getTimedPolls() + const { votes, updatedVotes } = await this.getTimedPollsVotes() + updated = updatedPolls || updatedVotes + this.asyncUpdating = false + } return { - DetailedTimedPolls: polls.map( + DetailedTimedPolls: this.timedPollInitMessages.map( (poll) => new DetailedTimedPoll( poll, - votes.filter((vote) => vote.id === poll.id) + this.timedPollVotesMessages.filter((vote) => vote.id === poll.id) ) ), - updated: updatedPolls || updatedVotes, + updated, } } }