diff --git a/src/composables/ars/base.js b/src/composables/ars/base.js new file mode 100644 index 00000000..26815eda --- /dev/null +++ b/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() + } + }) + } + } +} diff --git a/src/composables/ars/contract.js b/src/composables/ars/contract.js index e3e91da2..69a1a4d0 100644 --- a/src/composables/ars/contract.js +++ b/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. @@ -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. diff --git a/src/composables/ars/signer.js b/src/composables/ars/signer.js index 55f174c7..3d41b8fc 100644 --- a/src/composables/ars/signer.js +++ b/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. @@ -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") { diff --git a/src/composables/config/wallets-config.js b/src/composables/config/wallets-config.js index f8e76a9e..86801beb 100644 --- a/src/composables/config/wallets-config.js +++ b/src/composables/config/wallets-config.js @@ -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) } diff --git a/src/composables/proxies/proxy.js b/src/composables/proxies/proxy.js index bb1e14a0..3e611c61 100644 --- a/src/composables/proxies/proxy.js +++ b/src/composables/proxies/proxy.js @@ -6,8 +6,6 @@ export class TulipeProxy { constructor (ethersInstance=null, extensionInstance=null) { - this._ars = {} - const proxy = new Proxy(this, { get: function(target, prop, receiver) { @@ -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; @@ -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() - } - }) - } - } }