From 5029f2ddf8c9225d10475a09dc4caaed98be0fc2 Mon Sep 17 00:00:00 2001 From: Lucas Viana Date: Mon, 2 Oct 2023 22:55:14 -0300 Subject: [PATCH] perf(memory-leaks): update Observe*() methods to include callbacks when creating EventSubscriptions --- YDotNet/Document/Doc.cs | 49 +++++++++---------- YDotNet/Document/Types/Arrays/Array.cs | 9 ++-- YDotNet/Document/Types/Branches/Branch.cs | 23 ++++----- YDotNet/Document/Types/Maps/Map.cs | 9 ++-- YDotNet/Document/Types/Texts/Text.cs | 9 ++-- .../Document/Types/XmlElements/XmlElement.cs | 9 ++-- YDotNet/Document/Types/XmlTexts/XmlText.cs | 9 ++-- YDotNet/Document/UndoManagers/UndoManager.cs | 18 +++---- 8 files changed, 62 insertions(+), 73 deletions(-) diff --git a/YDotNet/Document/Doc.cs b/YDotNet/Document/Doc.cs index a8fb02ed..72dac9b8 100644 --- a/YDotNet/Document/Doc.cs +++ b/YDotNet/Document/Doc.cs @@ -332,12 +332,12 @@ public void Load(Transaction transaction) /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription ObserveClear(Action 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); } /// @@ -360,12 +360,12 @@ public void UnobserveClear(EventSubscription subscription) /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription ObserveUpdatesV1(Action 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); } /// @@ -388,12 +388,12 @@ public void UnobserveUpdatesV1(EventSubscription subscription) /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription ObserveUpdatesV2(Action 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); } /// @@ -416,12 +416,12 @@ public void UnobserveUpdatesV2(EventSubscription subscription) /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription ObserveAfterTransaction(Action 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); } /// @@ -441,12 +441,11 @@ public void UnobserveAfterTransaction(EventSubscription subscription) /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription ObserveSubDocs(Action 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); } /// diff --git a/YDotNet/Document/Types/Arrays/Array.cs b/YDotNet/Document/Types/Arrays/Array.cs index a1c8b16f..ea262100 100644 --- a/YDotNet/Document/Types/Arrays/Array.cs +++ b/YDotNet/Document/Types/Arrays/Array.cs @@ -107,12 +107,11 @@ public void Move(Transaction transaction, uint sourceIndex, uint targetIndex) /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription Observe(Action 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); } /// diff --git a/YDotNet/Document/Types/Branches/Branch.cs b/YDotNet/Document/Types/Branches/Branch.cs index d81eba8b..2ca1e64c 100644 --- a/YDotNet/Document/Types/Branches/Branch.cs +++ b/YDotNet/Document/Types/Branches/Branch.cs @@ -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; @@ -38,19 +36,18 @@ protected Branch(nint handle) /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription ObserveDeep(Action> 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); } /// diff --git a/YDotNet/Document/Types/Maps/Map.cs b/YDotNet/Document/Types/Maps/Map.cs index 0b8dcc4b..b9facaf2 100644 --- a/YDotNet/Document/Types/Maps/Map.cs +++ b/YDotNet/Document/Types/Maps/Map.cs @@ -116,12 +116,11 @@ public void RemoveAll(Transaction transaction) /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription Observe(Action 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); } /// diff --git a/YDotNet/Document/Types/Texts/Text.cs b/YDotNet/Document/Types/Texts/Text.cs index 9ce2cb11..4a192de0 100644 --- a/YDotNet/Document/Types/Texts/Text.cs +++ b/YDotNet/Document/Types/Texts/Text.cs @@ -149,12 +149,11 @@ public uint Length(Transaction transaction) /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription Observe(Action 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); } /// diff --git a/YDotNet/Document/Types/XmlElements/XmlElement.cs b/YDotNet/Document/Types/XmlElements/XmlElement.cs index 7193de9c..f32e1502 100644 --- a/YDotNet/Document/Types/XmlElements/XmlElement.cs +++ b/YDotNet/Document/Types/XmlElements/XmlElement.cs @@ -282,12 +282,11 @@ public void RemoveRange(Transaction transaction, uint index, uint length) /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription Observe(Action 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); } /// diff --git a/YDotNet/Document/Types/XmlTexts/XmlText.cs b/YDotNet/Document/Types/XmlTexts/XmlText.cs index 32ead3a0..d9203ee6 100644 --- a/YDotNet/Document/Types/XmlTexts/XmlText.cs +++ b/YDotNet/Document/Types/XmlTexts/XmlText.cs @@ -227,12 +227,11 @@ public void Format(Transaction transaction, uint index, uint length, Input attri /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription Observe(Action 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); } /// diff --git a/YDotNet/Document/UndoManagers/UndoManager.cs b/YDotNet/Document/UndoManagers/UndoManager.cs index 8c30bd99..ce44878e 100644 --- a/YDotNet/Document/UndoManagers/UndoManager.cs +++ b/YDotNet/Document/UndoManagers/UndoManager.cs @@ -47,12 +47,11 @@ public void Dispose() /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription ObserveAdded(Action 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); } /// @@ -73,12 +72,11 @@ public void UnobserveAdded(EventSubscription subscription) /// The subscription for the event. It may be used to unsubscribe later. public EventSubscription ObservePopped(Action 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); } ///