Skip to content

Commit c90e603

Browse files
authored
Merge pull request #710 from TokenScript/OH_fix_loop
fix: 🐛 limited event "tokensupdated" when token really updated
2 parents 490eebe + e365cb5 commit c90e603

File tree

7 files changed

+37
-20
lines changed

7 files changed

+37
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
> Description
22
3-
This feature release adds support for Ethereum Attestation Service (EAS) off chain attestations.
3+
This patch release addresses an off chain token management scenario.
44

55
### Upgrade Steps
66

7-
* Update NPM package to version 2.7.0
7+
* Update NPM package to version 2.7.1
88

99
### Breaking Changes
1010

1111
[none]
1212

1313
### New Features
1414

15-
* EAS off chain attestation support
16-
* Opening Start Screen default copy change
17-
* User controlled off chain signed token support / enhanced user control of token
15+
[none]
1816

1917
### Bug Fixes
2018

21-
* Added attestation backwards compatability
22-
* CSS alignment correction for Wallect Connect Version 2 icon
19+
* When off chain tokens are added to the application a fix has been added to stop the 'tokensupdated' event hook from triggering when these tokens already existed.
20+
* Addition of this hook to the README documentation.
2321

2422
### Performance Improvements
2523

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,19 @@ Changing the theme.
497497
negotiator.switchTheme("dark");
498498
```
499499

500+
### Tokens Updated Hook
501+
502+
Detect when new tokens have been added to the Token Negotiator during an applications lifecycle (for off chain tokens at this time).
503+
504+
```javascript
505+
506+
// temporary solution likely to change in the next major release version.
507+
document.body.addEventListener("tokensupdated", () => {
508+
console.log("Tokens updated event fired!!");
509+
});
510+
511+
```
512+
500513
### When working without NPM
501514

502515
For projects where you are not using a Node.js work flow. Or would prefer to inject the library into the html (polyfills included).

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tokenscript/token-negotiator",
3-
"version": "2.7.0",
3+
"version": "2.7.1",
44
"description": "Token-negotiator a token attestation bridge between web 2.0 and 3.0.",
55
"module": "dist/index.js",
66
"types": "dist/index.d.ts",

src/outlet/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,13 @@ export class Outlet {
228228

229229
public async readMagicLink() {
230230
try {
231-
await this.ticketStorage.importTicketFromMagicLink(this.urlParams)
232-
233-
const event = new Event('tokensupdated')
234-
235-
document.body.dispatchEvent(event)
231+
if (await this.ticketStorage.importTicketFromMagicLink(this.urlParams)) {
232+
const event = new Event('tokensupdated')
233+
document.body.dispatchEvent(event)
234+
// TODO: tokens could be read from the hooks "tokens" and "tokens-selected" by
235+
// triggering await this.sendTokens(this.getDataFromQuery('evtid')) at this point instead.
236+
// Let's review this approach to confirm if this hook is required.
237+
}
236238
} catch (e) {
237239
logger(2, e)
238240
}

src/outlet/ticketStorage.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class TicketStorage {
6666
this.loadTickets()
6767
}
6868

69-
public async importTicketFromMagicLink(urlParams: URLSearchParams) {
69+
public async importTicketFromMagicLink(urlParams: URLSearchParams): Promise<boolean> {
7070
const tokenFromQuery = decodeURIComponent(urlParams.get(this.config.tokenUrlName))
7171
const secretFromQuery = urlParams.get(this.config.tokenSecretName)
7272
const idFromQuery = urlParams.has(this.config.tokenIdName) ? urlParams.get(this.config.tokenIdName) : ''
@@ -76,7 +76,7 @@ export class TicketStorage {
7676

7777
const tokenData = await this.decodeTokenData(typeFromQuery, tokenFromQuery, true)
7878

79-
await this.updateOrInsertTicket({
79+
return await this.updateOrInsertTicket({
8080
type: typeFromQuery,
8181
token: tokenFromQuery,
8282
id: idFromQuery,
@@ -181,7 +181,7 @@ export class TicketStorage {
181181
return obj
182182
}
183183

184-
private async updateOrInsertTicket(tokenRecord: StoredTicketRecord) {
184+
private async updateOrInsertTicket(tokenRecord: StoredTicketRecord): Promise<boolean> {
185185
for (const [index, ticket] of this.tickets.entries()) {
186186
// Backward compatibility with old data
187187
if (!ticket.tokenId || !ticket.type) {
@@ -190,13 +190,17 @@ export class TicketStorage {
190190
}
191191

192192
if (ticket.tokenId === tokenRecord.tokenId) {
193+
if (JSON.stringify(tokenRecord) === JSON.stringify(this.tickets[index])) {
194+
return false
195+
}
193196
this.tickets[index] = tokenRecord
194197
this.storeTickets()
195-
return
198+
return true
196199
}
197200
}
198201

199202
this.addTicket(tokenRecord)
203+
return true
200204
}
201205

202206
/**

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// modified by build process.
2-
export const VERSION = '2.6.0'
2+
export const VERSION = '2.7.1'

0 commit comments

Comments
 (0)