mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
wait for pervious query on debounve
This commit is contained in:
parent
22e58c9c0e
commit
522fd09761
@ -144,8 +144,8 @@ export function Create() {
|
|||||||
<Text style={styles.nocreatetext}>Create</Text>
|
<Text style={styles.nocreatetext}>Create</Text>
|
||||||
</View>
|
</View>
|
||||||
)}
|
)}
|
||||||
<TouchableOpacity style={styles.login} onPress={actions.create}>
|
<TouchableOpacity style={styles.login} onPress={actions.login}>
|
||||||
<Text style={styles.createtext}>Account Login</Text>
|
<Text style={styles.logintext}>Account Login</Text>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
</View>
|
</View>
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
|
@ -7,6 +7,7 @@ import { getUsername } from 'api/getUsername';
|
|||||||
|
|
||||||
export function useCreate() {
|
export function useCreate() {
|
||||||
|
|
||||||
|
const [count, setCount] = useState(0);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const app = useContext(AppContext);
|
const app = useContext(AppContext);
|
||||||
|
|
||||||
@ -29,6 +30,8 @@ export function useCreate() {
|
|||||||
usernameValid: false,
|
usernameValid: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const backoff = useRef(false);
|
||||||
|
const checking = useRef(false);
|
||||||
const debounce = useRef(null);
|
const debounce = useRef(null);
|
||||||
|
|
||||||
const updateState = (value) => {
|
const updateState = (value) => {
|
||||||
@ -51,19 +54,23 @@ export function useCreate() {
|
|||||||
}
|
}
|
||||||
}, [state]);
|
}, [state]);
|
||||||
|
|
||||||
const check = (server, token, username) => {
|
useEffect(() => {
|
||||||
|
if (checking.current) {
|
||||||
|
backoff.current = true;
|
||||||
|
}
|
||||||
if (debounce.current) {
|
if (debounce.current) {
|
||||||
clearTimeout(debounce.current);
|
clearTimeout(debounce.current);
|
||||||
}
|
}
|
||||||
debounce.current = setTimeout(async () => {
|
debounce.current = setTimeout(async () => {
|
||||||
|
checking.current = true;
|
||||||
debounce.current = null;
|
debounce.current = null;
|
||||||
if (server) {
|
if (state.server) {
|
||||||
try {
|
try {
|
||||||
const available = await getAvailable(server);
|
const available = await getAvailable(state.server);
|
||||||
if (available) {
|
if (available) {
|
||||||
if (username) {
|
if (state.username) {
|
||||||
try {
|
try {
|
||||||
const claimable = await getUsername(username, server, null);
|
const claimable = await getUsername(state.username, state.server, null);
|
||||||
updateState({ usernameChecked: true, tokenChecked: true, serverChecked: true, tokenRequired: false,
|
updateState({ usernameChecked: true, tokenChecked: true, serverChecked: true, tokenRequired: false,
|
||||||
usernameValid: claimable, serverValid: true });
|
usernameValid: claimable, serverValid: true });
|
||||||
}
|
}
|
||||||
@ -78,13 +85,13 @@ export function useCreate() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (token) {
|
if (state.token) {
|
||||||
try {
|
try {
|
||||||
const accessible = await getUsername(null, server, token);
|
const accessible = await getUsername(null, state.server, state.token);
|
||||||
if (accessible) {
|
if (accessible) {
|
||||||
if (username) {
|
if (state.username) {
|
||||||
try {
|
try {
|
||||||
const claimable = await getUsername(username, server, token);
|
const claimable = await getUsername(state.username, state.server, state.token);
|
||||||
updateState({ usernameChecked: true, tokenChecked: true, serverChecked: true, tokenRequired: true,
|
updateState({ usernameChecked: true, tokenChecked: true, serverChecked: true, tokenRequired: true,
|
||||||
usernameValid: claimable, tokenValid: true, serverValid: true });
|
usernameValid: claimable, tokenValid: true, serverValid: true });
|
||||||
}
|
}
|
||||||
@ -118,8 +125,14 @@ export function useCreate() {
|
|||||||
updateState({ usernameChecked: true, tokenChecked: true, serverChecked: true, serverValid: false });
|
updateState({ usernameChecked: true, tokenChecked: true, serverChecked: true, serverValid: false });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 1000);
|
let retry = backoff.current;
|
||||||
|
backoff.current = false;
|
||||||
|
checking.current = false;
|
||||||
|
if (retry) {
|
||||||
|
setCount(count++);
|
||||||
}
|
}
|
||||||
|
}, 1000);
|
||||||
|
}, [count]);
|
||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
config: () => {
|
config: () => {
|
||||||
@ -127,15 +140,15 @@ export function useCreate() {
|
|||||||
},
|
},
|
||||||
setServer: (server) => {
|
setServer: (server) => {
|
||||||
updateState({ server, serverChecked: false });
|
updateState({ server, serverChecked: false });
|
||||||
check(server, state.token, state.username);
|
setCount(count+1);
|
||||||
},
|
},
|
||||||
setToken: (token) => {
|
setToken: (token) => {
|
||||||
updateState({ token, tokenChecked: false });
|
updateState({ token, tokenChecked: false });
|
||||||
check(state.server, token, state.username);
|
setCount(count+1);
|
||||||
},
|
},
|
||||||
setUsername: (username) => {
|
setUsername: (username) => {
|
||||||
updateState({ username, usernameChecked: false });
|
updateState({ username, usernameChecked: false });
|
||||||
check(state.server, state.token, username);
|
setCount(count+1);
|
||||||
},
|
},
|
||||||
setPassword: (password) => {
|
setPassword: (password) => {
|
||||||
updateState({ password });
|
updateState({ password });
|
||||||
|
Loading…
Reference in New Issue
Block a user