Skip to content

Commit

Permalink
Merge pull request #94 from y-crdt/91-update-core-bindings-to-yrs-v018
Browse files Browse the repository at this point in the history
Update core bindings to Yrs v0.18
  • Loading branch information
LSViana authored Jun 13, 2024
2 parents fb970ee + ba69d10 commit 3904b23
Show file tree
Hide file tree
Showing 111 changed files with 2,183 additions and 1,027 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
os: ubuntu-latest

# macOS
- build: macos-x86_64
- build: macos-aarch64
os: macos-latest

steps:
Expand Down
102 changes: 55 additions & 47 deletions Demo/Callback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,69 +35,77 @@ public ValueTask OnDocumentLoadedAsync(DocumentLoadEvent @event)

var map = @event.Document.Map("increment");

map?.ObserveDeep(changes =>
{
foreach (var change in changes)
map?.ObserveDeep(
changes =>
{
var key = change.MapEvent?.Keys.FirstOrDefault(x => x.Key == "value" && x.Tag != EventKeyChangeTag.Remove);
if (key != null)
foreach (var change in changes)
{
var valueOld = key.OldValue?.Double;
var valueNew = key.NewValue?.Double;
var key = change.MapEvent?.Keys.FirstOrDefault(
x => x.Key == "value" && x.Tag != EventKeyChangeTag.Remove);
if (valueOld == valueNew)
if (key != null)
{
continue;
}
var valueOld = key.OldValue?.Double;
var valueNew = key.NewValue?.Double;
if (valueOld == valueNew)
{
continue;
}
log.LogInformation("Counter updated from {oldValue} to {newValue}.", valueOld, valueNew);
log.LogInformation("Counter updated from {oldValue} to {newValue}.", valueOld, valueNew);
}
}
}
});
});

var chat = @event.Document.Array("stream");

chat?.ObserveDeep(async changes =>
{
var newNotificationsRaw =
changes
.Where(x => x.Tag == EventBranchTag.Array)
.Select(x => x.ArrayEvent)
.SelectMany(x => x.Delta.Where(x => x.Tag == EventChangeTag.Add))
.SelectMany(x => x.Values)
.ToArray();
if (newNotificationsRaw.Length == 0)
chat?.ObserveDeep(
async changes =>
{
return;
}
await Task.Delay(100);
var notificationCtx = new DocumentContext("notifications", 0);
await @event.Source.UpdateDocAsync(notificationCtx, (doc) =>
{
List<Notification> notifications;
// Keep the transaction open as short as possible.
using (var transaction = @event.Document.ReadTransaction())
var newNotificationsRaw =
changes
.Where(x => x.Tag == EventBranchTag.Array)
.Select(x => x.ArrayEvent)
.SelectMany(x => x.Delta.Where(x => x.Tag == EventChangeTag.Add))
.SelectMany(x => x.Values)
.ToArray();
if (newNotificationsRaw.Length == 0)
{
notifications = newNotificationsRaw.Select(x => x.To<Notification>(transaction)).ToList();
return;
}
var array = doc.Array("stream");
await Task.Delay(millisecondsDelay: 100);
notifications = notifications.Select(x => new Notification { Text = $"You got the follow message: {x.Text}" }).ToList();
var notificationCtx = new DocumentContext("notifications", ClientId: 0);
// Keep the transaction open as short as possible.
using (var transaction = doc.WriteTransaction())
{
array.InsertRange(transaction, array.Length, notifications.Select(x => x.ToInput()).ToArray());
}
await @event.Source.UpdateDocAsync(
notificationCtx, doc =>
{
List<Notification> notifications;
// Keep the transaction open as short as possible.
using (var transaction = @event.Document.ReadTransaction())
{
notifications = newNotificationsRaw.Select(x => x.To<Notification>(transaction)).ToList();
}
var array = doc.Array("stream");
notifications = notifications.Select(
x => new Notification { Text = $"You got the follow message: {x.Text}" })
.ToList();
// Keep the transaction open as short as possible.
using (var transaction = doc.WriteTransaction())
{
array.InsertRange(
transaction, array.Length(transaction),
notifications.Select(x => x.ToInput()).ToArray());
}
});
});
});


return default;
Expand Down
5 changes: 4 additions & 1 deletion Tests/YDotNet.Tests.Unit/Arrays/CreateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ public void Create()

// Act
var array = doc.Array("array");
var transaction = doc.ReadTransaction();
var length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(array.Handle, Is.GreaterThan(nint.Zero));
Assert.That(array.Length, Is.EqualTo(expected: 0));
Assert.That(length, Is.EqualTo(expected: 0));
}
}
20 changes: 8 additions & 12 deletions Tests/YDotNet.Tests.Unit/Arrays/InsertRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public void InsertEmptyRange()
// Act
var transaction = doc.WriteTransaction();
array.InsertRange(transaction, index: 0);
var length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(array.Length, Is.EqualTo(expected: 0));
Assert.That(length, Is.EqualTo(expected: 0));
}

[Test]
Expand All @@ -32,14 +33,12 @@ public void InsertSingleItemRange()
// Act
var transaction = doc.WriteTransaction();
array.InsertRange(
transaction, index: 0, new[]
{
Input.Boolean(value: true)
});
transaction, index: 0, Input.Boolean(value: true));
var length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(array.Length, Is.EqualTo(expected: 1));
Assert.That(length, Is.EqualTo(expected: 1));
}

