Skip to content

Commit

Permalink
Lazy load JS modules (#3159)
Browse files Browse the repository at this point in the history
* Lazy loads the JS modules

* Fix tests

* Safer dispose

* Check element

* Null checks

* Null checks

* Remove duplicate AddDataGrid setup

* Null check for button
  • Loading branch information
stsrki authored Nov 29, 2021
1 parent 733e89a commit 8a7cd8a
Show file tree
Hide file tree
Showing 29 changed files with 187 additions and 88 deletions.
32 changes: 25 additions & 7 deletions Source/Blazorise/Modules/BaseJSModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#region Using directives
using System;
using System.Threading.Tasks;
using Blazorise.Extensions;
using Microsoft.JSInterop;
#endregion

Expand All @@ -24,7 +25,7 @@ public abstract class BaseJSModule : IBaseJSModule, IAsyncDisposable
/// <summary>
/// Awaitable module instance.
/// </summary>
protected Task<IJSObjectReference> moduleTask;
private Lazy<Task<IJSObjectReference>> lazyModuleTask;

#endregion

Expand All @@ -39,6 +40,17 @@ public BaseJSModule( IJSRuntime jsRuntime, IVersionProvider versionProvider )
{
this.jsRuntime = jsRuntime;
this.versionProvider = versionProvider;

lazyModuleTask = new Lazy<Task<IJSObjectReference>>( LazyModuleFactory );
}

/// <summary>
/// Handles the loading of the JS module.
/// </summary>
/// <returns>An awaitable JS module reference.</returns>
protected virtual Task<IJSObjectReference> LazyModuleFactory()
{
return jsRuntime.InvokeAsync<IJSObjectReference>( "import", ModuleFileName ).AsTask();
}

#endregion
Expand All @@ -65,12 +77,19 @@ protected virtual async ValueTask DisposeAsync( bool disposing )

if ( disposing )
{
if ( moduleTask != null )
if ( lazyModuleTask != null )
{
var moduleInstance = await moduleTask;
await moduleInstance.DisposeAsync();
var moduleTask = lazyModuleTask.Value;

if ( moduleTask != null )
{
var moduleInstance = await moduleTask;
await moduleInstance.SafeDisposeAsync();

moduleTask = null;
}

moduleTask = null;
lazyModuleTask = null;
}
}
}
Expand All @@ -91,8 +110,7 @@ protected virtual async ValueTask DisposeAsync( bool disposing )
protected bool AsyncDisposed { get; private set; }

/// <inheritdoc/>
public Task<IJSObjectReference> Module
=> moduleTask ??= jsRuntime.InvokeAsync<IJSObjectReference>( "import", ModuleFileName ).AsTask();
public Task<IJSObjectReference> Module => lazyModuleTask.Value;

