Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move all methods commons to many ARS classes into a BaseARS class
- Loading branch information
0Lilian
committed
Sep 7, 2022
1 parent
cb7b557
commit 50cbecc
Showing
5 changed files
with
93 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters