From 9d2ed3fc50824bdaa7dcceaccb52ea7585d5bf78 Mon Sep 17 00:00:00 2001 From: Taylor Baldwin Date: Tue, 15 Oct 2024 14:27:51 -0400 Subject: [PATCH] update react package with initialClientToken option --- js-pkg/react/src/main.tsx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/js-pkg/react/src/main.tsx b/js-pkg/react/src/main.tsx index a57d2045..dffd8d81 100644 --- a/js-pkg/react/src/main.tsx +++ b/js-pkg/react/src/main.tsx @@ -7,6 +7,7 @@ import { debuggerUrl, } from '@y-sweet/client' import type { AuthEndpoint, YSweetProviderWithClientToken } from '@y-sweet/client' +import type { ClientToken } from '@y-sweet/sdk' import type { ReactNode } from 'react' import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react' import type { Awareness } from 'y-protocols/awareness' @@ -171,14 +172,18 @@ export type YDocProviderProps = { /** The children to render. */ children: ReactNode - /** Response of a `getConnectionKey` call, passed from server to client. */ + /** The doc id to use for the Y.Doc. */ docId: string - /** The endpoint to use for authentication. */ + /** The endpoint to use for authentication or an async function that returns a client token. */ authEndpoint: AuthEndpoint + /** An optional initial client token to use for the Y.Doc. This will be used initially, and if + * the client token expires, the `authEndpoint` will be called to get a new client token. */ + initialClientToken?: ClientToken + /** If set to a string, the URL query parameter with this name - * will be set to the doc id from connectionKey. */ + * will be set to the doc id provided. */ setQueryParam?: string } @@ -186,7 +191,7 @@ export type YDocProviderProps = { * A React component that provides a Y.Doc instance to its children given an auth endpoint and a doc id. */ export function YDocProvider(props: YDocProviderProps) { - const { children, docId, authEndpoint } = props + const { children, docId, authEndpoint, initialClientToken } = props const [ctx, setCtx] = useState(null) @@ -197,6 +202,7 @@ export function YDocProvider(props: YDocProviderProps) { ;(async () => { provider = await createYjsProvider(doc, docId, authEndpoint, { + initialClientToken, // TODO: this disables local cross-tab communication, which makes // debugging easier, but should be re-enabled eventually disableBc: true,