Skip to content

Commit

Permalink
new: prevent user to create 2 comchain account with 2 fast button click
Browse files Browse the repository at this point in the history
  • Loading branch information
vaab committed Sep 16, 2024
1 parent 222a279 commit aea1c78
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/components/CreateAccount.vue
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
import AuthPref from "@/components/AuthPref.vue"
import PasswordField from "@/components/PasswordField.vue"
import { mapGetters } from "vuex"
import { debounceMethod } from "@/utils/debounce"
@Options({
name: "CreateAccount",
components: { AuthPref, PasswordField },
Expand Down Expand Up @@ -255,7 +256,7 @@
saveSimplifiedAuthPref(accountAuthService: any, userConfigInput: any) {
this.userAuthPref = [accountAuthService, userConfigInput]
},
async createUserAccount() {
createUserAccount: debounceMethod(async function() {
let userAccount
try {
userAccount = await this.$store.dispatch("createUserAccount", [
Expand Down Expand Up @@ -299,7 +300,7 @@
}
this.$store.dispatch("fetchComponentDefs")
this.$router.push({ name: "dashboard" })
},
}),
},
})
export default class CreateAccount extends Vue {}
Expand Down
37 changes: 37 additions & 0 deletions src/utils/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export function debounceMethod<
T extends (this: any, ...args: any[]) => Promise<any>
>(fn: T): T {
const promiseMapKey = Symbol("__debouncePromiseMap__")

return function (this: any, ...args: Parameters<T>): ReturnType<T> {
if (!this[promiseMapKey]) {
this[promiseMapKey] = new Map<string, ReturnType<T>>()
}

const argsKey = JSON.stringify(args)

if (!this[promiseMapKey].has(argsKey)) {
const promise = (async () => {
try {
return await fn.apply(this, args)
} finally {
this[promiseMapKey].delete(argsKey)
}
})()

this[promiseMapKey].set(argsKey, promise)
} else {
console.trace(`Debounced call`)
}

return this[promiseMapKey].get(argsKey)
} as unknown as T
}

export function debounce(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
return debounceMethod(descriptor.value)
}

0 comments on commit aea1c78

Please sign in to comment.