From 09ee4f277f97a8a54effeade938a721c5f846b24 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 8 Feb 2021 11:57:55 +0200 Subject: [PATCH] Fix for events firing multiple times --- package.json | 2 +- src/contracts/contractsSaga.js | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 1da0e22..6a8eded 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ezerous/drizzle", - "version": "0.4.1", + "version": "0.4.2", "description": "A reactive data-store for web3 and smart contracts.", "license": "MIT", "author": "Ezerous ", diff --git a/src/contracts/contractsSaga.js b/src/contracts/contractsSaga.js index 12a927f..f14b16d 100644 --- a/src/contracts/contractsSaga.js +++ b/src/contracts/contractsSaga.js @@ -6,6 +6,18 @@ import * as TransactionsActions from '../transactions/transactionsActions' /* * Events */ + +// Patch for EVENT_FIRED being called multiple times for the same event +let eventSet = new Set(); + +function isEventUnique(event){ + const size = eventSet.size; + eventSet.add(event.event + '-' + event.transactionHash); + if(eventSet.size>size) + return true; + return false; +} + export function createContractEventChannel ({ contract, eventName, @@ -14,9 +26,10 @@ export function createContractEventChannel ({ const name = contract.contractName return eventChannel(emit => { - const eventListener = contract.events[eventName](eventOptions) + const eventListener = contract.events[eventName]() .on('data', event => { - emit({ type: ContractActions.CONTRACT_EVENT_FIRED, name, event }) + if(isEventUnique(event)) + emit({ type: ContractActions.CONTRACT_EVENT_FIRED, name, event }) }) .on('changed', event => { emit({ type: ContractActions.CONTRACT_EVENT_CHANGED, name, event })