mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2024-12-14 10:31:25 +01:00
650901aba1
Implemented my own two phase map based on vector clocks
44 lines
785 B
Go
44 lines
785 B
Go
package automerge
|
|
|
|
import (
|
|
"github.com/automerge/automerge-go"
|
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
|
)
|
|
|
|
type AutomergeSync struct {
|
|
state *automerge.SyncState
|
|
manager *CrdtMeshManager
|
|
}
|
|
|
|
func (a *AutomergeSync) GenerateMessage() ([]byte, bool) {
|
|
msg, valid := a.state.GenerateMessage()
|
|
|
|
if !valid {
|
|
return nil, false
|
|
}
|
|
|
|
return msg.Bytes(), true
|
|
}
|
|
|
|
func (a *AutomergeSync) RecvMessage(msg []byte) error {
|
|
_, err := a.state.ReceiveMessage(msg)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *AutomergeSync) Complete() {
|
|
logging.Log.WriteInfof("Sync Completed")
|
|
a.manager.SaveChanges()
|
|
}
|
|
|
|
func NewAutomergeSync(manager *CrdtMeshManager) *AutomergeSync {
|
|
return &AutomergeSync{
|
|
state: automerge.NewSyncState(manager.doc),
|
|
manager: manager,
|
|
}
|
|
}
|