Skip to content

Commit

Permalink
perf(memory-leaks): update Observe*() methods to include callbacks wh…
Browse files Browse the repository at this point in the history
…en creating EventSubscriptions
  • Loading branch information
LSViana committed Oct 3, 2023
1 parent f62969a commit 5029f2d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 73 deletions.
49 changes: 24 additions & 25 deletions YDotNet/Document/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,12 @@ public void Load(Transaction transaction)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveClear(Action<ClearEvent> action)
{
var subscriptionId = DocChannel.ObserveClear(
Handle,
nint.Zero,
(_, doc) => action(ClearEventNative.From(new Doc(doc)).ToClearEvent()));
DocChannel.ObserveClearCallback callback = (_, doc) =>
action(ClearEventNative.From(new Doc(doc)).ToClearEvent());

return new EventSubscription(subscriptionId);
var subscriptionId = DocChannel.ObserveClear(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand All @@ -360,12 +360,12 @@ public void UnobserveClear(EventSubscription subscription)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveUpdatesV1(Action<UpdateEvent> action)
{
var subscriptionId = DocChannel.ObserveUpdatesV1(
Handle,
nint.Zero,
(_, length, data) => action(UpdateEventNative.From(length, data).ToUpdateEvent()));
DocChannel.ObserveUpdatesCallback callback = (_, length, data) =>
action(UpdateEventNative.From(length, data).ToUpdateEvent());

var subscriptionId = DocChannel.ObserveUpdatesV1(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId);
return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand All @@ -388,12 +388,12 @@ public void UnobserveUpdatesV1(EventSubscription subscription)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveUpdatesV2(Action<UpdateEvent> action)
{
var subscriptionId = DocChannel.ObserveUpdatesV2(
Handle,
nint.Zero,
(_, length, data) => action(UpdateEventNative.From(length, data).ToUpdateEvent()));
DocChannel.ObserveUpdatesCallback callback = (_, length, data) =>
action(UpdateEventNative.From(length, data).ToUpdateEvent());

var subscriptionId = DocChannel.ObserveUpdatesV2(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId);
return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand All @@ -416,12 +416,12 @@ public void UnobserveUpdatesV2(EventSubscription subscription)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveAfterTransaction(Action<AfterTransactionEvent> action)
{
var subscriptionId = DocChannel.ObserveAfterTransaction(
Handle,
nint.Zero,
(_, afterTransactionEvent) => action(afterTransactionEvent.ToAfterTransactionEvent()));
DocChannel.ObserveAfterTransactionCallback callback = (_, afterTransactionEvent) =>
action(afterTransactionEvent.ToAfterTransactionEvent());

return new EventSubscription(subscriptionId);
var subscriptionId = DocChannel.ObserveAfterTransaction(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand All @@ -441,12 +441,11 @@ public void UnobserveAfterTransaction(EventSubscription subscription)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveSubDocs(Action<SubDocsEvent> action)
{
var subscriptionId = DocChannel.ObserveSubDocs(
Handle,
nint.Zero,
(_, subDocsEvent) => action(subDocsEvent.ToSubDocsEvent()));
DocChannel.ObserveSubdocsCallback callback = (_, subDocsEvent) => action(subDocsEvent.ToSubDocsEvent());

var subscriptionId = DocChannel.ObserveSubDocs(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId);
return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand Down
9 changes: 4 additions & 5 deletions YDotNet/Document/Types/Arrays/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ public void Move(Transaction transaction, uint sourceIndex, uint targetIndex)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription Observe(Action<ArrayEvent> action)
{
var subscriptionId = ArrayChannel.Observe(
Handle,
nint.Zero,
(_, eventHandle) => action(new ArrayEvent(eventHandle)));
ArrayChannel.ObserveCallback callback = (_, eventHandle) => action(new ArrayEvent(eventHandle));

return new EventSubscription(subscriptionId);
var subscriptionId = ArrayChannel.Observe(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand Down
23 changes: 10 additions & 13 deletions YDotNet/Document/Types/Branches/Branch.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using YDotNet.Document.Events;
using YDotNet.Document.StickyIndexes;
using YDotNet.Document.Transactions;
using YDotNet.Document.Types.Events;
using YDotNet.Infrastructure;
using YDotNet.Native.StickyIndex;
using YDotNet.Native.Types.Branches;

namespace YDotNet.Document.Types.Branches;
Expand Down Expand Up @@ -38,19 +36,18 @@ protected Branch(nint handle)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveDeep(Action<IEnumerable<EventBranch>> action)
{
var subscriptionId = BranchChannel.ObserveDeep(
Handle,
nint.Zero,
(_, length, eventsHandle) =>
{
var events = MemoryReader.TryReadIntPtrArray(eventsHandle, length, size: 24)!
.Select(x => new EventBranch(x))
.ToArray();
BranchChannel.ObserveCallback callback = (_, length, eventsHandle) =>
{
var events = MemoryReader.TryReadIntPtrArray(eventsHandle, length, size: 24)!
.Select(x => new EventBranch(x))
.ToArray();

action(events);
});
action(events);
};

return new EventSubscription(subscriptionId);
var subscriptionId = BranchChannel.ObserveDeep(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand Down
9 changes: 4 additions & 5 deletions YDotNet/Document/Types/Maps/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,11 @@ public void RemoveAll(Transaction transaction)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription Observe(Action<MapEvent> action)
{
var subscriptionId = MapChannel.Observe(
Handle,
nint.Zero,
(_, eventHandle) => action(new MapEvent(eventHandle)));
MapChannel.ObserveCallback callback = (_, eventHandle) => action(new MapEvent(eventHandle));

return new EventSubscription(subscriptionId);
var subscriptionId = MapChannel.Observe(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand Down
9 changes: 4 additions & 5 deletions YDotNet/Document/Types/Texts/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,11 @@ public uint Length(Transaction transaction)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription Observe(Action<TextEvent> action)
{
var subscriptionId = TextChannel.Observe(
Handle,
nint.Zero,
(_, eventHandle) => action(new TextEvent(eventHandle)));
TextChannel.ObserveCallback callback = (_, eventHandle) => action(new TextEvent(eventHandle));

return new EventSubscription(subscriptionId);
var subscriptionId = TextChannel.Observe(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand Down
9 changes: 4 additions & 5 deletions YDotNet/Document/Types/XmlElements/XmlElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,11 @@ public void RemoveRange(Transaction transaction, uint index, uint length)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription Observe(Action<XmlElementEvent> action)
{
var subscriptionId = XmlElementChannel.Observe(
Handle,
nint.Zero,
(_, eventHandle) => action(new XmlElementEvent(eventHandle)));
XmlElementChannel.ObserveCallback callback = (_, eventHandle) => action(new XmlElementEvent(eventHandle));

return new EventSubscription(subscriptionId);
var subscriptionId = XmlElementChannel.Observe(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand Down
9 changes: 4 additions & 5 deletions YDotNet/Document/Types/XmlTexts/XmlText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,11 @@ public void Format(Transaction transaction, uint index, uint length, Input attri
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription Observe(Action<XmlTextEvent> action)
{
var subscriptionId = XmlTextChannel.Observe(
Handle,
nint.Zero,
(_, eventHandle) => action(new XmlTextEvent(eventHandle)));
XmlTextChannel.ObserveCallback callback = (_, eventHandle) => action(new XmlTextEvent(eventHandle));

return new EventSubscription(subscriptionId);
var subscriptionId = XmlTextChannel.Observe(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand Down
18 changes: 8 additions & 10 deletions YDotNet/Document/UndoManagers/UndoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ public void Dispose()
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObserveAdded(Action<UndoEvent> action)
{
var subscriptionId = UndoManagerChannel.ObserveAdded(
Handle,
nint.Zero,
(_, undoEvent) => action(undoEvent.ToUndoEvent()));
UndoManagerChannel.ObserveAddedCallback callback = (_, undoEvent) => action(undoEvent.ToUndoEvent());

return new EventSubscription(subscriptionId);
var subscriptionId = UndoManagerChannel.ObserveAdded(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand All @@ -73,12 +72,11 @@ public void UnobserveAdded(EventSubscription subscription)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public EventSubscription ObservePopped(Action<UndoEvent> action)
{
var subscriptionId = UndoManagerChannel.ObservePopped(
Handle,
nint.Zero,
(_, undoEvent) => action(undoEvent.ToUndoEvent()));
UndoManagerChannel.ObservePoppedCallback callback = (_, undoEvent) => action(undoEvent.ToUndoEvent());

var subscriptionId = UndoManagerChannel.ObservePopped(Handle, nint.Zero, callback);

return new EventSubscription(subscriptionId);
return new EventSubscription(subscriptionId, callback);
}

/// <summary>
Expand Down

0 comments on commit 5029f2d

Please sign in to comment.