Skip to content

Commit

Permalink
Temporary hack for fixing dotnet/aspnetcore#11159
Browse files Browse the repository at this point in the history
  • Loading branch information
SamProf committed Jun 13, 2019
1 parent 1aaceb5 commit 367bf39
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 20 deletions.
37 changes: 32 additions & 5 deletions src/MatBlazor/Components/Base/BaseMatComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ public abstract class BaseMatComponent : ComponentBase, IBaseMatComponent, IDisp

public string MatBlazorId = IdGeneratorHelper.Generate("matBlazor_id_");

[Inject]
protected IJSRuntime Js { get; set; }

[Inject]
protected IComponentContext ComponentContext { get; set; }


private Queue<Func<Task>> afterRenderCallQuene = new Queue<Func<Task>>();

Expand Down Expand Up @@ -68,5 +64,36 @@ public virtual void Dispose()
}

protected bool Disposed { get; private set; }


[Inject]
protected IJSRuntime Js { get; set; }

#region Hack to fix https://github.com/aspnet/AspNetCore/issues/11159

public static object CreateDotNetObjectRefSyncObj = new object();

protected DotNetObjectRef<T> CreateDotNetObjectRef<T>(T value) where T : class
{
lock (CreateDotNetObjectRefSyncObj)
{
JSRuntime.SetCurrentJSRuntime(Js);
return DotNetObjectRef.Create(value);
}
}

protected void DisposeDotNetObjectRef<T>(DotNetObjectRef<T> value) where T : class
{
if (value != null)
{
lock (CreateDotNetObjectRefSyncObj)
{
JSRuntime.SetCurrentJSRuntime(Js);
value.Dispose();
}
}
}

#endregion
}
}
1 change: 1 addition & 0 deletions src/MatBlazor/Components/Base/BaseMatDomComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ public string Style

private string _class;
private string _style;

}
}
8 changes: 3 additions & 5 deletions src/MatBlazor/Components/MatDatePicker/BaseMatDatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,24 @@ public override ElementRef Ref
}



private DotNetObjectRef<BaseMatDatePicker> dotNetObjectRef;

public BaseMatDatePicker()
{
dotNetObjectRef = DotNetObjectRef.Create(this);
CallAfterRender(async () =>
{
dotNetObjectRef = dotNetObjectRef ?? CreateDotNetObjectRef(this);
Js.InvokeAsync<object>("matBlazor.matDatePicker.init", Ref, dotNetObjectRef, Value);
});
}

public override void Dispose()
public override void Dispose()
{
base.Dispose();
dotNetObjectRef.Dispose();
DisposeDotNetObjectRef(dotNetObjectRef);
}



[JSInvokable]
public async Task MatDatePickerOnChangeHandler(DateTime?[] value)
{
Expand Down
4 changes: 2 additions & 2 deletions src/MatBlazor/Components/MatDialog/BaseMatDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public BaseMatDialog()
ClassMapper.Add("mdc-dialog");
CallAfterRender(async () =>
{
dotNetObjectRef = dotNetObjectRef ?? DotNetObjectRef.Create(this);
dotNetObjectRef = dotNetObjectRef ?? CreateDotNetObjectRef(this);
await Js.InvokeAsync<object>("matBlazor.matDialog.init", Ref, dotNetObjectRef);
});
}

public override void Dispose()
{
base.Dispose();
dotNetObjectRef?.Dispose();
DisposeDotNetObjectRef(dotNetObjectRef);
}

[JSInvokable]
Expand Down
5 changes: 3 additions & 2 deletions src/MatBlazor/Components/MatDrawer/BaseMatDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public bool Opened
private DotNetObjectRef<BaseMatDrawer> dotNetObjectRef;
public BaseMatDrawer()
{
dotNetObjectRef = DotNetObjectRef.Create(this);

ClassMapper
.Add("mdc-drawer")
.Add("mat-drawer")
Expand All @@ -61,14 +61,15 @@ public BaseMatDrawer()

this.CallAfterRender(async () =>
{
dotNetObjectRef = dotNetObjectRef ?? CreateDotNetObjectRef(this);
await Js.InvokeAsync<object>("matBlazor.matDrawer.init", Ref, dotNetObjectRef);
});
}

public override void Dispose()
{
base.Dispose();
dotNetObjectRef.Dispose();
DisposeDotNetObjectRef(dotNetObjectRef);
}


Expand Down
4 changes: 2 additions & 2 deletions src/MatBlazor/Components/MatHidden/BaseMatHidden.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public BaseMatHidden()

CallAfterRender(async () =>
{
dotNetObjectRef = dotNetObjectRef ?? DotNetObjectRef.Create(this);
dotNetObjectRef = dotNetObjectRef ?? CreateDotNetObjectRef(this);
await Js.InvokeAsync<object>("matBlazor.matHidden.init", MatBlazorId, dotNetObjectRef);
await UpdateVisible();
});
Expand All @@ -69,7 +69,7 @@ public async Task MatHiddenUpdateHandler(decimal innerWidth)
public override void Dispose()
{
base.Dispose();
dotNetObjectRef?.Dispose();
DisposeDotNetObjectRef(dotNetObjectRef);
Js.InvokeAsync<object>("matBlazor.matHidden.destroy", MatBlazorId);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/MatBlazor/Components/MatSlider/BaseMatSlider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ public class BaseMatSlider : BaseMatDomComponent
public BaseMatSlider()
{
jsHelper = new JsHelper(this);
dotNetObjectRef = DotNetObjectRef.Create(jsHelper);

ClassMapper
.Add("mdc-slider")
.If("mdc-slider--discrete", () => Discrete);
CallAfterRender(async () =>
{
dotNetObjectRef = dotNetObjectRef??CreateDotNetObjectRef(jsHelper);
await Js.InvokeAsync<object>("matBlazor.matSlider.init", Ref, dotNetObjectRef);
});
}

public override void Dispose()
{
base.Dispose();
dotNetObjectRef.Dispose();
DisposeDotNetObjectRef(dotNetObjectRef);
}


Expand Down
4 changes: 2 additions & 2 deletions src/MatBlazor/Components/MatSnackbar/BaseMatSnackbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public BaseMatSnackbar()
.If("mdc-snackbar--leading", () => Leading);
CallAfterRender(async () =>
{
dotNetObjectRef = dotNetObjectRef ?? DotNetObjectRef.Create(this);
dotNetObjectRef = dotNetObjectRef ?? CreateDotNetObjectRef(this);
await Js.InvokeAsync<object>("matBlazor.matSnackbar.init", Ref, dotNetObjectRef);
});
}

public override void Dispose()
{
base.Dispose();
dotNetObjectRef?.Dispose();
DisposeDotNetObjectRef(dotNetObjectRef);
}

[JSInvokable]
Expand Down

0 comments on commit 367bf39

Please sign in to comment.