@@ -4,7 +4,7 @@ import * as error from 'lib0/error'
44import * as random from 'lib0/random'
55import * as encoding from 'lib0/encoding'
66import * as decoding from 'lib0/decoding'
7- import { Observable } from 'lib0/observable'
7+ import { ObservableV2 } from 'lib0/observable'
88import * as logging from 'lib0/logging'
99import * as promise from 'lib0/promise'
1010import * as bc from 'lib0/broadcastchannel'
@@ -342,9 +342,9 @@ export class Room {
342342 * Listens to Yjs updates and sends them to remote peers
343343 *
344344 *@param {Uint8Array } update
345- *@param {any }origin
345+ *@param {any }_origin
346346 */
347- this . _docUpdateHandler = ( update , origin ) => {
347+ this . _docUpdateHandler = ( update , _origin ) => {
348348const encoder = encoding . createEncoder ( )
349349encoding . writeVarUint ( encoder , messageSync )
350350syncProtocol . writeUpdate ( encoder , update )
@@ -354,9 +354,9 @@ export class Room {
354354 * Listens to Awareness updates and sends them to remote peers
355355 *
356356 *@param {any } changed
357- *@param {any }origin
357+ *@param {any }_origin
358358 */
359- this . _awarenessUpdateHandler = ( { added, updated, removed} , origin ) => {
359+ this . _awarenessUpdateHandler = ( { added, updated, removed} , _origin ) => {
360360const changedClients = added . concat ( updated ) . concat ( removed )
361361const encoderAwareness = encoding . createEncoder ( )
362362encoding . writeVarUint ( encoderAwareness , messageAwareness )
@@ -570,9 +570,25 @@ export class SignalingConn extends ws.WebsocketClient {
570570 */
571571
572572/**
573- *@extends Observable<string>
573+ *@param {WebrtcProvider } provider
574+ */
575+ const emitStatus = provider => {
576+ provider . emit ( 'status' , [ {
577+ connected :provider . connected
578+ } ] )
579+ }
580+
581+ /**
582+ *@typedef {Object } WebrtcProviderEvents
583+ *@property {function({connected:boolean}):void } WebrtcProviderEvent.status
584+ *@property {function({synced:boolean}):void } WebrtcProviderEvent.synced
585+ *@property {function({added:Array<string>,removed:Array<string>,webrtcPeers:Array<string>,bcPeers:Array<string>}):void } WebrtcProviderEvent.peers
586+ */
587+
588+ /**
589+ *@extends ObservableV2<WebrtcProviderEvents>
574590 */
575- export class WebrtcProvider extends Observable {
591+ export class WebrtcProvider extends ObservableV2 {
576592/**
577593 *@param {string } roomName
578594 *@param {Y.Doc } doc
@@ -618,13 +634,23 @@ export class WebrtcProvider extends Observable {
618634} else {
619635this . room . disconnect ( )
620636}
637+ emitStatus ( this )
621638} )
622639this . connect ( )
623640this . destroy = this . destroy . bind ( this )
624641doc . on ( 'destroy' , this . destroy )
625642}
626643
627644/**
645+ * Indicates whether the provider is looking for other peers.
646+ *
647+ * Other peers can be found via signaling servers or via broadcastchannel (cross browser-tab
648+ * communication). You never know when you are connected to all peers. You also don't know if
649+ * there are other peers. connected doesn't mean that you are connected to any physical peers
650+ * working on the same resource as you. It does not change unless you call provider.disconnect()
651+ *
652+ * `this.on('status', (event) => { console.log(event.connected) })`
653+ *
628654 *@type {boolean }
629655 */
630656get connected ( ) {
@@ -640,6 +666,7 @@ export class WebrtcProvider extends Observable {
640666} )
641667if ( this . room ) {
642668this . room . connect ( )
669+ emitStatus ( this )
643670}
644671}
645672
@@ -654,6 +681,7 @@ export class WebrtcProvider extends Observable {
654681} )
655682if ( this . room ) {
656683this . room . disconnect ( )
684+ emitStatus ( this )
657685}
658686}
659687