sync working

This commit is contained in:
Julian 2025-07-12 14:56:51 +02:00
parent f43fff233c
commit 060eb86c59
3 changed files with 34 additions and 0 deletions

View file

@ -42,6 +42,9 @@ object Main {
// update db when edit events happen // update db when edit events happen
entryEditBus.stream.addObserver(entryDbObserver)(using unsafeWindowOwner) entryEditBus.stream.addObserver(entryDbObserver)(using unsafeWindowOwner)
// sync out changes
entryEditBus.stream.addObserver(Sync.entrySyncOut)(using unsafeWindowOwner)
val allEntries: Signal[Set[Entry]] = val allEntries: Signal[Set[Entry]] =
allEntriesVar.signal allEntriesVar.signal

View file

@ -13,10 +13,13 @@ import typings.trystero.mod.joinRoom
import typings.trystero.mod.selfId import typings.trystero.mod.selfId
import scala.scalajs.js import scala.scalajs.js
import scala.scalajs.js.JSConverters.*
import typings.trystero.mod.ActionProgress import typings.trystero.mod.ActionProgress
import typings.trystero.mod.ActionSender import typings.trystero.mod.ActionSender
import typings.trystero.mod.ActionReceiver import typings.trystero.mod.ActionReceiver
import model.Entry import model.Entry
import org.getshaka.nativeconverter.NativeConverter
import fahrtenbuch.Trystero.updatePeers
object Trystero: object Trystero:
private val eturn = new RTCIceServer: private val eturn = new RTCIceServer:
@ -48,6 +51,7 @@ object Trystero:
// track online peers // track online peers
val peerList: Var[List[(String, RTCPeerConnection)]] = Var(List.empty) val peerList: Var[List[(String, RTCPeerConnection)]] = Var(List.empty)
def updatePeers(): Unit = def updatePeers(): Unit =
println(s"List of peers: ${room.getPeers().toList}")
peerList.set(room.getPeers().toList) peerList.set(room.getPeers().toList)
println(s"my peer ID is $selfId") println(s"my peer ID is $selfId")
room.onPeerJoin(peerId => room.onPeerJoin(peerId =>
@ -69,3 +73,17 @@ object Actions:
def sendEntry(entry: Entry): Unit = def sendEntry(entry: Entry): Unit =
entryAction._1(entry.toNative) 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())

View file

@ -1,7 +1,20 @@
package fahrtenbuch package fahrtenbuch
import com.raquo.laminar.api.L.* import com.raquo.laminar.api.L.*
import fahrtenbuch.model.Entry import fahrtenbuch.model.Entry
import fahrtenbuch.DexieDB.upsertEntry
import fahrtenbuch.Main.allEntriesVar
object Sync: object Sync:
val entrySyncOut = val entrySyncOut =
Observer[Entry](onNext = Actions.sendEntry(_)) 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)))
)