/// <inheritdoc/>
public abstract string ModuleFileName { get; }
Expand Down
16 changes: 4 additions & 12 deletions Source/Blazorise/Modules/JSBreakpointModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,17 @@ public virtual async ValueTask RegisterBreakpoint( DotNetObjectReference<Breakpo
/// <inheritdoc/>
public virtual async ValueTask UnregisterBreakpoint( IBreakpointActivator component )
{
if ( moduleTask != null )
{
var moduleInstance = await moduleTask;
var moduleInstance = await Module;

await moduleInstance.InvokeVoidAsync( "unregisterBreakpointComponent", component.ElementId );
}
await moduleInstance.InvokeVoidAsync( "unregisterBreakpointComponent", component.ElementId );
}

/// <inheritdoc/>
public virtual async ValueTask<string> GetBreakpoint()
{
if ( moduleTask != null )
{
var moduleInstance = await moduleTask;

return await moduleInstance.InvokeAsync<string>( "getBreakpoint" );
}
var moduleInstance = await Module;

return await Task.FromResult<string>( null );
return await moduleInstance.InvokeAsync<string>( "getBreakpoint" );
}

#endregion
Expand Down
7 changes: 2 additions & 5 deletions Source/Blazorise/Modules/JSButtonModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ public virtual async ValueTask Initialize( ElementReference elementRef, string e
/// <inheritdoc/>
public virtual async ValueTask Destroy( ElementReference elementRef, string elementId )
{
if ( moduleTask != null )
{
var moduleInstance = await moduleTask;
var moduleInstance = await Module;

await moduleInstance.InvokeVoidAsync( "destroy", elementRef, elementId );
}
await moduleInstance.InvokeVoidAsync( "destroy", elementRef, elementId );
}

#endregion
Expand Down
7 changes: 2 additions & 5 deletions Source/Blazorise/Modules/JSClosableModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ public virtual async ValueTask Register( DotNetObjectReference<CloseActivatorAda
/// <inheritdoc/>
public virtual async ValueTask Unregister( ICloseActivator component )
{
if ( moduleTask != null )
{
var moduleInstance = await moduleTask;
var moduleInstance = await Module;

await moduleInstance.InvokeVoidAsync( "unregisterClosableComponent", component.ElementRef );
}
await moduleInstance.InvokeVoidAsync( "unregisterClosableComponent", component.ElementRef );
}

#endregion
Expand Down
7 changes: 2 additions & 5 deletions Source/Blazorise/Modules/JSMemoEditModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ public virtual async ValueTask Initialize( ElementReference elementRef, string e
/// <inheritdoc/>
public virtual async ValueTask Destroy( ElementReference elementRef, string elementId )
{
if ( moduleTask != null )
{
var moduleInstance = await moduleTask;
var moduleInstance = await Module;

await moduleInstance.InvokeVoidAsync( "destroy", elementRef, elementId );
}
await moduleInstance.InvokeVoidAsync( "destroy", elementRef, elementId );
}

/// <inheritdoc/>
Expand Down
7 changes: 2 additions & 5 deletions Source/Blazorise/Modules/JSModalModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ public virtual async ValueTask OpenModal( ElementReference elementRef, bool scro
/// <inheritdoc/>
public virtual async ValueTask CloseModal( ElementReference elementRef )
{
if ( moduleTask != null )
{
var moduleInstance = await moduleTask;
var moduleInstance = await Module;

await moduleInstance.InvokeVoidAsync( "close", elementRef );
}
await moduleInstance.InvokeVoidAsync( "close", elementRef );
}

#endregion
Expand Down
14 changes: 4 additions & 10 deletions Source/Blazorise/Modules/JSNumericEditModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,17 @@ public virtual async ValueTask Initialize( DotNetObjectReference<NumericEditAdap
/// <inheritdoc/>
public virtual async ValueTask Destroy( ElementReference elementRef, string elementId )
{
if ( moduleTask != null )
{
var moduleInstance = await moduleTask;
var moduleInstance = await Module;

await moduleInstance.InvokeVoidAsync( "destroy", elementRef, elementId );
}
await moduleInstance.InvokeVoidAsync( "destroy", elementRef, elementId );
}

/// <inheritdoc/>
public virtual async ValueTask UpdateOptions( ElementReference elementRef, string elementId, object options )
{
if ( moduleTask != null )
{
var moduleInstance = await moduleTask;
var moduleInstance = await Module;

await moduleInstance.InvokeVoidAsync( "updateOptions", elementRef, elementId, options );
}
await moduleInstance.InvokeVoidAsync( "updateOptions", elementRef, elementId, options );
}

#endregion
Expand Down
7 changes: 2 additions & 5 deletions Source/Blazorise/Modules/JSTextEditModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ public virtual async ValueTask Initialize( ElementReference elementRef, string e
/// <inheritdoc/>
public virtual async ValueTask Destroy( ElementReference elementRef, string elementId )
{
if ( moduleTask != null )
{
var moduleInstance = await moduleTask;
var moduleInstance = await Module;

await moduleInstance.InvokeVoidAsync( "destroy", elementRef, elementId );
}
await moduleInstance.InvokeVoidAsync( "destroy", elementRef, elementId );
}

#endregion
Expand Down
14 changes: 4 additions & 10 deletions Source/Blazorise/Modules/JSTooltipModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,17 @@ public virtual async ValueTask Initialize( ElementReference elementRef, string e
/// <inheritdoc/>
public virtual async ValueTask Destroy( ElementReference elementRef, string elementId )
{
if ( moduleTask != null )
{
var moduleInstance = await moduleTask;
var moduleInstance = await Module;

await moduleInstance.InvokeVoidAsync( "destroy", elementRef, elementId );
}
await moduleInstance.InvokeVoidAsync( "destroy", elementRef, elementId );
}

/// <inheritdoc/>
public virtual async ValueTask UpdateContent( ElementReference elementRef, string elementId, string content )
{
if ( moduleTask != null )
{
var moduleInstance = await moduleTask;
var moduleInstance = await Module;

await moduleInstance.InvokeVoidAsync( "updateContent", elementId );
}
await moduleInstance.InvokeVoidAsync( "updateContent", elementId );
}

#endregion
Expand Down
11 changes: 9 additions & 2 deletions Source/Blazorise/wwwroot/button.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const _instances = [];
import { getRequiredElement } from "./utilities.js";

const _instances = [];

export function initialize(element, elementId, options) {
element = getRequiredElement(element, elementId);

if (!element)
return;

_instances[elementId] = new ButtonInfo(element, elementId, options);

if (element.type === "submit") {
if (element && element.type === "submit") {
element.addEventListener("click", (e) => {
click(_instances[elementId], e);
});
Expand Down
5 changes: 5 additions & 0 deletions Source/Blazorise/wwwroot/colorPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import * as utilities from "./utilities.js";
const _instancesInfos = [];

export function initialize(dotnetAdapter, element, elementId, options) {
element = utilities.getRequiredElement(element, elementId);

if (!element)
return;

const picker = Pickr.create({
el: element,
theme: 'monolith', // or 'monolith', or 'nano'
Expand Down
5 changes: 5 additions & 0 deletions Source/Blazorise/wwwroot/datePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import * as utilities from "./utilities.js";
const _pickers = [];

export function initialize(element, elementId, options) {
element = utilities.getRequiredElement(element, elementId);

if (!element)
return;

function mutationObserverCallback(mutationsList, observer) {
mutationsList.forEach(mutation => {
if (mutation.attributeName === 'class') {
Expand Down
11 changes: 10 additions & 1 deletion Source/Blazorise/wwwroot/fileEdit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const _instances = [];
import { getRequiredElement } from "./utilities.js";

const _instances = [];

export function initialize(adapter, element, elementId) {
element = getRequiredElement(element, elementId);

if (!element)
return;

var nextFileId = 0;

// save an instance of adapter
Expand Down Expand Up @@ -38,6 +45,8 @@ export function destroy(element, elementId) {
}

export function reset(element, elementId) {
element = getRequiredElement(element, elementId);

if (element) {
element.value = '';

Expand Down
3 changes: 3 additions & 0 deletions Source/Blazorise/wwwroot/inputMask.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ let _instances = [];
export function initialize(dotnetAdapter, element, elementId, options) {
element = getRequiredElement(element, elementId);

if (!element)
return;

const maskOptions = options.mask ? { mask: options.mask } : {};
const regexOptions = options.mask ? { regex: options.regex } : {};
const aliasOptions = options.alias ? { alias: options.alias, inputFormat: options.inputFormat, outputFormat: options.outputFormat } : {};
Expand Down
6 changes: 6 additions & 0 deletions Source/Blazorise/wwwroot/memoEdit.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Behave } from "./vendors/Behave.js";
import { getRequiredElement } from "./utilities.js";

const _instances = [];

export function initialize(element, elementId, options) {
element = getRequiredElement(element, elementId);

if (!element)
return;

const replaceTab = options.replaceTab || false;
const tabSize = options.tabSize || 4;
const softTabs = options.tabSize || true;
Expand Down
6 changes: 6 additions & 0 deletions Source/Blazorise/wwwroot/numericEdit.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { NumericMaskValidator } from "./validators/NumericMaskValidator.js";
import { getRequiredElement } from "./utilities.js";

let _instances = [];

export function initialize(dotnetAdapter, element, elementId, options) {
element = getRequiredElement(element, elementId);

if (!element)
return;

const instance = new NumericMaskValidator(dotnetAdapter, element, elementId, options);

_instances[elementId] = instance;
Expand Down
Loading

0 comments on commit 8a7cd8a

Please sign in to comment.