+
Skip to content

feat: return results on async requests #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/context/MoralisContext/MoralisContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export interface MoralisContextValue {
isInitializing: boolean;
initialize: (options?: { serverUrl?: string; appId?: string }) => void;

authenticate: (options?: AuthenticateOptions) => Promise<void>;
authenticate: (
options?: AuthenticateOptions,
) => Promise<MoralisType.User | undefined>;
logout: () => Promise<void>;
signup: Signup;
login: Login;
Expand All @@ -39,14 +41,16 @@ export interface MoralisContextValue {
isLoggingOut: boolean;
isAuthUndefined: boolean;

setUserData: (data: SetUserData) => Promise<void>;
setUserData: (data: SetUserData) => Promise<MoralisType.User | undefined>;
user: MoralisType.User | null;
_setUser: (user: MoralisType.User) => void;
userError: null | Error;
isUserUpdating: boolean;
refetchUserData: () => Promise<void>;
refetchUserData: () => Promise<MoralisType.User | undefined>;

enableWeb3: (options?: Web3EnableOptions) => void;
enableWeb3: (
options?: Web3EnableOptions,
) => Promise<MoralisType.Web3Provider | undefined>;
deactivateWeb3: () => Promise<void>;
web3: MoralisType.MoralisWeb3Provider | null;
isWeb3Enabled: boolean;
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/core/useMoralis/_useMoralisAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ export type Login = (
username: string,
password: string,
options?: LoginOptions,
) => Promise<void>;
) => Promise<MoralisType.User<MoralisType.Attributes> | undefined>;

export type Signup = (
username: string,
password: string,
email?: string,
otherFields?: SetUserData,
options?: SignupOptions,
) => Promise<void>;
) => Promise<MoralisType.User<MoralisType.Attributes> | undefined>;

export type OnAccountChanged = (account: string) => void;

Expand Down Expand Up @@ -172,6 +172,7 @@ export const _useMoralisAuth = (options: UseMoralisAuthOptions) => {
if (onSuccess) {
onSuccess(user);
}
return user;
} catch (error) {
setAuth({ state: AuthenticationState.ERROR, error });
setUser(null);
Expand Down Expand Up @@ -231,6 +232,7 @@ export const _useMoralisAuth = (options: UseMoralisAuthOptions) => {
if (onSuccess) {
onSuccess(user);
}
return user;
} catch (error) {
setAuth({ state: AuthenticationState.ERROR, error });
if (throwOnError) {
Expand Down Expand Up @@ -274,6 +276,7 @@ export const _useMoralisAuth = (options: UseMoralisAuthOptions) => {
if (onSuccess) {
onSuccess(user);
}
return user;
} catch (error) {
setAuth({ state: AuthenticationState.ERROR, error });
if (throwOnError) {
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/core/useMoralis/_useMoralisUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const _useMoralisUser = (Moralis: MoralisType) => {
if (onSuccess) {
onSuccess(user);
}
return user;
} catch (error) {
if (userHasLocallyUpdated) {
user.revert();
Expand Down Expand Up @@ -116,6 +117,7 @@ export const _useMoralisUser = (Moralis: MoralisType) => {
if (onSuccess) {
onSuccess(newUserData);
}
return newUserData;
} catch (error) {
setError(error);
if (throwOnError) {
Expand Down
13 changes: 10 additions & 3 deletions src/hooks/core/useMoralis/_useMoralisWeb3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ export interface Web3EnableOptions {
anyNetwork?: boolean;
}

type EnableWeb3 = (
options?: Web3EnableOptions | undefined,
) => Promise<MoralisType.Web3Provider | undefined>;

/**
* Handles enabling of web3 and providing it, as soon as the user is authenticated
*/
export const _useMoralisWeb3 = (
Moralis: MoralisType,
): {
enableWeb3: (options?: Web3EnableOptions) => Promise<void>;
enableWeb3: EnableWeb3;
web3: null | MoralisType.MoralisWeb3Provider;
isWeb3Enabled: boolean;
web3EnableError: Error | null;
Expand Down Expand Up @@ -99,7 +103,7 @@ export const _useMoralisWeb3 = (
/**
* Enable web3 with the browsers web3Provider (only available when a user has been authenticated)
*/
const enableWeb3 = useCallback(
const enableWeb3 = useCallback<EnableWeb3>(
async ({
throwOnError,
onComplete,
Expand All @@ -113,13 +117,16 @@ export const _useMoralisWeb3 = (
try {
// TODO: fix typechecking when passing ...rest
// @ts-ignore
const currentWeb3 = await Moralis.enableWeb3(rest);
const currentWeb3: MoralisType.Web3Provider = await Moralis.enableWeb3(
rest,
);

_setIsWeb3Enabled(true);

if (onSuccess) {
onSuccess(currentWeb3);
}
return currentWeb3;
} catch (error) {
setEnableWeb3Error(error);
if (throwOnError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const _useResolveCall = <Result, Params extends ResolveCallParams>(
if (onSuccess) {
onSuccess(results);
}
return results;
} catch (error) {
setData(initialData);
setError(error);
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载