Add debounce to username available check
This commit is contained in:
parent
92f3b39b30
commit
6f02425ec8
|
|
@ -13,8 +13,6 @@
|
||||||
|
|
||||||
usernameValid = await isUsernameValid(username)
|
usernameValid = await isUsernameValid(username)
|
||||||
|
|
||||||
console.log(username, ' is valid: ', usernameValid)
|
|
||||||
|
|
||||||
if (usernameValid) {
|
if (usernameValid) {
|
||||||
usernameAvailable = await isUsernameAvailable(username)
|
usernameAvailable = await isUsernameAvailable(username)
|
||||||
console.log(username, ' is available: ', usernameAvailable)
|
console.log(username, ' is available: ', usernameAvailable)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
export function asyncDebounce<A extends unknown[], R>(
|
||||||
|
fn: (...args: A) => Promise<R>,
|
||||||
|
wait: number
|
||||||
|
): (...args: A) => Promise<R> {
|
||||||
|
let lastTimeoutId: ReturnType<typeof setTimeout> | undefined = undefined
|
||||||
|
|
||||||
|
return (...args: A): Promise<R> => {
|
||||||
|
clearTimeout(lastTimeoutId)
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const currentTimeoutId = setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
if (currentTimeoutId === lastTimeoutId) {
|
||||||
|
const result = await fn(...args)
|
||||||
|
resolve(result)
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
|
}, wait)
|
||||||
|
|
||||||
|
lastTimeoutId = currentTimeoutId
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ import * as webnative from 'webnative'
|
||||||
import type FileSystem from 'webnative/fs/index'
|
import type FileSystem from 'webnative/fs/index'
|
||||||
import { USE_WNFS_IMPLEMENTATION } from 'webnative/auth/implementation/use-wnfs'
|
import { USE_WNFS_IMPLEMENTATION } from 'webnative/auth/implementation/use-wnfs'
|
||||||
import { setup } from 'webnative'
|
import { setup } from 'webnative'
|
||||||
|
import { asyncDebounce } from '$lib/common/utils'
|
||||||
|
|
||||||
// runfission.net = staging
|
// runfission.net = staging
|
||||||
setup.endpoints({ api: 'https://runfission.net', user: 'fissionuser.net' })
|
setup.endpoints({ api: 'https://runfission.net', user: 'fissionuser.net' })
|
||||||
|
|
@ -61,9 +62,12 @@ export const isUsernameValid = async (username: string): Promise<boolean> => {
|
||||||
return webnative.account.isUsernameValid(username)
|
return webnative.account.isUsernameValid(username)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const debouncedIsUsernameAvailable = asyncDebounce(webnative.account.isUsernameAvailable, 300)
|
||||||
|
|
||||||
export const isUsernameAvailable = async (username: string): Promise<boolean> => {
|
export const isUsernameAvailable = async (username: string): Promise<boolean> => {
|
||||||
return webnative.account.isUsernameAvailable(username)
|
return debouncedIsUsernameAvailable(username)
|
||||||
}
|
}
|
||||||
|
|
||||||
// interface StateFS {
|
// interface StateFS {
|
||||||
// fs?: FileSystem
|
// fs?: FileSystem
|
||||||
// }
|
// }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue