Skip to content

Commit

Permalink
Refactor DocumentProvider and useDocument for improved type safety an…
Browse files Browse the repository at this point in the history
…d clarity
  • Loading branch information
hackerwins committed Mar 6, 2025
1 parent 1e37976 commit a055289
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
7 changes: 3 additions & 4 deletions examples/react-todomvc/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import 'todomvc-app-css/index.css';
* `App` is the root component of the application.
*/
export default function App() {
const { root, update, loading, error } = useDocument<
{ todos: JSONArray<Todo> },
any
>();
const { root, update, loading, error } = useDocument<{
todos: JSONArray<Todo>;
}>();

if (loading) return <div>Loading...</div>;
if (error) return <div>Error: {error.message}</div>;
Expand Down
25 changes: 14 additions & 11 deletions packages/react/src/DocumentProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@
*/

import { createContext, useContext, useEffect, useState } from 'react';
import { Document, Indexable } from '@yorkie-js/sdk';
import { Document, Presence, Indexable } from '@yorkie-js/sdk';
import { useYorkie } from './YorkieProvider';

type DocumentContextType<R, P> = {
type DocumentContextType<R, P extends Indexable = Indexable> = {
root: R;
presences: { clientID: string; presence: P }[];
update: (callback: (root: R) => void) => void;
loading: boolean;
error: Error | undefined;
};

const DocumentContext = createContext<DocumentContextType<any, any> | null>(
null,
);
const DocumentContext = createContext<DocumentContextType<any> | null>(null);

export const DocumentProvider = <R, P extends Indexable>({
/**
* `DocumentProvider` is a component that provides a document to its children.
* This component must be under a `YorkieProvider` component to initialize the
* Yorkie client properly.
*/
export const DocumentProvider = <R, P extends Indexable = Indexable>({
docKey,
initialRoot,
children,
Expand Down Expand Up @@ -85,7 +88,7 @@ export const DocumentProvider = <R, P extends Indexable>({
};
}, [client, docKey]);

const update = (callback: (root: any) => void) => {
const update = (callback: (root: R, presence: Presence<P>) => void) => {
if (doc) {
doc.update(callback);
}
Expand All @@ -104,7 +107,7 @@ export const DocumentProvider = <R, P extends Indexable>({
* `useDocument` is a custom hook that returns the root object and update function of the document.
* This hook must be used within a `DocumentProvider`.
*/
export const useDocument = <R, P extends Indexable>() => {
export const useDocument = <R, P extends Indexable = Indexable>() => {
const context = useContext(DocumentContext);
if (!context) {
throw new Error('useDocument must be used within a DocumentProvider');
Expand Down Expand Up @@ -133,12 +136,12 @@ export const useRoot = () => {
};

/**
* `usePresence` is a custom hook that returns the presence of the document.
* `usePresences` is a custom hook that returns the presences of the document.
*/
export const usePresence = () => {
export const usePresences = () => {
const context = useContext(DocumentContext);
if (!context) {
throw new Error('usePresence must be used within a DocumentProvider');
throw new Error('usePresences must be used within a DocumentProvider');
}
return context.presences;
};
1 change: 1 addition & 0 deletions packages/react/src/YorkieProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface YorkieProviderProps {

/**
* `YorkieProvider` is a component that provides the Yorkie client to its children.
* It initializes the Yorkie client with the given API key and RPC address.
*/
export const YorkieProvider: React.FC<
PropsWithChildren<YorkieProviderProps>
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
DocumentProvider,
useDocument,
useRoot,
usePresence,
usePresences,
} from './DocumentProvider';

export type { JSONArray, JSONObject };
Expand All @@ -31,6 +31,6 @@ export {
DocumentProvider,
useDocument,
useRoot,
usePresence,
usePresences,
useYorkieDoc,
};
1 change: 1 addition & 0 deletions packages/sdk/src/yorkie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export {
Document,
type ChangeInfo,
} from '@yorkie-js-sdk/src/document/document';
export { Presence } from '@yorkie-js-sdk/src/document/presence/presence';
export {
type Observer,
type Observable,
Expand Down

0 comments on commit a055289

Please sign in to comment.