Skip to content

Commit

Permalink
Add "status" event and better typings for observable. Implements #64
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Dec 28, 2023
1 parent c7489c9 commit 8cb45d9
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions src/y-webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as error from 'lib0/error'
import * as random from 'lib0/random'
import * as encoding from 'lib0/encoding'
import * as decoding from 'lib0/decoding'
import { Observable } from 'lib0/observable'
import { ObservableV2 } from 'lib0/observable'
import * as logging from 'lib0/logging'
import * as promise from 'lib0/promise'
import * as bc from 'lib0/broadcastchannel'
Expand Down Expand Up @@ -342,9 +342,9 @@ export class Room {
* Listens to Yjs updates and sends them to remote peers
*
* @param {Uint8Array} update
* @param {any} origin
* @param {any} _origin
*/
this._docUpdateHandler = (update, origin) => {
this._docUpdateHandler = (update, _origin) => {
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, messageSync)
syncProtocol.writeUpdate(encoder, update)
Expand All @@ -354,9 +354,9 @@ export class Room {
* Listens to Awareness updates and sends them to remote peers
*
* @param {any} changed
* @param {any} origin
* @param {any} _origin
*/
this._awarenessUpdateHandler = ({ added, updated, removed }, origin) => {
this._awarenessUpdateHandler = ({ added, updated, removed }, _origin) => {
const changedClients = added.concat(updated).concat(removed)
const encoderAwareness = encoding.createEncoder()
encoding.writeVarUint(encoderAwareness, messageAwareness)
Expand Down Expand Up @@ -570,9 +570,25 @@ export class SignalingConn extends ws.WebsocketClient {
*/

/**
* @extends Observable<string>
* @param {WebrtcProvider} provider
*/
const emitStatus = provider => {
provider.emit('status', [{
connected: provider.connected
}])
}

/**
* @typedef {Object} WebrtcProviderEvents
* @property {function({connected:boolean}):void} WebrtcProviderEvent.status
* @property {function({synced:boolean}):void} WebrtcProviderEvent.synced
* @property {function({added:Array<string>,removed:Array<string>,webrtcPeers:Array<string>,bcPeers:Array<string>}):void} WebrtcProviderEvent.peers
*/

/**
* @extends ObservableV2<WebrtcProviderEvents>
*/
export class WebrtcProvider extends Observable {
export class WebrtcProvider extends ObservableV2 {
/**
* @param {string} roomName
* @param {Y.Doc} doc
Expand Down Expand Up @@ -618,13 +634,23 @@ export class WebrtcProvider extends Observable {
} else {
this.room.disconnect()
}
emitStatus(this)
})
this.connect()
this.destroy = this.destroy.bind(this)
doc.on('destroy', this.destroy)
}

/**
* Indicates whether the provider is looking for other peers.
*
* Other peers can be found via signaling servers or via broadcastchannel (cross browser-tab
* communication). You never know when you are connected to all peers. You also don't know if
* there are other peers. connected doesn't mean that you are connected to any physical peers
* working on the same resource as you. It does not change unless you call provider.disconnect()
*
* `this.on('status', (event) => { console.log(event.connected) })`
*
* @type {boolean}
*/
get connected () {
Expand All @@ -640,6 +666,7 @@ export class WebrtcProvider extends Observable {
})
if (this.room) {
this.room.connect()
emitStatus(this)
}
}

Expand All @@ -654,6 +681,7 @@ export class WebrtcProvider extends Observable {
})
if (this.room) {
this.room.disconnect()
emitStatus(this)
}
}

Expand Down

0 comments on commit 8cb45d9

Please sign in to comment.