Skip to content

Commit

Permalink
Make deposited event subscription #29
Browse files Browse the repository at this point in the history
  • Loading branch information
shingonu committed Mar 31, 2020
1 parent 008faa9 commit 760eb00
Showing 2 changed files with 29 additions and 4 deletions.
22 changes: 21 additions & 1 deletion src/layouts/MainLayout.vue
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
</template>

<script>
import { mapState } from 'vuex';
import WalletInfoContainer from '@/containers/WalletInfoContainer.vue';
import MenuContainer from '@/containers/MenuContainer.vue';
@@ -19,22 +20,41 @@ export default {
},
data () {
return {
depositedEventSubscription: null,
polling: null,
};
},
computed: {
...mapState([
'DepositManager',
]),
},
async created () {
this.poll();
this.subscribe();
},
beforeDestroy () {
clearInterval(this.polling);
this.depositedEventSubscription.unsubscribe();
},
methods: {
poll () {
this.polling = setInterval(() => {
if (this.$store.state.signIn) {
this.$store.dispatch('set');
}
}, 5000);
}, 5000); // 5s
},
subscribe () {
this.depositedEventSubscription = this.DepositManager.events.Deposited({
fromBlock: 'latest',
}, (error, event) => {
if (error) {
//
}
const result = event.returnValues;
this.$store.dispatch('addAccountDepositedWithPower', result.depositor);
});
},
},
};
11 changes: 8 additions & 3 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -160,9 +160,14 @@ export default new Vuex.Store({
SET_ACCOUNTS_DEPOSITED_WITH_POWER: (state, accounts) => {
state.accountsDepositedWithPower = accounts;
},
ADD_ACCOUNT_DEPOSITED_WITH_POWER: (state, account) => {
if (!state.accountsDepositedWithPower.find(a => a.address === account.address)) {
state.accountsDepositedWithPower.push(account);
ADD_ACCOUNT_DEPOSITED_WITH_POWER: (state, accountWithPower) => {
const findAccount = (account) => account.address.toLowerCase() === accountWithPower.address.toLowerCase();
const index = state.accountsDepositedWithPower.findIndex(findAccount);

if (index > -1) {
Vue.set(state.accountsDepositedWithPower, index, accountWithPower);
} else {
state.accountsDepositedWithPower.push(accountWithPower);
}
},
},

0 comments on commit 760eb00

Please sign in to comment.