mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2024-12-13 01:50:53 +01:00
34 lines
568 B
Go
34 lines
568 B
Go
|
package crdt
|
||
|
|
||
|
import (
|
||
|
"github.com/automerge/automerge-go"
|
||
|
)
|
||
|
|
||
|
type AutomergeSync struct {
|
||
|
state *automerge.SyncState
|
||
|
}
|
||
|
|
||
|
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 NewAutomergeSync(manager *CrdtNodeManager) *AutomergeSync {
|
||
|
return &AutomergeSync{state: automerge.NewSyncState(manager.doc)}
|
||
|
}
|