Skip to content

Commit

Permalink
Migrate to 0.19.1
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Jul 9, 2024
1 parent bee527a commit 654a603
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
29 changes: 29 additions & 0 deletions Tests/YDotNet.Tests.Unit/Maps/InsertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,35 @@ public void InsertDifferentTypeOnExistingKey()
Assert.That(length, Is.EqualTo(expected: 1));
}

[Test]
public void InsertNestedMap()
{
// Arrange
var doc = new Doc();
var map = doc.Map("map");

// Act
var transaction = doc.WriteTransaction();

var innerMap = Input.Map(new Dictionary<string, Input>
{
{ "text", Input.String("Nested data") }
});

var outerMap = Input.Map(new Dictionary<string, Input>
{
{ "innerMap", innerMap }
});

map.Insert(transaction, "outerMap", outerMap);
var length = map.Length(transaction);

transaction.Commit();

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

private (Doc, Map) ArrangeDoc()
{
var doc = new Doc();
Expand Down
31 changes: 31 additions & 0 deletions Tests/YDotNet.Tests.Unit/UndoManagers/AddScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,35 @@ public void StartsTrackingAfterAddingScope()
// Assert
Assert.That(undoEvent, Is.Not.Null);
}

[Test]
public void StartsTrackingAfterAddingScopeWithoutDefaultScope()
{
// Arrange
var doc = new Doc();
var array = doc.Array("array");
var undoManager = new UndoManager(doc, new UndoManagerOptions { CaptureTimeoutMilliseconds = 0 });

UndoEvent? undoEvent = null;
undoManager.ObserveAdded(e => undoEvent = e);

// Act
undoEvent = null;
var transaction = doc.WriteTransaction();
array.InsertRange(transaction, index: 0, new[] { Input.Long(value: 2469L) });
transaction.Commit();

// Assert
Assert.That(undoEvent, Is.Null);

// Act
undoEvent = null;
undoManager.AddScope(array);
transaction = doc.WriteTransaction();
array.InsertRange(transaction, index: 1, new[] { Input.Boolean(value: false) });
transaction.Commit();

// Assert
Assert.That(undoEvent, Is.Not.Null);
}
}
25 changes: 21 additions & 4 deletions YDotNet/Document/UndoManagers/UndoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ public class UndoManager : UnmanagedResource
private readonly EventSubscriber<UndoEvent> onAdded;
private readonly EventSubscriber<UndoEvent> onPopped;

/// <summary>
/// Initializes a new instance of the <see cref="UndoManager" /> class without a scope.
/// </summary>
/// <param name="doc">The <see cref="Doc" /> to operate over.</param>
/// <param name="options">The options to initialize the <see cref="UndoManager" />.</param>
public UndoManager(Doc doc, UndoManagerOptions? options = null)
: this(doc, null, options)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UndoManager" /> class.
/// </summary>
/// <param name="doc">The <see cref="Doc" /> to operate over.</param>
/// <param name="branch">The shared type in the <see cref="Doc" /> to operate over.</param>
/// <param name="branch">The shared type in the <see cref="Doc" /> to operate over. To be added as a default scope..</param>
/// <param name="options">The options to initialize the <see cref="UndoManager" />.</param>
public UndoManager(Doc doc, Branch branch, UndoManagerOptions? options = null)
public UndoManager(Doc doc, Branch? branch, UndoManagerOptions? options = null)
: base(Create(doc, branch, options))
{
onAdded = new EventSubscriber<UndoEvent>(
Expand Down Expand Up @@ -186,10 +196,17 @@ protected override void DisposeCore(bool disposing)
UndoManagerChannel.Destroy(Handle);
}

private static nint Create(Doc doc, Branch branch, UndoManagerOptions? options)
private static nint Create(Doc doc, Branch? branch, UndoManagerOptions? options)
{
var unsafeOptions = MemoryWriter.WriteStruct(options?.ToNative() ?? default);

return UndoManagerChannel.NewWithOptions(doc.Handle, branch.Handle, unsafeOptions.Handle);
var handle = UndoManagerChannel.NewWithOptions(doc.Handle, unsafeOptions.Handle);

if (branch != null)
{
UndoManagerChannel.AddScope(handle, branch.Handle);
}

return handle;
}
}
2 changes: 1 addition & 1 deletion YDotNet/Native/UndoManager/UndoManagerChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static class UndoManagerChannel
ChannelSettings.NativeLib,
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "yundo_manager")]
public static extern nint NewWithOptions(nint doc, nint branch, nint options);
public static extern nint NewWithOptions(nint doc, nint options);

[DllImport(
ChannelSettings.NativeLib,
Expand Down

0 comments on commit 654a603

Please sign in to comment.