sync working
This commit is contained in:
parent
f43fff233c
commit
060eb86c59
3 changed files with 34 additions and 0 deletions
|
|
@ -42,6 +42,9 @@ object Main {
|
|||
// update db when edit events happen
|
||||
entryEditBus.stream.addObserver(entryDbObserver)(using unsafeWindowOwner)
|
||||
|
||||
// sync out changes
|
||||
entryEditBus.stream.addObserver(Sync.entrySyncOut)(using unsafeWindowOwner)
|
||||
|
||||
val allEntries: Signal[Set[Entry]] =
|
||||
allEntriesVar.signal
|
||||
|
||||
|
|
|
|||
|
|
@ -13,10 +13,13 @@ import typings.trystero.mod.joinRoom
|
|||
import typings.trystero.mod.selfId
|
||||
|
||||
import scala.scalajs.js
|
||||
import scala.scalajs.js.JSConverters.*
|
||||
import typings.trystero.mod.ActionProgress
|
||||
import typings.trystero.mod.ActionSender
|
||||
import typings.trystero.mod.ActionReceiver
|
||||
import model.Entry
|
||||
import org.getshaka.nativeconverter.NativeConverter
|
||||
import fahrtenbuch.Trystero.updatePeers
|
||||
|
||||
object Trystero:
|
||||
private val eturn = new RTCIceServer:
|
||||
|
|
@ -48,6 +51,7 @@ object Trystero:
|
|||
// track online peers
|
||||
val peerList: Var[List[(String, RTCPeerConnection)]] = Var(List.empty)
|
||||
def updatePeers(): Unit =
|
||||
println(s"List of peers: ${room.getPeers().toList}")
|
||||
peerList.set(room.getPeers().toList)
|
||||
println(s"my peer ID is $selfId")
|
||||
room.onPeerJoin(peerId =>
|
||||
|
|
@ -69,3 +73,17 @@ object Actions:
|
|||
|
||||
def sendEntry(entry: Entry): Unit =
|
||||
entryAction._1(entry.toNative)
|
||||
|
||||
def sendEntry(entry: Entry, targetPeers: List[String]): Unit =
|
||||
if targetPeers.isEmpty then sendEntry(entry)
|
||||
else
|
||||
entryAction._1(data = entry.toNative, targetPeers = targetPeers.toJSArray)
|
||||
|
||||
def receiveEntry(callback: Entry => Unit): Unit =
|
||||
entryAction._2((data: js.Any, peerId: String, metaData) =>
|
||||
val incoming = NativeConverter[Entry].fromNative(data)
|
||||
callback(incoming)
|
||||
)
|
||||
|
||||
// update peers when receiving entries
|
||||
receiveEntry(_ => updatePeers())
|
||||
|
|
|
|||
|
|
@ -1,7 +1,20 @@
|
|||
package fahrtenbuch
|
||||
import com.raquo.laminar.api.L.*
|
||||
import fahrtenbuch.model.Entry
|
||||
import fahrtenbuch.DexieDB.upsertEntry
|
||||
import fahrtenbuch.Main.allEntriesVar
|
||||
|
||||
object Sync:
|
||||
val entrySyncOut =
|
||||
Observer[Entry](onNext = Actions.sendEntry(_))
|
||||
|
||||
val entrySyncIn =
|
||||
Actions.receiveEntry(received => upsertEntry(received))
|
||||
|
||||
// sync all entries on initial connection
|
||||
Trystero.room.onPeerJoin(peerId =>
|
||||
Trystero.updatePeers()
|
||||
allEntriesVar
|
||||
.now()
|
||||
.foreach(entry => Actions.sendEntry(entry, List(peerId)))
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue