Skip to content

Blazor Exit Lifecycle Method with Active WebSocket Connection #10098

@TheFanatr

Description

@TheFanatr

Purpose

Certain Blazor applications could benefit from having a lifecycle method for when the component is being navigated away from, while JavaScript interop is still available. Using the @implements IDisposable pattern will allow code to execute when the component is being disposed, but it would be nice if it would be possible to delay something like an unload to execute some final interop that requires an active connection. A possible usage of this could be logging something before the site is navigated away, or showing an alert from the server.

Suggestion

I'd like the addition of another lifecycle method which would be invoked and let to run to completion before the disposal of a component. The possible names for this method are OnBeforeDispose, OnExit, and OnClose, with async variants provided as well.

Alternatives

Another possibility would be the provisioning of a state control method called KeepAlive(ref object o), which, when invoked, will lock further execution of disposing logic until the provided object is null. An overload could also be provided for a TimeSpan instance for how long the process should be delayed. I think these solutions could end up being sub-optimal in terms of being misused, and could be considerably delicate, especially in the ref object parameter overload's case.

Additional Context

The reason I brought this up is because I tried logging something to the browser console in IDisposable.Dispose() and it did not work.

Metadata

Metadata

Labels

area-blazorIncludes: Blazor, Razor Components

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions