diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/FileUpload/BitFileUploadJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/FileUpload/BitFileUploadJsRuntimeExtensions.cs index 803396c26f..497ee7c0cd 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/FileUpload/BitFileUploadJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/FileUpload/BitFileUploadJsRuntimeExtensions.cs @@ -12,7 +12,7 @@ internal static ValueTask BitFileUploadReset(this IJSRuntime jsRu string uploadAddress, IReadOnlyDictionary uploadRequestHttpHeaders) { - return jsRuntime.InvokeAsync("BitBlazorUI.FileUpload.reset", id, dotnetObjectReference, element, uploadAddress, uploadRequestHttpHeaders); + return jsRuntime.Invoke("BitBlazorUI.FileUpload.reset", id, dotnetObjectReference, element, uploadAddress, uploadRequestHttpHeaders); } internal static ValueTask BitFileUploadUpload(this IJSRuntime jsRuntime, @@ -23,29 +23,29 @@ internal static ValueTask BitFileUploadUpload(this IJSRuntime jsRuntime, string? uploadUrl, IReadOnlyDictionary? httpHeaders) { - return (httpHeaders is null ? jsRuntime.InvokeVoidAsync("BitBlazorUI.FileUpload.upload", id, from, to, index, uploadUrl) - : jsRuntime.InvokeVoidAsync("BitBlazorUI.FileUpload.upload", id, from, to, index, uploadUrl, httpHeaders)); + return (httpHeaders is null ? jsRuntime.InvokeVoid("BitBlazorUI.FileUpload.upload", id, from, to, index, uploadUrl) + : jsRuntime.InvokeVoid("BitBlazorUI.FileUpload.upload", id, from, to, index, uploadUrl, httpHeaders)); } internal static ValueTask BitFileUploadPause(this IJSRuntime jsRuntime, string id, int index = -1) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.FileUpload.pause", id, index); + return jsRuntime.InvokeVoid("BitBlazorUI.FileUpload.pause", id, index); } internal static ValueTask BitFileUploadSetupDragDrop(this IJSRuntime jsRuntime, ElementReference dragDropZoneElement, ElementReference inputFileElement) { - return jsRuntime.InvokeAsync("BitBlazorUI.FileUpload.setupDragDrop", dragDropZoneElement, inputFileElement); + return jsRuntime.Invoke("BitBlazorUI.FileUpload.setupDragDrop", dragDropZoneElement, inputFileElement); } internal static ValueTask BitFileUploadBrowse(this IJSRuntime jsRuntime, ElementReference inputFileElement) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.FileUpload.browse", inputFileElement); + return jsRuntime.InvokeVoid("BitBlazorUI.FileUpload.browse", inputFileElement); } internal static ValueTask BitFileUploadDispose(this IJSRuntime jsRuntime, string id) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.FileUpload.dispose", id); + return jsRuntime.InvokeVoid("BitBlazorUI.FileUpload.dispose", id); } } diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/OtpInput/BitOtpInputJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/OtpInput/BitOtpInputJsRuntimeExtensions.cs index a558d5de0f..8f9986a5ec 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/OtpInput/BitOtpInputJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/OtpInput/BitOtpInputJsRuntimeExtensions.cs @@ -4,6 +4,6 @@ internal static class BitOtpInputJsRuntimeExtensions { internal static ValueTask BitOtpInputSetup(this IJSRuntime jsRuntime, DotNetObjectReference obj, ElementReference input) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.OtpInput.setup", obj, input); + return jsRuntime.InvokeVoid("BitBlazorUI.OtpInput.setup", obj, input); } } diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SearchBox/BitSearchBoxJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SearchBox/BitSearchBoxJsRuntimeExtensions.cs index d28c6f1ec4..2e35b6e09c 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SearchBox/BitSearchBoxJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SearchBox/BitSearchBoxJsRuntimeExtensions.cs @@ -4,6 +4,6 @@ internal static class BitSearchBoxJsRuntimeExtensions { internal static ValueTask BitSearchBoxMoveCursorToEnd(this IJSRuntime jsRuntime, ElementReference input) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.SearchBox.moveCursorToEnd", input); + return jsRuntime.InvokeVoid("BitBlazorUI.SearchBox.moveCursorToEnd", input); } } diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/_Pickers/CircularTimePicker/BitCircularTimePickerJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/_Pickers/CircularTimePicker/BitCircularTimePickerJsRuntimeExtensions.cs index 436a5ca9c2..38150ac314 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/_Pickers/CircularTimePicker/BitCircularTimePickerJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/_Pickers/CircularTimePicker/BitCircularTimePickerJsRuntimeExtensions.cs @@ -4,16 +4,16 @@ internal static class BitCircularTimePickerJsRuntimeExtensions { internal static ValueTask BitCircularTimePickerRegisterPointerUp(this IJSRuntime js, DotNetObjectReference obj, string methodName) { - return js.InvokeAsync("BitBlazorUI.CircularTimePicker.registerEvent", "pointerup", obj, methodName); + return js.Invoke("BitBlazorUI.CircularTimePicker.registerEvent", "pointerup", obj, methodName); } internal static ValueTask BitCircularTimePickerRegisterPointerMove(this IJSRuntime js, DotNetObjectReference obj, string methodName) { - return js.InvokeAsync("BitBlazorUI.CircularTimePicker.registerEvent", "pointermove", obj, methodName); + return js.Invoke("BitBlazorUI.CircularTimePicker.registerEvent", "pointermove", obj, methodName); } internal static ValueTask BitCircularTimePickerAbort(this IJSRuntime jSRuntime, string? abortControllerId, bool dispose = false) { - return jSRuntime.InvokeVoidAsync("BitBlazorUI.CircularTimePicker.abort", abortControllerId, dispose); + return jSRuntime.InvokeVoid("BitBlazorUI.CircularTimePicker.abort", abortControllerId, dispose); } } diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/_Pickers/ColorPicker/BitColorPickerJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/_Pickers/ColorPicker/BitColorPickerJsRuntimeExtensions.cs index d14811e990..9a70d74407 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/_Pickers/ColorPicker/BitColorPickerJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/_Pickers/ColorPicker/BitColorPickerJsRuntimeExtensions.cs @@ -4,16 +4,16 @@ internal static class BitColorPickerJsRuntimeExtensions { internal static ValueTask BitColorPickerRegisterPointerUp(this IJSRuntime js, DotNetObjectReference obj, string methodName) { - return js.InvokeAsync("BitBlazorUI.ColorPicker.registerEvent", "pointerup", obj, methodName); + return js.Invoke("BitBlazorUI.ColorPicker.registerEvent", "pointerup", obj, methodName); } internal static ValueTask BitColorPickerRegisterPointerMove(this IJSRuntime js, DotNetObjectReference obj, string methodName) { - return js.InvokeAsync("BitBlazorUI.ColorPicker.registerEvent", "pointermove", obj, methodName); + return js.Invoke("BitBlazorUI.ColorPicker.registerEvent", "pointermove", obj, methodName); } internal static ValueTask BitColorPickerAbort(this IJSRuntime jSRuntime, string? abortControllerId, bool dispose = false) { - return jSRuntime.InvokeVoidAsync("BitBlazorUI.ColorPicker.abort", abortControllerId, dispose); + return jSRuntime.InvokeVoid("BitBlazorUI.ColorPicker.abort", abortControllerId, dispose); } } diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Lists/Swiper/BitSwiperJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Components/Lists/Swiper/BitSwiperJsRuntimeExtensions.cs index 163e53ee99..6f43001e3e 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Lists/Swiper/BitSwiperJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Lists/Swiper/BitSwiperJsRuntimeExtensions.cs @@ -4,11 +4,11 @@ internal static class BitSwiperJsRuntimeExtensions { internal static ValueTask BitSwiperGetDimensions(this IJSRuntime jsRuntime, ElementReference root, ElementReference swiper) { - return jsRuntime.InvokeAsync("BitBlazorUI.Swiper.getDimensions", root, swiper); + return jsRuntime.Invoke("BitBlazorUI.Swiper.getDimensions", root, swiper); } internal static ValueTask BitSwiperRegisterPointerLeave(this IJSRuntime jsRuntime, ElementReference root, DotNetObjectReference dotnetObj) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.Swiper.registerPointerLeave", root, dotnetObj); + return jsRuntime.InvokeVoid("BitBlazorUI.Swiper.registerPointerLeave", root, dotnetObj); } } diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Modal/BitModalJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Modal/BitModalJsRuntimeExtensions.cs index 3e2ddf9f65..4bbdb16006 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Modal/BitModalJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Modal/BitModalJsRuntimeExtensions.cs @@ -4,11 +4,11 @@ internal static class BitModalJsRuntimeExtensions { internal static ValueTask BitModalSetupDragDrop(this IJSRuntime js, string id, string dragElementSelector) { - return js.InvokeVoidAsync("BitBlazorUI.Modal.setupDragDrop", id, dragElementSelector); + return js.InvokeVoid("BitBlazorUI.Modal.setupDragDrop", id, dragElementSelector); } internal static ValueTask BitModalRemoveDragDrop(this IJSRuntime js, string id, string dragElementSelector) { - return js.InvokeVoidAsync("BitBlazorUI.Modal.removeDragDrop", id, dragElementSelector); + return js.InvokeVoid("BitBlazorUI.Modal.removeDragDrop", id, dragElementSelector); } } diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Splitter/BitSplitter.razor.cs b/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Splitter/BitSplitter.razor.cs index bfbcc2c525..c534c4e283 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Splitter/BitSplitter.razor.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Splitter/BitSplitter.razor.cs @@ -111,8 +111,8 @@ protected override void RegisterCssStyles() private async Task ResetPaneDimensions() { - await _js.InvokeVoidAsync("BitSplitter.resetPaneDimensions", _firstPanelRef); - await _js.InvokeVoidAsync("BitSplitter.resetPaneDimensions", _secondPanelRef); + await _js.BitSplitterResetPaneDimensions(_firstPanelRef); + await _js.BitSplitterResetPaneDimensions(_secondPanelRef); } private async Task OnDraggingStart(double position) @@ -122,11 +122,11 @@ private async Task OnDraggingStart(double position) _initialPosition = position; - _initialFirstPanelWidth = await _js.InvokeAsync("BitSplitter.getSplitterWidth", _firstPanelRef); - _initialSecondPanelWidth = await _js.InvokeAsync("BitSplitter.getSplitterWidth", _secondPanelRef); + _initialFirstPanelWidth = await _js.BitSplitterGetSplitterWidth(_firstPanelRef); + _initialSecondPanelWidth = await _js.BitSplitterGetSplitterWidth(_secondPanelRef); - _initialFirstPanelHeight = await _js.InvokeAsync("BitSplitter.getSplitterHeight", _firstPanelRef); - _initialSecondPanelHeight = await _js.InvokeAsync("BitSplitter.getSplitterHeight", _secondPanelRef); + _initialFirstPanelHeight = await _js.BitSplitterGetSplitterHeight(_firstPanelRef); + _initialSecondPanelHeight = await _js.BitSplitterGetSplitterHeight(_secondPanelRef); } private async Task OnDragging(double position) @@ -139,15 +139,15 @@ private async Task OnDragging(double position) { var newPrimaryHeight = _initialFirstPanelHeight + delta; var newSecondaryHeight = _initialSecondPanelHeight - delta; - await _js.InvokeVoidAsync("BitSplitter.setSplitterHeight", _firstPanelRef, newPrimaryHeight); - await _js.InvokeVoidAsync("BitSplitter.setSplitterHeight", _secondPanelRef, newSecondaryHeight); + await _js.BitSplitterSetSplitterHeight(_firstPanelRef, newPrimaryHeight); + await _js.BitSplitterSetSplitterHeight(_secondPanelRef, newSecondaryHeight); } else { var newPrimaryWidth = _initialFirstPanelWidth + delta; var newSecondaryWidth = _initialSecondPanelWidth - delta; - await _js.InvokeVoidAsync("BitSplitter.setSplitterWidth", _firstPanelRef, newPrimaryWidth); - await _js.InvokeVoidAsync("BitSplitter.setSplitterWidth", _secondPanelRef, newSecondaryWidth); + await _js.BitSplitterSetSplitterWidth(_firstPanelRef, newPrimaryWidth); + await _js.BitSplitterSetSplitterWidth(_secondPanelRef, newSecondaryWidth); } } } @@ -157,7 +157,7 @@ private async Task OnDraggingEnd() _isDragging = false; ClassBuilder.Reset(); - await _js.InvokeVoidAsync("BitSplitter.handleSplitterDraggingEnd"); + await _js.BitSplitterHandleSplitterDraggingEnd(); } private async Task OnPointerDown(PointerEventArgs e) @@ -172,7 +172,7 @@ private async Task OnPointerMove(PointerEventArgs e) private async Task OnTouchStart(TouchEventArgs e) { - await _js.InvokeVoidAsync("BitSplitter.handleSplitterDragging", e); + await _js.BitSplitterHandleSplitterDragging(e); await OnDraggingStart(Vertical ? e.Touches[0].ClientY : e.Touches[0].ClientX); } diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Splitter/BitSplitter.ts b/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Splitter/BitSplitter.ts index 00d583c91b..7ef14df21e 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Splitter/BitSplitter.ts +++ b/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Splitter/BitSplitter.ts @@ -1,30 +1,32 @@ -class BitSplitter { - public static handleSplitterDragging(event: TouchEvent) { - document.body.style.overscrollBehavior = 'none'; - }; +namespace BitBlazorUI { + export class Splitter { + public static handleSplitterDragging(event: TouchEvent) { + document.body.style.overscrollBehavior = 'none'; + }; - public static handleSplitterDraggingEnd() { - document.body.style.overscrollBehavior = ''; - }; + public static handleSplitterDraggingEnd() { + document.body.style.overscrollBehavior = ''; + }; - public static getSplitterWidth(element: HTMLElement) { - return element.getBoundingClientRect().width; - }; + public static getSplitterWidth(element: HTMLElement) { + return element.getBoundingClientRect().width; + }; - public static setSplitterWidth(element: HTMLElement, width: number) { - element.style.width = width + 'px'; - }; + public static setSplitterWidth(element: HTMLElement, width: number) { + element.style.width = width + 'px'; + }; - public static getSplitterHeight(element: HTMLElement) { - return element.getBoundingClientRect().height; - }; + public static getSplitterHeight(element: HTMLElement) { + return element.getBoundingClientRect().height; + }; - public static setSplitterHeight(element: HTMLElement, height: number) { - element.style.height = height + 'px'; - }; - - public static resetPaneDimensions(element: HTMLElement) { - element.style.width = ''; - element.style.height = ''; - }; -} + public static setSplitterHeight(element: HTMLElement, height: number) { + element.style.height = height + 'px'; + }; + + public static resetPaneDimensions(element: HTMLElement) { + element.style.width = ''; + element.style.height = ''; + }; + } +} \ No newline at end of file diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Splitter/BitSplitterJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Splitter/BitSplitterJsRuntimeExtensions.cs new file mode 100644 index 0000000000..6dab2ebcd4 --- /dev/null +++ b/src/BlazorUI/Bit.BlazorUI/Components/Surfaces/Splitter/BitSplitterJsRuntimeExtensions.cs @@ -0,0 +1,39 @@ +namespace Bit.BlazorUI; + +internal static class BitSplitterJsRuntimeExtensions +{ + internal static ValueTask BitSplitterResetPaneDimensions(this IJSRuntime js, ElementReference element) + { + return js.InvokeVoid("BitBlazorUI.Splitter.resetPaneDimensions", element); + } + + internal static ValueTask BitSplitterGetSplitterWidth(this IJSRuntime js, ElementReference element) + { + return js.Invoke("BitBlazorUI.Splitter.getSplitterWidth", element); + } + + internal static ValueTask BitSplitterSetSplitterWidth(this IJSRuntime js, ElementReference element, double value) + { + return js.InvokeVoid("BitBlazorUI.Splitter.setSplitterWidth", element, value); + } + + internal static ValueTask BitSplitterGetSplitterHeight(this IJSRuntime js, ElementReference element) + { + return js.Invoke("BitBlazorUI.Splitter.getSplitterHeight", element); + } + + internal static ValueTask BitSplitterSetSplitterHeight(this IJSRuntime js, ElementReference element, double value) + { + return js.InvokeVoid("BitBlazorUI.Splitter.setSplitterHeight", element, value); + } + + internal static ValueTask BitSplitterHandleSplitterDragging(this IJSRuntime js, TouchEventArgs e) + { + return js.InvokeVoid("BitBlazorUI.Splitter.handleSplitterDragging", e); + } + + internal static ValueTask BitSplitterHandleSplitterDraggingEnd(this IJSRuntime js) + { + return js.InvokeVoid("BitBlazorUI.Splitter.handleSplitterDraggingEnd"); + } +} diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Utilities/Overlay/BitOverlayJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Components/Utilities/Overlay/BitOverlayJsRuntimeExtensions.cs index 8713c88769..6aa261ae33 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Utilities/Overlay/BitOverlayJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Utilities/Overlay/BitOverlayJsRuntimeExtensions.cs @@ -4,6 +4,6 @@ internal static class BitOverlayJsRuntimeExtensions { internal static ValueTask BitOverlayToggleScroll(this IJSRuntime jsRuntime, string scrollerSelector, bool isOpen) { - return jsRuntime.InvokeAsync("BitBlazorUI.Overlay.toggleScroll", scrollerSelector, isOpen); + return jsRuntime.Invoke("BitBlazorUI.Overlay.toggleScroll", scrollerSelector, isOpen); } } diff --git a/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/CalloutsJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/CalloutsJsRuntimeExtensions.cs index 80722ea220..ddc4d28696 100644 --- a/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/CalloutsJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/CalloutsJsRuntimeExtensions.cs @@ -22,26 +22,26 @@ internal static class CalloutsJsRuntimeExtensions bool setCalloutWidth, string rootCssClass) where T : class { - return jsRuntime.InvokeAsync("BitBlazorUI.Callouts.toggle", - dotnetObj, - componentId, - component, - calloutId, - callout, - isCalloutOpen, - responsiveMode, - dropDirection, - isRtl, - scrollContainerId, - scrollOffset, - headerId, - footerId, - setCalloutWidth, - rootCssClass); + return jsRuntime.Invoke("BitBlazorUI.Callouts.toggle", + dotnetObj, + componentId, + component, + calloutId, + callout, + isCalloutOpen, + responsiveMode, + dropDirection, + isRtl, + scrollContainerId, + scrollOffset, + headerId, + footerId, + setCalloutWidth, + rootCssClass); } internal static ValueTask ClearCallout(this IJSRuntime jsRuntime, string calloutId) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.Callouts.clear", calloutId); + return jsRuntime.InvokeVoid("BitBlazorUI.Callouts.clear", calloutId); } } diff --git a/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/IJSRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/IJSRuntimeExtensions.cs new file mode 100644 index 0000000000..47244a6012 --- /dev/null +++ b/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/IJSRuntimeExtensions.cs @@ -0,0 +1,81 @@ +using System.Diagnostics.CodeAnalysis; + +namespace Bit.BlazorUI; + +public static class IJSRuntimeExtensions +{ + /// + /// Only tries to Invoke the js call when the runtime is valid. + /// + public static async ValueTask InvokeVoid(this IJSRuntime jsRuntime, string identifier, params object?[]? args) + { + if (jsRuntime.IsRuntimeInvalid()) return; + + await jsRuntime.InvokeVoidAsync(identifier, args); + } + + /// + /// Only tries to Invoke the js call when the runtime is valid. + /// + public static async ValueTask InvokeVoid(this IJSRuntime jsRuntime, string identifier, TimeSpan timeout, params object?[]? args) + { + if (jsRuntime.IsRuntimeInvalid()) return; + + await jsRuntime.InvokeVoidAsync(identifier, timeout, args); + } + + /// + /// Only tries to Invoke the js call when the runtime is valid. + /// + public static async ValueTask InvokeVoid(this IJSRuntime jsRuntime, string identifier, CancellationToken cancellationToken, params object?[]? args) + { + if (jsRuntime.IsRuntimeInvalid()) return; + + await jsRuntime.InvokeVoidAsync(identifier, cancellationToken, args); + } + + + + /// + /// Only tries to Invoke the js call when the runtime is valid. + /// + public static ValueTask Invoke<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(this IJSRuntime jsRuntime, string identifier, params object?[]? args) + { + if (jsRuntime.IsRuntimeInvalid()) return default; + + return jsRuntime.InvokeAsync(identifier, args); + } + + /// + /// Only tries to Invoke the js call when the runtime is valid. + /// + public static ValueTask InvokeAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(this IJSRuntime jsRuntime, string identifier, TimeSpan timeout, params object?[]? args) + { + if (jsRuntime.IsRuntimeInvalid()) return default; + + return jsRuntime.InvokeAsync(identifier, timeout, args); + } + + /// + /// Only tries to Invoke the js call when the runtime is valid. + /// + public static ValueTask InvokeAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(this IJSRuntime jsRuntime, string identifier, CancellationToken cancellationToken, params object?[]? args) + { + if (jsRuntime.IsRuntimeInvalid()) return default; + + return jsRuntime.InvokeAsync(identifier, cancellationToken, args); + } + + + + public static bool IsRuntimeInvalid(this IJSRuntime jsRuntime) + { + var type = jsRuntime.GetType(); + + if (type.Name is "UnsupportedJavaScriptRuntime") return true; + + if (type.Name is not "RemoteJSRuntime") return false; // Blazor WASM/Hybrid + + return (bool)type.GetProperty("IsInitialized")!.GetValue(jsRuntime)! is false; + } +} diff --git a/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/ObserversJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/ObserversJsRuntimeExtensions.cs index a718642c90..dc10db3a8d 100644 --- a/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/ObserversJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/ObserversJsRuntimeExtensions.cs @@ -7,11 +7,11 @@ internal static class ObserversJsRuntimeExtensions [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ContentRect))] internal static ValueTask BitObserversRegisterResize(this IJSRuntime jsRuntime, ElementReference element, DotNetObjectReference obj, string method) where T : class { - return jsRuntime.InvokeAsync("BitBlazorUI.Observers.registerResize", element, obj, method); + return jsRuntime.Invoke("BitBlazorUI.Observers.registerResize", element, obj, method); } internal static ValueTask BitObserversUnregisterResize(this IJSRuntime jsRuntime, ElementReference element, string id, DotNetObjectReference obj) where T : class { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.Observers.unregisterResize", element, id, obj); + return jsRuntime.InvokeVoid("BitBlazorUI.Observers.unregisterResize", element, id, obj); } } diff --git a/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/UtilsJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/UtilsJsRuntimeExtensions.cs index 5dd79a45db..0fc3e46755 100644 --- a/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/UtilsJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Extensions/JsInterop/UtilsJsRuntimeExtensions.cs @@ -4,56 +4,56 @@ internal static class UtilsJsRuntimeExtensions { internal static ValueTask Log(this IJSRuntime jsRuntime, object value) { - return jsRuntime.InvokeVoidAsync("console.log", value); + return jsRuntime.InvokeVoid("console.log", value); } internal static ValueTask SetProperty(this IJSRuntime jsRuntime, ElementReference element, string property, object value) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.Utils.setProperty", element, property, value); + return jsRuntime.InvokeVoid("BitBlazorUI.Utils.setProperty", element, property, value); } internal static ValueTask GetProperty(this IJSRuntime jsRuntime, ElementReference element, string property) { - return jsRuntime.InvokeAsync("BitBlazorUI.Utils.getProperty", element, property); + return jsRuntime.Invoke("BitBlazorUI.Utils.getProperty", element, property); } internal static ValueTask GetClientHeight(this IJSRuntime jsRuntime, ElementReference element) { - return jsRuntime.InvokeAsync("BitBlazorUI.Utils.getClientHeight", element); + return jsRuntime.Invoke("BitBlazorUI.Utils.getClientHeight", element); } internal static ValueTask GetBoundingClientRect(this IJSRuntime jsRuntime, ElementReference element) { - return jsRuntime.InvokeAsync("BitBlazorUI.Utils.getBoundingClientRect", element); + return jsRuntime.Invoke("BitBlazorUI.Utils.getBoundingClientRect", element); } internal static ValueTask ScrollElementIntoView(this IJSRuntime jsRuntime, string targetElementId) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.Utils.scrollElementIntoView", targetElementId); + return jsRuntime.InvokeVoid("BitBlazorUI.Utils.scrollElementIntoView", targetElementId); } internal static ValueTask SelectText(this IJSRuntime jsRuntime, ElementReference element) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.Utils.selectText", element); + return jsRuntime.InvokeVoid("BitBlazorUI.Utils.selectText", element); } internal static ValueTask SetStyle(this IJSRuntime jsRuntime, ElementReference element, string key, string value) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.Utils.setStyle", element, key, value); + return jsRuntime.InvokeVoid("BitBlazorUI.Utils.setStyle", element, key, value); } internal static ValueTask PreventDefault(this IJSRuntime jsRuntime, ElementReference element, string @event) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.Utils.preventDefault", element, @event); + return jsRuntime.InvokeVoid("BitBlazorUI.Utils.preventDefault", element, @event); } internal static ValueTask GetComputedTransform(this IJSRuntime jsRuntime, ElementReference element) { - return jsRuntime.InvokeAsync("BitBlazorUI.Utils.getComputedTransform", element); + return jsRuntime.Invoke("BitBlazorUI.Utils.getComputedTransform", element); } internal static ValueTask ToggleOverflow(this IJSRuntime jsRuntime, string scrollerSelector, bool isHidden) { - return jsRuntime.InvokeAsync("BitBlazorUI.Utils.toggleOverflow", scrollerSelector, isHidden); + return jsRuntime.Invoke("BitBlazorUI.Utils.toggleOverflow", scrollerSelector, isHidden); } } diff --git a/src/BlazorUI/Bit.BlazorUI/Utils/Theme/BitThemeJsExtensions.cs b/src/BlazorUI/Bit.BlazorUI/Utils/Theme/BitThemeJsExtensions.cs index 8106cd8a68..b1b5a504d7 100644 --- a/src/BlazorUI/Bit.BlazorUI/Utils/Theme/BitThemeJsExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI/Utils/Theme/BitThemeJsExtensions.cs @@ -4,21 +4,21 @@ internal static class BitThemeJsExtensions { internal static ValueTask GetCurrentTheme(this IJSRuntime js) { - return js.InvokeAsync("BitTheme.get"); + return js.Invoke("BitTheme.get"); } internal static ValueTask ToggleThemeDarkLight(this IJSRuntime js) { - return js.InvokeAsync("BitTheme.toggleDarkLight"); + return js.Invoke("BitTheme.toggleDarkLight"); } internal static ValueTask SetTheme(this IJSRuntime js, string themeName) { - return js.InvokeVoidAsync("BitTheme.set", themeName); + return js.InvokeVoid("BitTheme.set", themeName); } internal static ValueTask ApplyBitTheme(this IJSRuntime js, Dictionary theme, ElementReference? element) { - return js.InvokeVoidAsync("BitTheme.applyBitTheme", theme, element); + return js.InvokeVoid("BitTheme.applyBitTheme", theme, element); } }