Skip to content
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

RJS-2867: Add useProgress and tests #6804

Merged
merged 22 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use default context for exports
  • Loading branch information
gagik committed Aug 4, 2024
commit 8fbafb0b00ef4fedc03ddc2eb09a1c71bf6d37a5
2 changes: 1 addition & 1 deletion packages/realm-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { RealmProvider, ProgressDirection, ProgressMode } from "@realm/react";
const ProgressText = () => {
const progress = useProgress({ direction: ProgressDirection.Download, mode: ProgressMode.ReportIndefinitely });

return <Text>Loading: {progress}/1.0 Downloaded</Text>;
return <Text>Loading: {(100 * progress).toFixed()}%</Text>;
}

const MyApp() = () => {
Expand Down
18 changes: 18 additions & 0 deletions packages/realm-react/src/RealmContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from "./RealmProvider";
import { createContext } from "react";
import Realm from "realm";
import { createUseProgress } from "./useProgress";

export type RealmContext<RealmProvider = DynamicRealmProvider> = {
/**
Expand Down Expand Up @@ -112,6 +113,21 @@ export type RealmContext<RealmProvider = DynamicRealmProvider> = {
* @returns either the desired {@link Realm.Object} or `null` in the case of it being deleted or not existing.
*/
useObject: ReturnType<typeof createUseObject>;
/**
* Returns the value representing the progress for a given {@link Realm.ProgressDirection}
* and {@link Realm.ProgressMode}. The hook will register a progress notifier and update from
* any changes from it.
* @example
* ```
* const progress = useProgress({ direction: ProgressDirection.Download, mode: ProgressMode.ReportIndefinitely });
* return <Text>Loading: {(100 * progress).toFixed()}%</Text>;
* ```
* @param options
* @param options.direction - The {@link Realm.ProgressDirection} for the progress notifier.
* @param options.mode - The {@link Realm.ProgressMode} for the progress notifier.
* @returns a number between 0 and 1 representing the progress
*/
useProgress: ReturnType<typeof createUseProgress>;
};

/**
Expand Down Expand Up @@ -186,11 +202,13 @@ export function createRealmContext(
const useRealm = createUseRealm(RealmContext);
const useQuery = createUseQuery(useRealm);
const useObject = createUseObject(useRealm);
const useProgress = createUseProgress(useRealm);

return {
RealmProvider,
useRealm,
useQuery,
useObject,
useProgress,
};
}
16 changes: 15 additions & 1 deletion packages/realm-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ export const useQuery = defaultContext.useQuery;
* @returns either the desired {@link Realm.Object} or `null` in the case of it being deleted or not existing.
*/
export const useObject = defaultContext.useObject;
/**
* Returns the value representing the progress for a given {@link Realm.ProgressDirection}
* and {@link Realm.ProgressMode}. The hook will register a progress notifier and update from
* any changes from it.
* @example
* ```
* const progress = useProgress({ direction: ProgressDirection.Download, mode: ProgressMode.ReportIndefinitely });
* return <Text>Loading: {(100 * progress).toFixed()}%</Text>;
* ```
* @param options
* @param options.direction - The {@link Realm.ProgressDirection} for the progress notifier.
* @param options.mode - The {@link Realm.ProgressMode} for the progress notifier.
* @returns a number between 0 and 1 representing the progress
*/
export const useProgress = defaultContext.useProgress;

/*
* @ignore This will end up documenting all of Realm, which is documented elsewhere
Expand All @@ -130,4 +145,3 @@ export { useUser, UserProvider } from "./UserProvider";
export * from "./useAuth";
export * from "./useEmailPasswordAuth";
export * from "./types";
export * from "./useProgress";
36 changes: 17 additions & 19 deletions packages/realm-react/src/useProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,33 @@
////////////////////////////////////////////////////////////////////////////

import { ProgressDirection, ProgressMode } from "realm";
import { useRealm } from ".";
import { useEffect, useState } from "react";
import { EstimateProgressNotificationCallback } from "realm/dist/public-types/internal";
import { UseRealmHook } from "./useRealm";

type UserProgressHook = {
direction: ProgressDirection;
mode: ProgressMode;
};

/**
*
* @returns An object containing operations and state for authenticating with an Atlas App.
*/
export function useProgress({ direction, mode }: UserProgressHook): number | null {
const realm = useRealm();
const [progress, setProgress] = useState<number | null>(null);
export function createUseProgress(useRealm: UseRealmHook): ({ direction, mode }: UserProgressHook) => number | null {
return function useProgress({ direction, mode }: UserProgressHook): number | null {
const realm = useRealm();
const [progress, setProgress] = useState<number | null>(null);

useEffect(() => {
if (!realm.syncSession) {
throw new Error("No sync session found.");
}
const callback: EstimateProgressNotificationCallback = (estimate) => {
setProgress(estimate);
};
useEffect(() => {
if (!realm.syncSession) {
throw new Error("No sync session found.");
}
const callback: EstimateProgressNotificationCallback = (estimate) => {
setProgress(estimate);
};

realm.syncSession.addProgressNotification(direction, mode, callback);
realm.syncSession.addProgressNotification(direction, mode, callback);

return () => realm.syncSession?.removeProgressNotification(callback);
}, [realm, direction, mode]);
return () => realm.syncSession?.removeProgressNotification(callback);
}, [realm, direction, mode]);

return progress;
return progress;
};
}
Loading