From e1afb4e5710c1ab8a8cffbd3363ad1cbf369c6e5 Mon Sep 17 00:00:00 2001 From: Ben Foxall Date: Sun, 22 Sep 2024 12:34:20 +0000 Subject: [PATCH] Retrieve ice candidates This was hard! :sweat: Breaking on caught exceptions helped --- src/components/Editor.tsx | 17 ++++++++++------- src/yconfig.ts | 32 ++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/components/Editor.tsx b/src/components/Editor.tsx index 10a7a0a..f673e8a 100644 --- a/src/components/Editor.tsx +++ b/src/components/Editor.tsx @@ -67,13 +67,16 @@ export const Editor: FC<{ _SET_MONACO(mon); - new MonacoBinding( - type, - model, - new Set([editor]), - yconfig.provider.awareness, - ); - + yconfig.provider.then(provider => { + new MonacoBinding( + type, + model, + new Set([editor]), + provider.awareness, + ); + }) + + editor.setModel(model); return () => { diff --git a/src/yconfig.ts b/src/yconfig.ts index 1e0d536..679fecb 100644 --- a/src/yconfig.ts +++ b/src/yconfig.ts @@ -8,7 +8,7 @@ if (import.meta.hot) { interface YI { doc: Y.Doc; - provider: WebrtcProvider; + provider: Promise; room: string; initiator: boolean; } @@ -34,9 +34,33 @@ const doc = new Y.Doc(); const [name, password] = room.split('~'); -// @ts-expect-error -// WebrtcProvider expects full Opts object, though it seems that Partial works okay -const provider = new WebrtcProvider(name, doc, { password, signaling: ['wss://y-ben.fly.dev'] }); +const provider = fetch('https://nice.benfoxall.workers.dev/') + .then((res) => res.json()) + .then((config) => { + // correct iterable ice servers + if (config?.iceServers && !Array.isArray(config?.iceServers)) { + console.log('correcting ice servers'); + config.iceServers = [config.iceServers]; + } + + // nest for peerOpts + return config; + }) + .catch(() => undefined) + .then((config) => { + // @ts-expect-error + // WebrtcProvider expects full Opts object, though it seems that Partial works okay + const provider = new WebrtcProvider(name, doc, { + password: null, + signaling: ['wss://y-ben.fly.dev'], + filterBcConns: false, + peerOpts: config ? { config } : undefined, + }); + + console.log('INIT PROVIDER', provider); + + return provider; + }); const config: YI = { room, doc, provider, initiator };