Skip to content

Commit

Permalink
feat: replace EventSubscriber with EventSubscriberFromId in Map
Browse files Browse the repository at this point in the history
…, `Text`, `XmlElement`, `XmlFragment`, and `XmlText`
  • Loading branch information
LSViana committed May 13, 2024
1 parent 33a37b5 commit 3deb5d5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
26 changes: 13 additions & 13 deletions YDotNet/Document/Types/Maps/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ namespace YDotNet.Document.Types.Maps;
/// </summary>
public class Map : Branch
{
private readonly EventSubscriber<MapEvent> onObserve;
private readonly EventSubscriberFromId<MapEvent> onObserve;

internal Map(nint handle, Doc doc, bool isDeleted)
: base(handle, doc, isDeleted)
{
this.onObserve = new EventSubscriber<MapEvent>(
onObserve = new EventSubscriberFromId<MapEvent>(
doc.EventManager,
handle,
this,
(map, action) =>
{
MapChannel.ObserveCallback callback = (_, eventHandle) =>
action(new MapEvent(eventHandle, this.Doc));
action(new MapEvent(eventHandle, Doc));
return (MapChannel.Observe(map, nint.Zero, callback), callback);
},
Expand All @@ -47,7 +47,7 @@ public void Insert(Transaction transaction, string key, Input input)
using var unsafeKey = MemoryWriter.WriteUtf8String(key);
using var unsafeValue = MemoryWriter.WriteStruct(input.InputNative);

MapChannel.Insert(this.GetHandle(transaction), transaction.Handle, unsafeKey.Handle, unsafeValue.Handle);
MapChannel.Insert(GetHandle(transaction), transaction.Handle, unsafeKey.Handle, unsafeValue.Handle);
}

/// <summary>
Expand All @@ -63,9 +63,9 @@ public void Insert(Transaction transaction, string key, Input input)
{
using var unsafeName = MemoryWriter.WriteUtf8String(key);

var handle = MapChannel.Get(this.GetHandle(transaction), transaction.Handle, unsafeName.Handle);
var handle = MapChannel.Get(GetHandle(transaction), transaction.Handle, unsafeName.Handle);

return handle != nint.Zero ? Output.CreateAndRelease(handle, this.Doc) : null;
return handle != nint.Zero ? Output.CreateAndRelease(handle, Doc) : null;
}

/// <summary>
Expand All @@ -75,7 +75,7 @@ public void Insert(Transaction transaction, string key, Input input)
/// <returns>The number of entries stored in the <see cref="Map" />.</returns>
public uint Length(Transaction transaction)
{
return MapChannel.Length(this.GetHandle(transaction), transaction.Handle);
return MapChannel.Length(GetHandle(transaction), transaction.Handle);
}

/// <summary>
Expand All @@ -88,7 +88,7 @@ public bool Remove(Transaction transaction, string key)
{
using var unsafeKey = MemoryWriter.WriteUtf8String(key);

return MapChannel.Remove(this.GetHandle(transaction), transaction.Handle, unsafeKey.Handle) == 1;
return MapChannel.Remove(GetHandle(transaction), transaction.Handle, unsafeKey.Handle) == 1;
}

/// <summary>
Expand All @@ -97,7 +97,7 @@ public bool Remove(Transaction transaction, string key)
/// <param name="transaction">The transaction that wraps this operation.</param>
public void RemoveAll(Transaction transaction)
{
MapChannel.RemoveAll(this.GetHandle(transaction), transaction.Handle);
MapChannel.RemoveAll(GetHandle(transaction), transaction.Handle);
}

/// <summary>
Expand All @@ -108,9 +108,9 @@ public void RemoveAll(Transaction transaction)
/// <returns>The <see cref="MapIterator" /> instance.</returns>
public MapIterator Iterate(Transaction transaction)
{
var handle = MapChannel.Iterator(this.GetHandle(transaction), transaction.Handle).Checked();
var handle = MapChannel.Iterator(GetHandle(transaction), transaction.Handle).Checked();

return new MapIterator(handle, this.Doc);
return new MapIterator(handle, Doc);
}

/// <summary>
Expand All @@ -123,6 +123,6 @@ public MapIterator Iterate(Transaction transaction)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public IDisposable Observe(Action<MapEvent> action)
{
return this.onObserve.Subscribe(action);
return onObserve.Subscribe(action);
}
}
30 changes: 15 additions & 15 deletions YDotNet/Document/Types/Texts/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ namespace YDotNet.Document.Types.Texts;
/// </summary>
public class Text : Branch
{
private readonly EventSubscriber<TextEvent> onObserve;
private readonly EventSubscriberFromId<TextEvent> onObserve;

internal Text(nint handle, Doc doc, bool isDeleted)
: base(handle, doc, isDeleted)
{
this.onObserve = new EventSubscriber<TextEvent>(
onObserve = new EventSubscriberFromId<TextEvent>(
doc.EventManager,
handle,
this,
(text, action) =>
{
TextChannel.ObserveCallback callback = (_, eventHandle) =>
action(new TextEvent(eventHandle, this.Doc));
action(new TextEvent(eventHandle, Doc));
return (TextChannel.Observe(text, nint.Zero, callback), callback);
},
Expand All @@ -51,7 +51,7 @@ public void Insert(Transaction transaction, uint index, string value, Input? att
using var unsafeAttributes = MemoryWriter.WriteStruct(attributes?.InputNative);

TextChannel.Insert(
this.GetHandle(transaction),
GetHandle(transaction),
transaction.Handle,
index,
unsafeValue.Handle,
Expand All @@ -74,7 +74,7 @@ public void InsertEmbed(Transaction transaction, uint index, Input content, Inpu
var unsafeAttributes = MemoryWriter.WriteStruct(attributes?.InputNative);

TextChannel.InsertEmbed(
this.GetHandle(transaction),
GetHandle(transaction),
transaction.Handle,
index,
unsafeContent.Handle,
Expand All @@ -92,7 +92,7 @@ public void InsertEmbed(Transaction transaction, uint index, Input content, Inpu
/// </param>
public void RemoveRange(Transaction transaction, uint index, uint length)
{
TextChannel.RemoveRange(this.GetHandle(transaction), transaction.Handle, index, length);
TextChannel.RemoveRange(GetHandle(transaction), transaction.Handle, index, length);
}

/// <summary>
Expand All @@ -112,7 +112,7 @@ public void Format(Transaction transaction, uint index, uint length, Input attri
{
using var unsafeAttributes = MemoryWriter.WriteStruct(attributes.InputNative);

TextChannel.Format(this.GetHandle(transaction), transaction.Handle, index, length, unsafeAttributes.Handle);
TextChannel.Format(GetHandle(transaction), transaction.Handle, index, length, unsafeAttributes.Handle);
}

/// <summary>
Expand All @@ -122,9 +122,9 @@ public void Format(Transaction transaction, uint index, uint length, Input attri
/// <returns>The <see cref="TextChunks" /> that compose this <see cref="Text" />.</returns>
public TextChunks Chunks(Transaction transaction)
{
var handle = TextChannel.Chunks(this.GetHandle(transaction), transaction.Handle, out var length).Checked();
var handle = TextChannel.Chunks(GetHandle(transaction), transaction.Handle, out var length).Checked();

return new TextChunks(handle, length, this.Doc);
return new TextChunks(handle, length, Doc);
}

/// <summary>
Expand All @@ -134,7 +134,7 @@ public TextChunks Chunks(Transaction transaction)
/// <returns>The full string stored in the instance.</returns>
public string String(Transaction transaction)
{
var handle = TextChannel.String(this.GetHandle(transaction), transaction.Handle);
var handle = TextChannel.String(GetHandle(transaction), transaction.Handle);

return MemoryReader.ReadStringAndDestroy(handle);
}
Expand All @@ -149,7 +149,7 @@ public string String(Transaction transaction)
/// <returns>The length, in bytes, of the string stored in the instance.</returns>
public uint Length(Transaction transaction)
{
return TextChannel.Length(this.GetHandle(transaction), transaction.Handle);
return TextChannel.Length(GetHandle(transaction), transaction.Handle);
}

/// <summary>
Expand All @@ -159,7 +159,7 @@ public uint Length(Transaction transaction)
/// <returns>The subscription for the event. It may be used to unsubscribe later.</returns>
public IDisposable Observe(Action<TextEvent> action)
{
return this.onObserve.Subscribe(action);
return onObserve.Subscribe(action);
}

/// <summary>
Expand All @@ -177,10 +177,10 @@ public IDisposable Observe(Action<TextEvent> action)
public StickyIndex? StickyIndex(Transaction transaction, uint index, StickyAssociationType associationType)
{
var handle = StickyIndexChannel.FromIndex(
this.GetHandle(transaction),
GetHandle(transaction),
transaction.Handle,
index,
(sbyte) associationType);
(sbyte)associationType);

return handle != nint.Zero ? new StickyIndex(handle) : null;
}
Expand Down
6 changes: 3 additions & 3 deletions YDotNet/Document/Types/XmlElements/XmlElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ namespace YDotNet.Document.Types.XmlElements;
/// </summary>
public class XmlElement : XmlFragment
{
private readonly EventSubscriber<XmlElementEvent> onObserve;
private readonly EventSubscriberFromId<XmlElementEvent> onObserve;

internal XmlElement(nint handle, Doc doc, bool isDeleted)
: base(handle, doc, isDeleted)
{
onObserve = new EventSubscriber<XmlElementEvent>(
onObserve = new EventSubscriberFromId<XmlElementEvent>(
doc.EventManager,
handle,
this,
(xmlElement, action) =>
{
XmlElementChannel.ObserveCallback callback = (_, eventHandle) =>
Expand Down
6 changes: 3 additions & 3 deletions YDotNet/Document/Types/XmlFragments/XmlFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ namespace YDotNet.Document.Types.XmlFragments;
/// </summary>
public class XmlFragment : Branch
{
private readonly EventSubscriber<XmlFragmentEvent> onObserve;
private readonly EventSubscriberFromId<XmlFragmentEvent> onObserve;

internal XmlFragment(nint handle, Doc doc, bool isDeleted)
: base(handle, doc, isDeleted)
{
onObserve = new EventSubscriber<XmlFragmentEvent>(
onObserve = new EventSubscriberFromId<XmlFragmentEvent>(
doc.EventManager,
handle,
this,
(xmlFragment, action) =>
{
XmlElementChannel.ObserveCallback callback = (_, eventHandle) =>
Expand Down
6 changes: 3 additions & 3 deletions YDotNet/Document/Types/XmlTexts/XmlText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ namespace YDotNet.Document.Types.XmlTexts;
/// </summary>
public class XmlText : Branch
{
private readonly EventSubscriber<XmlTextEvent> onObserve;
private readonly EventSubscriberFromId<XmlTextEvent> onObserve;

internal XmlText(nint handle, Doc doc, bool isDeleted)
: base(handle, doc, isDeleted)
{
onObserve = new EventSubscriber<XmlTextEvent>(
onObserve = new EventSubscriberFromId<XmlTextEvent>(
doc.EventManager,
handle,
this,
(xmlText, action) =>
{
XmlTextChannel.ObserveCallback callback = (_, eventHandle) =>
Expand Down

0 comments on commit 3deb5d5

Please sign in to comment.