[Test]
Expand All @@ -52,14 +51,11 @@ public void InsertMultiItemRange()
// Act
var transaction = doc.WriteTransaction();
array.InsertRange(
transaction, index: 0, new[]
{
Input.Boolean(value: true),
Input.Long(value: 2469L)
});
transaction, index: 0, Input.Boolean(value: true), Input.Long(value: 2469L));
var length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(array.Length, Is.EqualTo(expected: 2));
Assert.That(length, Is.EqualTo(expected: 2));
}
}
33 changes: 14 additions & 19 deletions Tests/YDotNet.Tests.Unit/Arrays/LengthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public void InitialLengthIsZero()
var array = doc.Array("array");

// Act
var length = array.Length;
var transaction = doc.ReadTransaction();
var length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(length, Is.EqualTo(expected: 0));
Expand All @@ -30,26 +32,22 @@ public void IncreasesWhenAdded()
// Act
var transaction = doc.WriteTransaction();
array.InsertRange(
transaction, index: 0, new[]
{
Input.Boolean(value: true)
});
transaction, index: 0, Input.Boolean(value: true));
var length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(array.Length, Is.EqualTo(expected: 1));
Assert.That(length, Is.EqualTo(expected: 1));

// Act
transaction = doc.WriteTransaction();
array.InsertRange(
transaction, index: 0, new[]
{
Input.Boolean(value: true)
});
transaction, index: 0, Input.Boolean(value: true));
length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(array.Length, Is.EqualTo(expected: 2));
Assert.That(length, Is.EqualTo(expected: 2));
}

[Test]
Expand All @@ -60,23 +58,20 @@ public void DecreasesWhenRemoved()
var array = doc.Array("array");
var transaction = doc.WriteTransaction();
array.InsertRange(
transaction, index: 0, new[]
{
Input.Boolean(value: true),
Input.Long(value: 2469L),
Input.Undefined()
});
transaction, index: 0, Input.Boolean(value: true), Input.Long(value: 2469L), Input.Undefined());
var length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(array.Length, Is.EqualTo(expected: 3));
Assert.That(length, Is.EqualTo(expected: 3));

// Act
transaction = doc.WriteTransaction();
array.RemoveRange(transaction, index: 1, length: 2);
length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(array.Length, Is.EqualTo(expected: 1));
Assert.That(length, Is.EqualTo(expected: 1));
}
}
13 changes: 4 additions & 9 deletions Tests/YDotNet.Tests.Unit/Arrays/MoveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void MoveFromBeginningToEnding()

// Act
var transaction = doc.WriteTransaction();
array.Move(transaction, sourceIndex: 0, array.Length);
array.Move(transaction, sourceIndex: 0, array.Length(transaction));
transaction.Commit();

// Assert
Expand All @@ -30,7 +30,7 @@ public void MoveFromEndingToBeginning()

// Act
var transaction = doc.WriteTransaction();
array.Move(transaction, array.Length - 1, targetIndex: 0);
array.Move(transaction, array.Length(transaction) - 1, targetIndex: 0);
transaction.Commit();

// Assert
Expand Down Expand Up @@ -74,13 +74,8 @@ public void MoveToTheSameIndex()

var transaction = doc.WriteTransaction();
array.InsertRange(
transaction, index: 0, new[]
{
Input.Long(value: 1),
Input.Long(value: 2),
Input.Long(value: 3),
Input.Long(value: 4)
});
transaction, index: 0, Input.Long(value: 1), Input.Long(value: 2), Input.Long(value: 3),
Input.Long(value: 4));
transaction.Commit();

return (doc, array);
Expand Down
16 changes: 7 additions & 9 deletions Tests/YDotNet.Tests.Unit/Arrays/RemoveRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public void RemoveEmptyRange()
// Act
var transaction = doc.WriteTransaction();
array.RemoveRange(transaction, index: 0, length: 0);
var length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(array.Length, Is.EqualTo(expected: 3));
Assert.That(length, Is.EqualTo(expected: 3));
}

[Test]
Expand All @@ -31,10 +32,11 @@ public void RemoveSingleItemRange()
// Act
var transaction = doc.WriteTransaction();
array.RemoveRange(transaction, index: 1, length: 1);
var length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(array.Length, Is.EqualTo(expected: 2));
Assert.That(length, Is.EqualTo(expected: 2));
}

[Test]
Expand All @@ -46,10 +48,11 @@ public void RemoveMultiItemRange()
// Act
var transaction = doc.WriteTransaction();
array.RemoveRange(transaction, index: 0, length: 3);
var length = array.Length(transaction);
transaction.Commit();

// Assert
Assert.That(array.Length, Is.EqualTo(expected: 0));
Assert.That(length, Is.EqualTo(expected: 0));
}

private (Doc, Array) ArrangeDoc()
Expand All @@ -59,12 +62,7 @@ public void RemoveMultiItemRange()

var transaction = doc.WriteTransaction();
array.InsertRange(
transaction, index: 0, new[]
{
Input.Long(value: 420L),
Input.Boolean(value: true),
Input.Null()
});
transaction, index: 0, Input.Long(value: 420L), Input.Boolean(value: true), Input.Null());
transaction.Commit();

return (doc, array);
Expand Down
Loading

0 comments on commit 3904b23

Please sign in to comment.