Skip to content

Commit

Permalink
fix: move EventManagerProvider to app-level (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-ziv authored May 22, 2022
1 parent 2cbc942 commit 7fbd4fe
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './useConstants';
export * from './useTranslation';
export * from './useTracking';
export * from './useBuyProviders';
export * from './useAccountChange';
13 changes: 13 additions & 0 deletions src/hooks/useAccountChange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {useEffect} from 'react';

import {useAccountHash} from '../providers/WalletsProvider';

export const useAccountChange = fn => {
const accountHash = useAccountHash();

useEffect(() => {
if (accountHash) {
fn();
}
}, [accountHash]);
};
35 changes: 12 additions & 23 deletions src/providers/EventManagerProvider/EventManagerProvider.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import PropTypes from 'prop-types';
import React, {useEffect} from 'react';
import React from 'react';

import {EventName, SelectorName} from '../../enums';
import {useL1TokenBridgeContract, useLogger} from '../../hooks';
import {useAccountChange, useL1TokenBridgeContract, useLogger} from '../../hooks';
import {starknet} from '../../libs';
import {parseToFelt} from '../../utils/parser';
import {parseToFelt} from '../../utils';
import {useL1Tokens, useL2Tokens} from '../TokensProvider';
import {useL1Wallet, useL2Wallet} from '../WalletsProvider';
import {EventManagerContext} from './event-manager-context';

const listeners = {};
const filters = {};
let emitters = [];

export const EventManagerProvider = ({children}) => {
const logger = useLogger(EventManagerProvider.displayName);
Expand All @@ -21,11 +20,10 @@ export const EventManagerProvider = ({children}) => {
const l1Tokens = useL1Tokens();
const l2Tokens = useL2Tokens();

useEffect(() => {
useAccountChange(() => {
setEventFilters();
addDepositWithdrawalListeners();
return () => cleanEmittersAndRemoveListeners();
}, []);
addListeners();
});

const addListener = (eventName, callback) => {
logger.log(`Registered to ${eventName} event.`);
Expand Down Expand Up @@ -62,7 +60,7 @@ export const EventManagerProvider = ({children}) => {
emitListeners(EventName.L1.LOG_DEPOSIT, error, event);
};

const addDepositWithdrawalListeners = () => {
const addListeners = () => {
l1Tokens.forEach(l1Token => {
const bridgeContract = getTokenBridgeContract(l1Token.bridgeAddress);
logger.log(`Add ${EventName.L1.LOG_DEPOSIT} listener for token ${l1Token.symbol}.`);
Expand Down Expand Up @@ -103,23 +101,14 @@ export const EventManagerProvider = ({children}) => {
};

const addContractEventListener = (contract, eventName, filter, handler) => {
emitters.push(
contract.events[eventName](
{
filter
},
handler
)
contract.events[eventName](
{
filter
},
handler
);
};

const cleanEmittersAndRemoveListeners = () => {
logger.log('Remove all listeners.');
emitters.forEach(emitter => emitter.removeAllListeners());
logger.log('Clean emitters.');
emitters = [];
};

const value = {
addListener,
getPastEvents,
Expand Down
8 changes: 4 additions & 4 deletions src/providers/TokensProvider/TokensProvider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PropTypes from 'prop-types';
import React, {useEffect, useReducer} from 'react';
import React, {useReducer} from 'react';

import {useConstants, useLogger} from '../../hooks';
import {useAccountChange, useConstants, useLogger} from '../../hooks';
import {useL1TokenBalance, useL2TokenBalance} from '../../hooks/useTokenBalance';
import {useL1Wallet, useL2Wallet} from '../WalletsProvider';
import {TokensContext} from './tokens-context';
Expand All @@ -16,9 +16,9 @@ export const TokensProvider = ({children}) => {
const getL1TokenBalance = useL1TokenBalance(l1Account);
const getL2TokenBalance = useL2TokenBalance(l2Account);

useEffect(() => {
useAccountChange(() => {
fetchBalances(tokens);
}, []);
});

const updateTokenBalance = symbol => {
logger.log(symbol ? `Update ${symbol} token balance` : 'Update all tokens balances');
Expand Down
9 changes: 3 additions & 6 deletions src/providers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ export const AppProviders = combineProviders([
ModalProvider,
WalletProvider,
WalletsProvider,
BlockHashProvider
]);

export const BridgeProviders = combineProviders([
TokensProvider,
EventManagerProvider,
TransfersLogProvider
EventManagerProvider
]);

export const BridgeProviders = combineProviders([BlockHashProvider, TransfersLogProvider]);

0 comments on commit 7fbd4fe

Please sign in to comment.