Skip to content

Commit

Permalink
feat: move all methods commons to many ARS classes into a BaseARS class
Browse files Browse the repository at this point in the history
  • Loading branch information
0Lilian committed Sep 7, 2022
1 parent cb7b557 commit 50cbecc
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 73 deletions.
75 changes: 75 additions & 0 deletions src/composables/ars/base.js
@@ -0,0 +1,75 @@
import { getCurrentInstance, watch } from "vue";

export class BaseARS {

constructor () {
this._ars = {
oldEthersInstance: null,
events: {},
watchers: {},
}
}

_purgeARS () {
/*
ARS is ethersInstance targeted, it means that each time ethersInstance changes
the ARS is disabled for the old instance and enabled for the new one.
This _purgeARS() method is called at the top of _initARS() methods in order
to purge / disable the old ethersInstance ARS.
*/

// Unwatch watchers
if (this._ars.unwatchers) {
for (const unwatch of this._ars.unwatchers) {
unwatch()
}
}
this._ars.unwatchers = [];

if (this._ars.oldEthersInstance) {

// Unlisten events
if (this._ars.events) {
for (const [eventName, listener] of Object.entries(this._ars.events)) {
this._ars.oldEthersInstance.off(eventName, listener)
}
}
}
}

_initEthersInstanceARS () {

}

_initPlaceholderInstanceARS () {

}

_initARS () {
// 1) Purge old ethersInstance ARS
this._purgeARS();

// 2) Init ethersInstance ARS, if there is an ethersInstance
if (this.proxy.ethersInstance) {
this._initEthersInstanceARS()
}

// 3) Init placeholderInstance ARS
this._initPlaceholderInstanceARS()
}

onSafe (func) {
const component = getCurrentInstance();
if (this.isSafe.value) {
func(component)
}
else {
const unwatch = watch(this.isSafe, () => {
if (this.isSafe.value) {
func(component)
unwatch()
}
})
}
}
}
12 changes: 10 additions & 2 deletions src/composables/ars/contract.js
@@ -1,9 +1,14 @@
import { dapp, Status, OnContractReadSafe, OnContractWriteSafe } from "../../index.js";
import { computed, watch, getCurrentInstance, createVNode } from "vue";
import { BaseARS } from "./base.js";

export class ContractARS {
export class ContractARS extends BaseARS {

constructor (name) {
super();

this.name = name;

this.status = new Status(`contract:${name}`, [
"NO_PROVIDER", // Default status. If not changed it means that app don't have provider.
"UNAVAILABLE", // Set when contract is not available for the current network or not available at all.
Expand Down Expand Up @@ -52,7 +57,10 @@ export class ContractARS {
}
}

init() {
init () {
// Fill the oldEthersInstance instance used by ARS for purging
this._ars.oldEthersInstance = {...dapp.contracts[this.name].proxy.ethersInstance}

watch(dapp.signer.isSafe, (newValue, oldValue) => {
if (newValue !== oldValue) {
// Here the contract is removed and then recreated in order to fully destroy the old signer and provider.
Expand Down
8 changes: 7 additions & 1 deletion src/composables/ars/signer.js
@@ -1,9 +1,12 @@
import { dapp, Status, OnSignerSafe } from "../../index.js";
import { computed, watch, getCurrentInstance } from "vue";
import { BaseARS } from "./base.js";

export class SignerARS {
export class SignerARS extends BaseARS {

constructor () {
super();

this.status = new Status("signer", [
"NO_PROVIDER", // Default, unchanged if dapp.provider is not safe.
"DISCONNECTED", // Default status. Not changed if the DApp is not connected to any wallet when loading.
Expand All @@ -22,6 +25,9 @@ export class SignerARS {
}

init() {
// Fill the oldEthersInstance instance used by ARS for purging
this._ars.oldEthersInstance = {...dapp.signer.proxy.ethersInstance}

// 1) Auto-update status when provider status is WRONG, DISCONNECTED or in ERROR
dapp.provider.status.watchAny((status) => {
if (status === "WRONG_NETWORK") {
Expand Down
2 changes: 0 additions & 2 deletions src/composables/config/wallets-config.js
Expand Up @@ -35,8 +35,6 @@ export class WalletsConfig {
}

getCurrent () {
console.log("getCurrent")
console.log(dapp.signer.walletId)
if (dapp.signer.proxy.ethersInstance) {
return this.getById(dapp.signer.walletId)
}
Expand Down
69 changes: 1 addition & 68 deletions src/composables/proxies/proxy.js
Expand Up @@ -6,8 +6,6 @@ export class TulipeProxy {

constructor (ethersInstance=null, extensionInstance=null) {

this._ars = {}

const proxy = new Proxy(this, {
get: function(target, prop, receiver) {

Expand Down Expand Up @@ -84,9 +82,7 @@ export class TulipeProxy {
return this._ethersInstance;
},
set ethersInstance(instance) {
// Fill the oldEthersInstance instance used by ARS for purging
proxy._ars.oldEthersInstance = {...this._ethersInstance}


// Set the new ethersInstance
this._ethersInstance = instance ? markRaw(instance) : instance;

Expand All @@ -101,67 +97,4 @@ export class TulipeProxy {

return proxy;
}

_purgeARS () {
/*
ARS is ethersInstance targeted, it means that each time ethersInstance changes
the ARS is disabled for the old instance and enabled for the new one.
This _purgeARS() method is called at the top of _initARS() methods in order
to purge / disable the old ethersInstance ARS.
*/

// Unwatch watchers
if (this._ars.unwatchers) {
for (const unwatch of this._ars.unwatchers) {
unwatch()
}
}
this._ars.unwatchers = [];

if (this._ars.oldEthersInstance) {

// Unlisten events
if (this._ars.events) {
for (const [eventName, listener] of Object.entries(this._ars.events)) {
this._ars.oldEthersInstance.off(eventName, listener)
}
}
}
}

_initEthersInstanceARS () {

}

_initPlaceholderInstanceARS () {

}

_initARS () {
// 1) Purge old ethersInstance ARS
this._purgeARS();

// 2) Init ethersInstance ARS, if there is an ethersInstance
if (this.proxy.ethersInstance) {
this._initEthersInstanceARS()
}

// 3) Init placeholderInstance ARS
this._initPlaceholderInstanceARS()
}

onSafe (func) {
const component = getCurrentInstance();
if (this.isSafe.value) {
func(component)
}
else {
const unwatch = watch(this.isSafe, () => {
if (this.isSafe.value) {
func(component)
unwatch()
}
})
}
}
}

0 comments on commit 50cbecc

Please sign in to comment.