Yrs and Yjs compatible collaborative data structures in MoonBit (in development)
moon add bobzhang/yrs@0.1.1///|
test "two replicas exchange concurrent edits" {
let a = @yrs.Doc(client_id=1UL)
let b = @yrs.Doc(client_id=2UL)
let left = a.get_or_insert_text("note")
let right = b.get_or_insert_text("note")
left.insert(0, "Hello")
b.apply_update_v1(a.encode_state_as_update_v1())
left.insert(5, " Moon")
right.insert(5, " Rust")
a.apply_update_v1(b.encode_state_as_update_v1(remote=a.state_vector()))
b.apply_update_v1(a.encode_state_as_update_v1(remote=b.state_vector()))
assert_eq(left.get_string(), "Hello Moon Rust")
assert_eq(left.get_string(), right.get_string())
}///|
test "resolve a typed collection on another replica" {
let source = @yrs.Doc(client_id=1UL)
let root : @yrs.Root[@yrs.MapRef] = Root("workspace")
let map = root.get_or_create(source)
let text = map.insert_text("title")
text.insert(0, "Hello")
let handle = text.hook()
let peer = @yrs.Doc(client_id=2UL)
peer.apply_update_v2(source.encode_state_as_update_v2())
assert_eq(handle.get(peer).unwrap().get_string(), "Hello")
map.remove("title")
peer.apply_update_v1(source.encode_state_as_update_v1())
assert_true(handle.get(peer) is None)
}pub(open) trait AsyncProtocol {
async fn start(Self, Awareness) -> Bytes = _
async fn handle(Self, Awareness, Bytes) -> Array[Message] = _
async fn handle_message(Self, Awareness, Message) -> Message? = _
async fn handle_sync_step1(Self, Awareness, StateVector) -> Message? = _
async fn handle_sync_step2(Self, Awareness, Update) -> Message? = _
async fn handle_update(Self, Awareness, Update) -> Message? = _
async fn handle_auth(Self, Awareness, String?) -> Message? = _
async fn handle_awareness_query(Self, Awareness) -> Message? = _
async fn handle_awareness_update(Self, Awareness, AwarenessUpdate) -> Message? = _
async fn missing_handle(Self, Awareness, Byte, Bytes) -> Message? = _
}pub(open) trait Decode {
fn decode(&Decoder) -> Self raise DecodeError
fn decode_v1(Bytes) -> Self raise DecodeError = _
fn decode_v2(Bytes) -> Self raise DecodeError = _
}pub(open) trait Decoder : Read {
fn reset_ds_cur_val(Self) -> Unit
fn read_to_end(Self) -> Bytes
fn read_ds_clock(Self) -> UInt raise DecodeError
fn read_ds_len(Self) -> UInt raise DecodeError
fn read_left_id(Self) -> Id raise DecodeError
fn read_right_id(Self) -> Id raise DecodeError
fn read_client(Self) -> UInt64 raise DecodeError
fn read_info(Self) -> Byte raise DecodeError
fn read_parent_info(Self) -> Bool raise DecodeError
fn read_type_ref(Self) -> Byte raise DecodeError
fn read_len(Self) -> UInt raise DecodeError
fn read_any(Self) -> Any raise DecodeError
fn read_json(Self) -> Any raise DecodeError
fn read_key(Self) -> String raise DecodeError
}pub(open) trait Encode {
fn encode(Self, &Encoder) -> Unit
fn encode_v1(Self) -> Bytes raise DecodeError = _
fn encode_v2(Self) -> Bytes raise DecodeError = _
}pub(open) trait Encoder : Write {
fn to_bytes(Self) -> Bytes raise DecodeError
fn reset_ds_cur_val(Self) -> Unit
fn write_ds_clock(Self, UInt) -> Unit
fn write_ds_len(Self, UInt) -> Unit
fn write_left_id(Self, Id) -> Unit
fn write_right_id(Self, Id) -> Unit
fn write_client(Self, UInt64) -> Unit
fn write_info(Self, Byte) -> Unit
fn write_parent_info(Self, Bool) -> Unit
fn write_type_ref(Self, Byte) -> Unit
fn write_len(Self, UInt) -> Unit
fn write_any(Self, Any) -> Unit
fn write_json(Self, Any) -> Unit
fn write_key(Self, String) -> Unit
}pub(open) trait Protocol {
fn start(Self, Awareness) -> Bytes raise = _
fn handle(Self, Awareness, Bytes) -> Array[Message] raise = _
fn handle_message(Self, Awareness, Message) -> Message? raise = _
fn handle_sync_step1(Self, Awareness, StateVector) -> Message? raise = _
fn handle_sync_step2(Self, Awareness, Update) -> Message? raise = _
fn handle_update(Self, Awareness, Update) -> Message? raise = _
fn handle_auth(Self, Awareness, String?) -> Message? raise = _
fn handle_awareness_query(Self, Awareness) -> Message? raise = _
fn handle_awareness_update(Self, Awareness, AwarenessUpdate) -> Message? raise = _
fn missing_handle(Self, Awareness, Byte, Bytes) -> Message? raise = _
}pub(open) trait Read {
fn read_exact(Self, Int) -> Bytes raise DecodeError
fn read_u8(Self) -> Byte raise DecodeError = _
fn read_u16(Self) -> UInt16 raise DecodeError = _
fn read_u32(Self) -> UInt raise DecodeError = _
fn read_u32_be(Self) -> UInt raise DecodeError = _
fn read_u64(Self) -> UInt64 raise DecodeError = _
fn read_i64(Self) -> Int64 raise DecodeError = _
fn read_f32(Self) -> Float raise DecodeError = _
fn read_f64(Self) -> Double raise DecodeError = _
fn read_var_uint(Self) -> UInt64 raise DecodeError = _
fn read_var_int(Self) -> Int64 raise DecodeError = _
fn read_var_signed(Self) -> (Int64, Bool) raise DecodeError = _
fn read_buf(Self) -> Bytes raise DecodeError = _
fn read_string(Self) -> String raise DecodeError = _
}impl ToAny for ReadOnlyArray[T]pub(open) trait Write {
fn write_all(Self, Bytes) -> Unit
fn write_u8(Self, Byte) -> Unit = _
fn write_u16(Self, UInt16) -> Unit = _
fn write_u32(Self, UInt) -> Unit = _
fn write_u32_be(Self, UInt) -> Unit = _
fn write_u64(Self, UInt64) -> Unit = _
fn write_i64(Self, Int64) -> Unit = _
fn write_f32(Self, Float) -> Unit = _
fn write_f64(Self, Double) -> Unit = _
fn write_var_uint(Self, UInt64) -> Unit = _
fn write_var_int(Self, Int64) -> Unit = _
fn write_var_signed(Self, (Int64, Bool)) -> Unit = _
fn write_buf(Self, Bytes) -> Unit = _
fn write_string(Self, String) -> Unit = _
}pub struct ArrayEvent {
// private fields
}pub struct ArrayRef {
// private fields
}impl DefaultPrelim for ArrayReffn ArrayRef::observe(self : ArrayRef, key : String, callback : (ArrayEvent, Transaction) -> Unit) -> Unitfn ArrayRef::observe_deep(self : ArrayRef, key : String, callback : (ReadOnlyArray[Event], Transaction) -> Unit) -> Unitfn ArrayRef::quote(self : ArrayRef, start : QuoteBound, end : QuoteBound) -> WeakPrelim raise QuoteErrorpub(all) struct AttrRange[A] {
start : UInt
end : UInt
attrs : ContentAttributes[A]
} derive(Eq, Debug)pub struct Awareness {
// private fields
}fn Awareness::apply_update(self : Awareness, update : AwarenessUpdate, origin? : Bytes) -> AwarenessSummary?fn Awareness::on_change(self : Awareness, key : String, callback : (Awareness, AwarenessSummary, Bytes?) -> Unit) -> Unitfn Awareness::on_update(self : Awareness, key : String, callback : (Awareness, AwarenessSummary, Bytes?) -> Unit) -> Unitfn Awareness::update_with_clients(self : Awareness, clients : Array[UInt64]) -> AwarenessUpdate raise AwarenessErrorpub struct AwarenessUpdate {
// private fields
}pub struct ByteWriter {
// private fields
}impl Write for ByteWriterimpl Eq for ContentAttributes[A]fn[A] ContentAttributes::ContentAttributes(values? : Array[ContentAttribute[A]]) -> ContentAttributes[A]fn[A : Eq] ContentAttributes::equal(self : ContentAttributes[A], other : ContentAttributes[A]) -> Boolfn[A : Eq] ContentAttributes::merge(self : ContentAttributes[A], other : ContentAttributes[A]) -> ContentAttributes[A]pub struct Cursor {
// private fields
}pub struct DecoderV1 {
// private fields
}pub struct DecoderV2 {
// private fields
}pub struct DefaultProtocol {
}impl AsyncProtocol for DefaultProtocolasync fn missing_handle(_self : DefaultProtocol, _awareness : Awareness, tag : Byte, _bytes : Bytes) -> Message?impl Protocol for DefaultProtocolfn missing_handle(_self : DefaultProtocol, _awareness : Awareness, tag : Byte, _bytes : Bytes) -> Message? raisefn DefaultProtocol::handle(self : DefaultProtocol, awareness : Awareness, bytes : Bytes) -> Array[Message] raisefn DefaultProtocol::handle_auth(_self : DefaultProtocol, _awareness : Awareness, reason : String?) -> Message? raisefn DefaultProtocol::handle_awareness_query(_self : DefaultProtocol, awareness : Awareness) -> Message? raisefn DefaultProtocol::handle_awareness_update(_self : DefaultProtocol, awareness : Awareness, update : AwarenessUpdate) -> Message? raisefn DefaultProtocol::handle_message(self : DefaultProtocol, awareness : Awareness, message : Message) -> Message? raisefn DefaultProtocol::handle_sync_step1(_self : DefaultProtocol, awareness : Awareness, remote : StateVector) -> Message? raisefn DefaultProtocol::handle_sync_step2(_self : DefaultProtocol, awareness : Awareness, update : Update) -> Message? raisefn DefaultProtocol::handle_update(self : DefaultProtocol, awareness : Awareness, update : Update) -> Message? raisefn DefaultProtocol::missing_handle(_self : DefaultProtocol, _awareness : Awareness, tag : Byte, _bytes : Bytes) -> Message? raisepub struct Doc {
// private fields
}fn Doc::Doc(client_id~ : UInt64, guid? : String, collection_id? : String, auto_load? : Bool, should_load? : Bool, offset_kind? : OffsetKind, skip_gc? : Bool, cleanup_formatting? : Bool) -> Docfn Doc::encode_state_from_snapshot(self : Doc, snapshot : Snapshot, encoder : &Encoder) -> Unit raise DecodeErrorfn Doc::observe_after_transaction(self : Doc, key : String, callback : (Transaction) -> Unit) -> Unitfn Doc::observe_before_observer_calls(self : Doc, key : String, callback : (Transaction) -> Unit) -> Unitfn Doc::observe_subdocs(self : Doc, key : String, callback : (SubdocsEvent, Transaction) -> Unit) -> Unitfn Doc::observe_transaction_cleanup(self : Doc, key : String, callback : (Transaction) -> Unit) -> Unitfn Doc::observe_update_v1(self : Doc, key : String, callback : (Bytes, Transaction) -> Unit) -> Unitfn Doc::observe_update_v2(self : Doc, key : String, callback : (Bytes, Transaction) -> Unit) -> Unitpub struct EncoderV1 {
// private fields
}pub struct EncoderV2 {
// private fields
}pub(all) enum Event {
Text(TextEvent)
Array(ArrayEvent)
Map(MapEvent)
XmlFragment(XmlEvent)
XmlText(XmlTextEvent)
Weak(WeakEvent)
}pub struct Hook[S] {
// private fields
}pub struct IdMap[A] {
// private fields
}fn[A] IdMap::decode_v1(bytes : Bytes, decode_value : (Any) -> A raise DecodeError, max_items? : Int) -> IdMap[A] raise DecodeErrorfn[A] IdMap::decode_v2(bytes : Bytes, decode_value : (Any) -> A raise DecodeError, max_items? : Int) -> IdMap[A] raise DecodeErrorfn[A] IdMap::filter(self : IdMap[A], predicate : (ReadOnlyArray[ContentAttribute[A]]) -> Bool) -> IdMap[A]fn[A : Eq] IdMap::insert(self : IdMap[A], range : BlockRange, attrs : Array[ContentAttribute[A]]) -> Unitpub struct IdSet {
// private fields
}pub(all) enum In {
Value(Any)
Array(ReadOnlyArray[In])
Map(ReadOnlyArray[(String, In)])
Text(ReadOnlyArray[DeltaOf[In]])
XmlElement(String, ReadOnlyArray[(String, In)], ReadOnlyArray[In])
XmlFragment(ReadOnlyArray[In])
XmlText(ReadOnlyArray[DeltaOf[In]], ReadOnlyArray[(String, In)])
Subdoc(Doc)
WeakLink(WeakPrelim)
}pub struct JsonPath {
// private fields
}pub struct MapEvent {
// private fields
}pub struct MapRef {
// private fields
}impl DefaultPrelim for MapReffn MapRef::observe_deep(self : MapRef, key : String, callback : (ReadOnlyArray[Event], Transaction) -> Unit) -> Unitpub(all) enum Message {
Sync(SyncMessage)
Auth(String?)
AwarenessQuery
Awareness(AwarenessUpdate)
Custom(Byte, Bytes)
}pub struct Observer[K, F] {
// private fields
}pub(all) enum Out {
Value(Any)
Subdoc(Doc)
UndefinedRef(UndefinedRef)
Array(ArrayRef)
Map(MapRef)
Text(Text)
XmlElement(XmlElement)
XmlFragment(XmlFragment)
XmlText(XmlText)
WeakLink(WeakRef)
} derive(Eq)pub struct Root[S] {
name : String
}fn Snapshot::Snapshot(state? : StateVector, deletions? : Array[(UInt64, UInt, UInt)], delete_set? : IdSet) -> Snapshotpub struct StackItem[M] {
meta : M
// private fields
}fn[M] StackItem::from_json(json : Json, doc : Doc, decode_meta : (Json) -> M raise DecodeError) -> StackItem[M] raise DecodeErrorimpl Decode for StateVectorimpl Encode for StateVectorimpl Eq for StateVectorimpl Decode for StickyIndeximpl Encode for StickyIndexpub struct SubdocsEvent {
// private fields
}pub struct Text {
// private fields
}fn Text::observe_deep(self : Text, key : String, callback : (ReadOnlyArray[Event], Transaction) -> Unit) -> Unitpub struct TextEvent {
// private fields
}pub struct Transaction {
// private fields
}pub(all) enum TypeRef {
Array
Map
Text
XmlElement(String)
XmlFragment
XmlHook
XmlText
WeakLink(LinkSource)
SubDoc
Undefined
} derive(Eq, Debug)pub struct UndefinedRef {
// private fields
}impl AsPrelim for UndefinedRefimpl Eq for UndefinedRefpub struct UndoEvent[M] {
meta : M
// private fields
}pub struct UndoManager[M] {
// private fields
}fn[M : Default] UndoManager::UndoManager(clock~ : () -> UInt64, capture_timeout_millis? : UInt64, tracked_origins? : Array[Bytes], capture_transaction? : (Transaction) -> Bool, init_undo_stack? : Array[StackItem[M]], init_redo_stack? : Array[StackItem[M]]) -> UndoManager[M]fn[M] UndoManager::observe_item_added(self : UndoManager[M], key : String, callback : (UndoEvent[M], Transaction) -> Unit) -> Unitfn[M] UndoManager::observe_item_popped(self : UndoManager[M], key : String, callback : (UndoEvent[M], Transaction) -> Unit) -> Unitfn[M] UndoManager::observe_item_updated(self : UndoManager[M], key : String, callback : (UndoEvent[M], Transaction) -> Unit) -> Unitfn[M] UndoManager::observe_stack_cleared(self : UndoManager[M], key : String, callback : (Bool, Bool) -> Unit) -> Unitpub struct Update {
// private fields
}pub struct WeakEvent {
// private fields
}pub struct WeakPrelim {
// private fields
}pub struct WeakRef {
// private fields
}fn WeakRef::observe(self : WeakRef, key : String, callback : (WeakEvent, Transaction) -> Unit) -> Unitfn WeakRef::observe_deep(self : WeakRef, key : String, callback : (ReadOnlyArray[Event], Transaction) -> Unit) -> Unitpub struct XmlElement {
// private fields
}impl AsPrelim for XmlElementimpl FromOut for XmlElementimpl Eq for XmlElementfn XmlElement::observe(self : XmlElement, key : String, callback : (XmlEvent, Transaction) -> Unit) -> Unitfn XmlElement::observe_deep(self : XmlElement, key : String, callback : (ReadOnlyArray[Event], Transaction) -> Unit) -> Unitfn XmlElement::quote(self : XmlElement, start : QuoteBound, end : QuoteBound) -> WeakPrelim raise QuoteErrorpub struct XmlEvent {
// private fields
}pub struct XmlFragment {
// private fields
}impl AsPrelim for XmlFragmentimpl DefaultPrelim for XmlFragmentimpl FromOut for XmlFragmentimpl RootRef for XmlFragmentimpl Eq for XmlFragmentfn XmlFragment::observe(self : XmlFragment, key : String, callback : (XmlEvent, Transaction) -> Unit) -> Unitfn XmlFragment::observe_deep(self : XmlFragment, key : String, callback : (ReadOnlyArray[Event], Transaction) -> Unit) -> Unitfn XmlFragment::quote(self : XmlFragment, start : QuoteBound, end : QuoteBound) -> WeakPrelim raise QuoteErrorpub struct XmlText {
// private fields
}impl DefaultPrelim for XmlTextfn XmlText::observe(self : XmlText, key : String, callback : (XmlTextEvent, Transaction) -> Unit) -> Unitfn XmlText::observe_deep(self : XmlText, key : String, callback : (ReadOnlyArray[Event], Transaction) -> Unit) -> Unitfn XmlText::quote(self : XmlText, start : QuoteBound, end : QuoteBound) -> WeakPrelim raise QuoteErrorpub struct XmlTextEvent {
// private fields
}fn uuid_v4(entropy : () -> Bytes) -> Stringfn uuid_v4_from(value : Bytes) -> StringInstall
Download zipYrs and Yjs compatible collaborative data structures in MoonBit (in development)