-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
60 changed files
with
1,621 additions
and
432 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using static Bootsharp.Instances; | ||
|
||
namespace Bootsharp.Common.Test; | ||
|
||
public class InstancesTest | ||
{ | ||
[Fact] | ||
public void ThrowsWhenGettingUnregisteredInstance () | ||
{ | ||
Assert.Throws<Error>(() => Get(0)); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsWhenDisposingUnregisteredInstance () | ||
{ | ||
Assert.Throws<Error>(() => Dispose(0)); | ||
} | ||
|
||
[Fact] | ||
public void CanRegisterGetAndDisposeInstance () | ||
{ | ||
var instance = new object(); | ||
var id = Register(instance); | ||
Assert.Same(instance, Get(id)); | ||
Dispose(id); | ||
Assert.Throws<Error>(() => Get(id)); | ||
} | ||
|
||
[Fact] | ||
public void GeneratesUniqueIdsOnEachRegister () | ||
{ | ||
Assert.NotEqual(Register(new object()), Register(new object())); | ||
} | ||
|
||
[Fact] | ||
public void ReusesIdOfDisposedInstance () | ||
{ | ||
var id = Register(new object()); | ||
Dispose(id); | ||
Assert.Equal(id, Register(new object())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace Bootsharp; | ||
|
||
/// <summary> | ||
/// Manages exported (C# -> JavaScript) instanced interop interfaces. | ||
/// </summary> | ||
public static class Instances | ||
{ | ||
private static readonly Dictionary<int, object> idToInstance = []; | ||
private static readonly Queue<int> idPool = []; | ||
private static int nextId = int.MinValue; | ||
|
||
/// <summary> | ||
/// Registers specified interop instance and associates it with unique ID. | ||
/// </summary> | ||
/// <param name="instance">The instance to register.</param> | ||
/// <returns>Unique ID associated with the registered instance.</returns> | ||
public static int Register (object instance) | ||
{ | ||
var id = idPool.Count > 0 ? idPool.Dequeue() : nextId++; | ||
idToInstance[id] = instance; | ||
return id; | ||
} | ||
|
||
/// <summary> | ||
/// Resolves registered instance by the specified ID. | ||
/// </summary> | ||
/// <param name="id">Unique ID of the instance to resolve.</param> | ||
public static object Get (int id) | ||
{ | ||
if (!idToInstance.TryGetValue(id, out var instance)) | ||
throw new Error($"Failed to resolve exported interop instance with '{id}' ID: not registered."); | ||
return instance; | ||
} | ||
|
||
/// <summary> | ||
/// Notifies that interop instance is no longer used on JavaScript side | ||
/// (eg, was garbage collected) and can be released on C# side as well. | ||
/// </summary> | ||
/// <param name="id">ID of the disposed interop instance.</param> | ||
public static void Dispose (int id) | ||
{ | ||
if (!idToInstance.Remove(id)) | ||
throw new Error($"Failed to dispose exported interop instance with '{id}' ID: not registered."); | ||
idPool.Enqueue(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.