Skip to content

Commit

Permalink
Dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Feb 24, 2024
1 parent 8e8e90b commit 3aec503
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 28 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.2.2</Version>
<Version>1.2.3</Version>
<Authors>artemiusgreat</Authors>
<Copyright>indemos.com</Copyright>
<PackageProjectUrl>http://indemos.com</PackageProjectUrl>
Expand Down
8 changes: 0 additions & 8 deletions Libs/ScriptControl.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,6 @@ function ScriptModule(instance, options) {
o && o.disconnect();
this.observerMap[actionName] = null;
};

try {
this.unsubscribeFromSize(sizeObserverName);
this.subscribeToSize(document.body, sizeObserverName);
} catch (e) {
this.unsubscribe(sizeObserverName);
this.subscribe(window, "resize", sizeObserverName);
}
};

export function getScriptModule(instance, options) {
Expand Down
22 changes: 4 additions & 18 deletions Libs/ScriptService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ScriptContainer
/// <summary>
/// Singleton service
/// </summary>
public class ScriptService : IAsyncDisposable
public class ScriptService : IDisposable
{
/// <summary>
/// Script runtime
Expand Down Expand Up @@ -110,7 +110,7 @@ public async Task<string> SubscribeToSize(ElementReference element, string actio
/// <returns></returns>
public async Task<ScriptService> CreateModule(IDictionary<string, dynamic> options = null)
{
await DisposeAsync();
Dispose();

_serviceInstance = DotNetObjectReference.Create(this);
_scriptModule = await _runtime.InvokeAsync<IJSObjectReference>("import", "./_content/ScriptContainer/ScriptControl.razor.js");
Expand All @@ -131,29 +131,15 @@ public void OnChange(dynamic message, string actionName)
if (Actions.TryGetValue(actionName, out var action)) {
action(message);
}
}
}

/// <summary>
/// Dispose
/// </summary>
/// <returns></returns>
public async ValueTask DisposeAsync()
public virtual void Dispose()
{
if (_scriptInstance is not null)
{
await _scriptInstance.DisposeAsync();
}

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

_serviceInstance?.Dispose();

_scriptModule = null;
_scriptInstance = null;
_serviceInstance = null;
}
}
}
2 changes: 1 addition & 1 deletion Samples/SampleServer/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

protected async Task Setup()
{
ScaleService?.DisposeAsync();
ScaleService?.Dispose();
ScaleService = new ScriptService(runtime);
ScaleService.Actions[nameof(MessageEnum.Size)] = async message => await GetBounds();
ScaleService.Actions["Demo"] = async message => await GetBounds();
Expand Down

0 comments on commit 3aec503

Please sign in to comment.