diff --git a/.gitignore b/.gitignore index 8349a57..200a205 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules dist -package-lock.json \ No newline at end of file +package-lock.json +**.tgz \ No newline at end of file diff --git a/examples/react-example/src/App.tsx b/examples/react-example/src/App.tsx index d6b670f..276f340 100644 --- a/examples/react-example/src/App.tsx +++ b/examples/react-example/src/App.tsx @@ -5,7 +5,7 @@ import { SocketIOProvider } from 'y-socket.io'; function App() { const [doc, setDoc] = useState(null); const [provider, setProvider] = useState(null); - const [connected, setConnected] = useState(false); + const [connected, setConnected] = useState(true); const [input, setInput] = useState(''); const [clients, setClients] = useState([]); @@ -15,7 +15,6 @@ function App() { const _doc = new Y.Doc(); const yMap = _doc.getMap('data'); if (!yMap.has('input')) { - yMap.set('input', ''); yMap.observe((event, transaction) => { setInput(yMap.get('input') as string); }); @@ -49,6 +48,12 @@ function App() { if (!provider) return

Initializing provider...

; + const onChange: React.ChangeEventHandler = (e) => { + if (!doc) return; + const yMap = doc.getMap('data'); + yMap.set('input', e.target.value ?? '') + } + return (
App @@ -59,13 +64,13 @@ function App() { ? <> - : !!doc &&
+ : !!doc &&
-                { JSON.stringify(clients, null, 4) }
+                {JSON.stringify(clients, null, 4)}
               
doc.getMap('data').set('input', e.target.value ?? '')} + onChange={onChange} />
diff --git a/src/client/provider.ts b/src/client/provider.ts index 4c587f8..c0ecfc4 100644 --- a/src/client/provider.ts +++ b/src/client/provider.ts @@ -246,6 +246,7 @@ export class SocketIOProvider extends Observable { if (!this.socket.connected) { this.emit('status', [{ status: 'connecting' }]) this.socket.connect() + if (!this.disableBc) this.connectBc() this.synced = false } } @@ -266,7 +267,6 @@ export class SocketIOProvider extends Observable { Y.applyUpdate(this.doc, new Uint8Array(update), this) }) if (this.awareness.getLocalState() !== null) this.socket.emit('awareness-update', AwarenessProtocol.encodeAwarenessUpdate(this.awareness, [this.doc.clientID])) - if (!this.disableBc) this.connectBc() if (resyncInterval > 0) { this.resyncInterval = setInterval(() => { if (this.socket.disconnected) return @@ -342,7 +342,7 @@ export class SocketIOProvider extends Observable { bc.publish(this._broadcastChannel, { type: 'sync-update', data: update - }) + }, this) } } } @@ -395,6 +395,7 @@ export class SocketIOProvider extends Observable { this.bcconnected = true } bc.publish(this._broadcastChannel, { type: 'sync-step-1', data: Y.encodeStateVector(this.doc) }, this) + bc.publish(this._broadcastChannel, { type: 'sync-step-2', data: Y.encodeStateAsUpdate(this.doc) }, this) bc.publish(this._broadcastChannel, { type: 'query-awareness', data: null }, this) bc.publish(this._broadcastChannel, { type: 'awareness-update', data: AwarenessProtocol.encodeAwarenessUpdate(this.awareness, [this.doc.clientID]) }, this) }