Skip to content

Commit d098663

Browse files
authored
[y sweet] support optional initialClientToken for YSweetProvider (#319)
This updates the `YSweetProvider` (and the React `<YDocProvider>`) to take an optional `initialClientToken`. This can be used to cut out one server roundtrip if the client token is obtained at render time.
1 parent 17779f7 commit d098663

3 files changed

Lines changed: 232 additions & 7 deletions

File tree

js-pkg/client/src/provider.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ export type YSweetProviderParams = {
224224
resyncInterval?: number
225225
maxBackoffTime?: number
226226
disableBc?: boolean
227+
initialClientToken?: ClientToken
227228
}
228229

229230
/**
@@ -526,8 +527,10 @@ export async function ySweetProviderWrapper(
526527
authEndpoint: AuthEndpoint,
527528
roomname: string,
528529
doc: Y.Doc,
529-
providerParams: YSweetProviderParams = {},
530+
wrapperParams: YSweetProviderParams = {},
530531
): Promise<YSweetProviderWithClientToken> {
532+
let { initialClientToken, ...providerParams } = wrapperParams
533+
531534
// we use an observable that lives outside the provider to store event listeners
532535
// so that we can re-subscribe to events when the provider is re-created
533536
const observable = new Observable<string>()
@@ -538,7 +541,7 @@ export async function ySweetProviderWrapper(
538541
const awareness = providerParams.awareness ?? new awarenessProtocol.Awareness(doc)
539542
providerParams = { ...providerParams, awareness }
540543

541-
let _clientToken = await getClientToken(authEndpoint, roomname)
544+
let _clientToken = initialClientToken ?? (await getClientToken(authEndpoint, roomname))
542545
let _provider = new YSweetProvider(_clientToken.url, roomname, doc, {
543546
...updateProviderParams(providerParams, _clientToken),
544547
})

js-pkg/react/src/main.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
debuggerUrl,
88
} from '@y-sweet/client'
99
import type { AuthEndpoint, YSweetProviderWithClientToken } from '@y-sweet/client'
10+
import type { ClientToken } from '@y-sweet/sdk'
1011
import type { ReactNode } from 'react'
1112
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'
1213
import type { Awareness } from 'y-protocols/awareness'
@@ -171,22 +172,26 @@ export type YDocProviderProps = {
171172
/** The children to render. */
172173
children: ReactNode
173174

174-
/** Response of a `getConnectionKey` call, passed from server to client. */
175+
/** The doc id to use for the Y.Doc. */
175176
docId: string
176177

177-
/** The endpoint to use for authentication. */
178+
/** The endpoint to use for authentication or an async function that returns a client token. */
178179
authEndpoint: AuthEndpoint
179180

181+
/** An optional initial client token to use for the Y.Doc. This will be used initially, and if
182+
* the client token expires, the `authEndpoint` will be called to get a new client token. */
183+
initialClientToken?: ClientToken
184+
180185
/** If set to a string, the URL query parameter with this name
181-
* will be set to the doc id from connectionKey. */
186+
* will be set to the doc id provided. */
182187
setQueryParam?: string
183188
}
184189

185190
/**
186191
* A React component that provides a Y.Doc instance to its children given an auth endpoint and a doc id.
187192
*/
188193
export function YDocProvider(props: YDocProviderProps) {
189-
const { children, docId, authEndpoint } = props
194+
const { children, docId, authEndpoint, initialClientToken } = props
190195

191196
const [ctx, setCtx] = useState<YjsContextType | null>(null)
192197

@@ -197,6 +202,7 @@ export function YDocProvider(props: YDocProviderProps) {
197202

198203
;(async () => {
199204
provider = await createYjsProvider(doc, docId, authEndpoint, {
205+
initialClientToken,
200206
// TODO: this disables local cross-tab communication, which makes
201207
// debugging easier, but should be re-enabled eventually
202208
disableBc: true,

package-lock.json

Lines changed: 217 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)