Skip to content

Commit fd09c41

Browse files
authored
support on && removeListener event for vsys (#31)
* support on && removeListener event for vsys * extends EventEmitter * update README.md for new features `on` and `removeListener`
1 parent 393302d commit fd09c41

File tree

6 files changed

+88
-5
lines changed

6 files changed

+88
-5
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,39 @@ GeneralContract
213213

214214
If developers want to get user NFT list, he/she can filter the result with `NonFungibleContract` type only to get the user's NFT list.
215215

216+
### Listen for AccountsChanged and ChainChanged Events
217+
218+
The JS sample code of listening
219+
220+
```javascript
221+
//Accounts Changed
222+
window.vsys.on('accountsChanged', (accounts) => {
223+
// Handle the new accounts, or lack thereof.
224+
// "accounts" will always be an array, but it can be empty.
225+
});
226+
227+
// Chain Changed
228+
window.vsys.on('chainChanged', (chain) => {
229+
// Handle the new chain.
230+
// "chain" is an object which includes `networkType` and `nodeUrl` two keys
231+
});
232+
```
233+
234+
Also, don't forget to remove listeners once you are done listening to them:
235+
236+
```javascript
237+
function handleAccountsChanged(accounts) {
238+
// ...
239+
}
240+
241+
window.vsys.on('accountsChanged', handleAccountsChanged);
242+
243+
// Later
244+
245+
window.vsys.removeListener('accountsChanged', handleAccountsChanged);
246+
```
247+
248+
216249
### Add Token to Watch List of Extension Wallet
217250

218251
The JS sample code of request:

src/components/NavBar.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ export default {
216216
return amount
217217
},
218218
select(index) {
219-
this.$store.commit('account/updateSelectedAccount', index)
219+
const newAccountInfo = { 'index': index, 'address': this.addresses[index]}
220+
this.$store.commit('account/updateSelectedAccount', newAccountInfo)
220221
},
221222
changePage(data) {
222223
this.$refs.popover.$emit('close')

src/content/content.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ function listenForProviderRequest () {
4141
})
4242
}
4343

44+
chrome.runtime.onMessage.addListener(
45+
function(request, sender) {
46+
if (sender.tab || !request.data || !request.notification) return
47+
switch (request.notification) {
48+
case 'accountsChanged': case 'chainChanged':
49+
injectScript(`window.vsys.emit('${request.notification}', ${ JSON.stringify(request.data)})`)
50+
break
51+
}
52+
}
53+
);
54+
4455
listenForProviderRequest()
4556

4657
function getSiteName (window) {

src/inpage/inpage.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11

22

33
let providerHandle
4+
let EventEmitter = require("events").EventEmitter
45
const methodType = ["address", "publicKey", "amount", "tokenAmount", "watchedTokens", "depositToken", "withdrawToken", "lockToken", "send", "addToken",'webList', "info", "sendNFT", "execContractFunc", "signContent", "regContract"]
5-
class vsys {
6+
// const vsysEventEmitter = new EventEmitter()
7+
// const eventType = ["accountsChanged", "chainChanged"]
8+
// function vsysEventEmitterHandler({detail}) {
9+
// vsysEventEmitter.emit(detail.notification, detail.data)
10+
// }
611

12+
class vsys extends EventEmitter{
713
constructor() {
8-
this.isInstalled = true
14+
super();
15+
this.isInstalled = true;
916
}
1017

1118
request(data) {
@@ -42,5 +49,18 @@ class vsys {
4249
window.postMessage(message, "*")
4350
})
4451
}
52+
// on(eventName, callBack) {
53+
// if (!eventType.includes(eventName)) {
54+
// return { "result": false, "message": "Invalid event" }
55+
// }
56+
// vsysEventEmitter.on(eventName, callBack)
57+
// let listenerName = "vsys-on-" + eventName
58+
// window.removeEventListener(listenerName, vsysEventEmitterHandler)
59+
// window.addEventListener(listenerName, vsysEventEmitterHandler)
60+
// }
61+
// removeListener(eventName, callBack) {
62+
// let listenerName = "vsys-on-" + eventName
63+
// vsysEventEmitter.removeListener(eventName, callBack)
64+
// }
4565
}
4666
window.vsys = new vsys()

src/store/API.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,20 @@ export default {
1111
},
1212
mutations: {
1313
updateAPI(state, object) {
14+
let isChanged = JSON.stringify(state.chain) !== "{}" && state.chain.host_ip !== object.nodeUrl
1415
state.chain = new Blockchain(object.nodeUrl, object.networkByte)
1516
state.account = new Account(object.networkByte)
17+
if (isChanged) {
18+
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
19+
chrome.tabs.sendMessage(tabs[0].id, {
20+
notification: "chainChanged",
21+
data: {
22+
nodeUrl: object.nodeUrl,
23+
networkType: String.fromCharCode(object.networkByte)
24+
}
25+
});
26+
});
27+
}
1628
}
1729
}
1830
}

src/store/account.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ export default {
1313
addAccount(state, account) {
1414
state.accountNames.push(account)
1515
},
16-
updateSelectedAccount(state, index) {
17-
state.selectedAccount = index
16+
updateSelectedAccount(state, newAccountInfo) {
17+
state.selectedAccount = newAccountInfo.index
18+
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
19+
chrome.tabs.sendMessage(tabs[0].id, {
20+
notification: "accountsChanged",
21+
data: [newAccountInfo.address]
22+
});
23+
});
1824
},
1925
updateToken(state, tokenRecordsInfo) {
2026
if (String.fromCharCode(tokenRecordsInfo.networkByte) === 'M') {

0 commit comments

Comments
 (0)