Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Mar 5, 2023
1 parent 12a9cc4 commit 16398c4
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Libs/ScriptContainer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.1.7-prerelease</Version>
<Version>1.1.8-prerelease</Version>
<Authors>artemiusgreat</Authors>
<Copyright>indemos.com</Copyright>
<PackageProjectUrl>http://indemos.com</PackageProjectUrl>
Expand Down
9 changes: 6 additions & 3 deletions Libs/ScriptControl.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ function ScriptModule(instance, options) {
};
};

this.onSize = (e) => {
this.onSize = () => {
clearTimeout(this.sizeScheduler);
this.sizeScheduler = setTimeout(() => {
this.serviceInstance.invokeMethodAsync("OnScriptSize", this.getDocBounds());
this
.serviceInstance
.invokeMethodAsync("OnSizeChange", this.getDocBounds())
.catch(o => this.unsubscribe());
}, options.interval);
};

Expand All @@ -31,7 +34,7 @@ function ScriptModule(instance, options) {
this.events.push({ element, e, done });
};

this.dispose = () => {
this.unsubscribe = () => {
this.events.forEach(o => o.element.removeEventListener(o.e, o.done));
};

Expand Down
2 changes: 1 addition & 1 deletion Libs/ScriptMessage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ScriptContainer
{
public class ScriptMessage
public struct ScriptMessage
{
public int Interval { get; set; }
public double X { get; set; }
Expand Down
63 changes: 21 additions & 42 deletions Libs/ScriptService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,33 @@ namespace ScriptContainer
/// <summary>
/// Singleton service
/// </summary>
public class ScriptService : IDisposable, IAsyncDisposable
public class ScriptService : IAsyncDisposable
{
/// <summary>
/// Script runtime
/// </summary>
private IJSRuntime _scripts = null;
private IJSRuntime _runtime;

/// <summary>
/// Script reference
/// </summary>
private IJSObjectReference _scriptModule = null;
private IJSObjectReference _scriptModule;

/// <summary>
/// Script instance
/// </summary>
private IJSObjectReference _scriptInstance = null;
private IJSObjectReference _scriptInstance;

/// <summary>
/// Service instance
/// </summary>
private DotNetObjectReference<ScriptService> _serviceInstance = null;
private DotNetObjectReference<ScriptService> _serviceInstance;

/// <summary>
/// Constructor
/// </summary>
/// <param name="scripts"></param>
public ScriptService(IJSRuntime scripts)
{
_scripts = scripts;
}
/// <param name="runtime"></param>
public ScriptService(IJSRuntime runtime) => _runtime = runtime;

/// <summary>
/// On size event
Expand All @@ -49,7 +46,7 @@ public ScriptService(IJSRuntime scripts)
/// Get document bounds
/// </summary>
/// <returns></returns>
public async Task<ScriptMessage> GetDocBounds()
public async Task<ScriptMessage?> GetDocBounds()
{
if (_scriptInstance is not null)
{
Expand All @@ -64,7 +61,7 @@ public async Task<ScriptMessage> GetDocBounds()
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
public async Task<ScriptMessage> GetElementBounds(ElementReference element)
public async Task<ScriptMessage?> GetElementBounds(ElementReference element)
{
if (_scriptInstance is not null)
{
Expand All @@ -90,7 +87,7 @@ public async Task<ScriptService> CreateModule(IDictionary<string, object> option
}

_serviceInstance = DotNetObjectReference.Create(this);
_scriptModule = await _scripts.InvokeAsync<IJSObjectReference>("import", "./_content/ScriptContainer/ScriptControl.razor.js");
_scriptModule = await _runtime.InvokeAsync<IJSObjectReference>("import", "./_content/ScriptContainer/ScriptControl.razor.js");
_scriptInstance = await _scriptModule.InvokeAsync<IJSObjectReference>("getScriptModule", _serviceInstance, options);

return this;
Expand All @@ -102,49 +99,31 @@ public async Task<ScriptService> CreateModule(IDictionary<string, object> option
/// <param name="message"></param>
/// <returns></returns>
[JSInvokable]
public Task<object> OnScriptSize(ScriptMessage message)
{
if (OnSize is not null)
{
OnSize(message);
}

return Task.FromResult<object>(0);
}

/// <summary>
/// Dispose
/// </summary>
public void Dispose() => Task.Run(DisposeAsync).ConfigureAwait(false).GetAwaiter().GetResult();
public void OnSizeChange(ScriptMessage message) => OnSize(message);

/// <summary>
/// Dispose
/// </summary>
/// <returns></returns>
public async ValueTask DisposeAsync()
{
var scriptModule = _scriptModule;
var scriptInstance = _scriptInstance;
var serviceInstance = _serviceInstance;
OnSize = o => { };

_scriptModule = null;
_scriptInstance = null;
_serviceInstance = null;

OnSize = null;

if (scriptInstance is not null)
if (_scriptInstance is not null)
{
await scriptInstance.InvokeVoidAsync("dispose");
await scriptInstance.DisposeAsync();
await _scriptInstance.DisposeAsync();
}

if (scriptModule is not null)
if (_scriptModule is not null)
{
await scriptModule.DisposeAsync();
await _scriptModule.DisposeAsync();
}

serviceInstance?.Dispose();
_serviceInstance?.Dispose();

_scriptModule = null;
_scriptInstance = null;
_serviceInstance = null;
}
}
}
41 changes: 22 additions & 19 deletions Samples/SampleClient/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
@page "/"
@using ScriptContainer
@implements IDisposable
@inject IJSRuntime scriptService
@inject IJSRuntime runtime

<div class="element-container">
<button @onclick="Setup">
Setup
</button>
<div class="doc">
<h2>Document Bounds</h2>
<ul>
Expand Down Expand Up @@ -43,37 +45,38 @@
{
if (setup)
{
ScaleService = new ScriptService(scriptService);

await ScaleService.CreateModule();
await GetBounds();

ScaleService.OnSize = async message => await GetBounds();
await Setup();
}

await base.OnAfterRenderAsync(setup);
}

protected async Task Setup()
{
ScaleService?.DisposeAsync();
ScaleService = new ScriptService(runtime);

await ScaleService.CreateModule();
await GetBounds();

ScaleService.OnSize = async message => await GetBounds();
}

protected async Task GetBounds()
{
var docBounds = await ScaleService.GetDocBounds();

DocW = docBounds.X;
DocH = docBounds.Y;
DocW = docBounds.Value.X;
DocH = docBounds.Value.Y;

var itemBoundsA = await ScaleService.GetElementBounds(ElementA);
var itemBoundsB = await ScaleService.GetElementBounds(ElementB);

ItemAW = itemBoundsA.X;
ItemAH = itemBoundsA.Y;
ItemBW = itemBoundsB.X;
ItemBH = itemBoundsB.Y;
ItemAW = itemBoundsA.Value.X;
ItemAH = itemBoundsA.Value.Y;
ItemBW = itemBoundsB.Value.X;
ItemBH = itemBoundsB.Value.Y;

await InvokeAsync(StateHasChanged);
}

public void Dispose()
{
ScaleService?.Dispose();
}
}
41 changes: 22 additions & 19 deletions Samples/SampleServer/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
@page "/"
@using ScriptContainer
@implements IDisposable
@inject IJSRuntime scriptService
@inject IJSRuntime runtime

<div class="element-container">
<button @onclick="Setup">
Setup
</button>
<div class="doc">
<h2>Document Bounds</h2>
<ul>
Expand Down Expand Up @@ -45,37 +47,38 @@
{
if (setup)
{
ScaleService = new ScriptService(scriptService);

await ScaleService.CreateModule();
await GetBounds();

ScaleService.OnSize = async message => await GetBounds();
await Setup();
}

await base.OnAfterRenderAsync(setup);
}

protected async Task Setup()
{
ScaleService?.DisposeAsync();
ScaleService = new ScriptService(runtime);

await ScaleService.CreateModule();
await GetBounds();

ScaleService.OnSize = async message => await GetBounds();
}

protected async Task GetBounds()
{
var docBounds = await ScaleService.GetDocBounds();

DocW = docBounds.X;
DocH = docBounds.Y;
DocW = docBounds.Value.X;
DocH = docBounds.Value.Y;

var itemBoundsA = await ScaleService.GetElementBounds(ElementA);
var itemBoundsB = await ScaleService.GetElementBounds(ElementB);

ItemAW = itemBoundsA.X;
ItemAH = itemBoundsA.Y;
ItemBW = itemBoundsB.X;
ItemBH = itemBoundsB.Y;
ItemAW = itemBoundsA.Value.X;
ItemAH = itemBoundsA.Value.Y;
ItemBW = itemBoundsB.Value.X;
ItemBH = itemBoundsB.Value.Y;

await InvokeAsync(StateHasChanged);
}

public void Dispose()
{
ScaleService?.Dispose();
}
}

0 comments on commit 16398c4

Please sign in to comment.