|
1 | 1 | @page "/counter" |
| 2 | +@using SpawnDev.BlazorJS.JSObjects |
2 | 3 | @implements IDisposable |
3 | 4 |
|
| 5 | + |
4 | 6 | <PageTitle>Counter</PageTitle> |
5 | 7 |
|
6 | 8 | <h1>Window Shared Counter</h1> |
7 | 9 | <p> |
8 | | - Open this page in another window. You will see that the new window will have the same counter value as this window. |
9 | | - Clicking the button on either window will increment the counter on all windows. |
| 10 | + Open this page in another window. You will see that the new window will have the same counter value as this window. |
| 11 | + Clicking the button on either window will increment the counter on all windows. |
10 | 12 | </p> |
11 | 13 | <p> |
12 | | - This demonstrates calling a static method in another window inside a component.<br/> |
| 14 | + This demonstrates calling a static method in another window inside a component.<br /> |
13 | 15 | </p> |
14 | 16 |
|
15 | 17 | <p role="status">Current count: @currentCount</p> |
16 | 18 |
|
| 19 | +<p> |
| 20 | + <div>Instance count: @(WebWorkerService.Instances.Count)</div> |
| 21 | + @foreach (var instance in WebWorkerService.Instances) |
| 22 | + { |
| 23 | + var thisInstance = WebWorkerService.InstanceId == instance.Info.InstanceId; |
| 24 | + var name = string.IsNullOrEmpty(instance.Info.Name) ? "[UNNAMED]" : instance.Info.Name; |
| 25 | + var scope = instance.Info.Scope.ToString(); |
| 26 | + if (instance.Info.IFrameWorker) scope += " IFrame"; |
| 27 | + if (thisInstance) name += " (THIS)"; |
| 28 | + <div style="padding: 5px;"> |
| 29 | + <div>Name: @name</div> |
| 30 | + <div>Scope: @scope</div> |
| 31 | + <div>InstanceId: @instance.Info.InstanceId</div> |
| 32 | + <div>ParentId: @instance.Info.ParentInstanceId</div> |
| 33 | + <div>OwnerId: @instance.Info.OwnerId</div> |
| 34 | + <div><button class="btn btn-primary" @onclick="@(() => CloseIt(instance))">❌</button></div> |
| 35 | + </div> |
| 36 | + } |
| 37 | +</p> |
| 38 | + |
17 | 39 | <button class="btn btn-primary" @onclick="IncrementCount">Increment Count</button> |
18 | 40 |
|
19 | 41 | <button class="btn btn-primary" @onclick="OpenNewWindow">Open New Window</button> |
| 42 | +<button class="btn btn-primary" @onclick="OpenNewIFrameWorker">New IFrame Worker</button> |
| 43 | +<button class="btn btn-primary" @onclick="OpenNewWebWorker">New WebWorker</button> |
| 44 | +<button class="btn btn-primary" @onclick="OpenNewSharedWorker">New SharedWebWorker</button> |
20 | 45 |
|
21 | 46 | @code { |
22 | 47 | [Inject] |
23 | 48 | WebWorkerService WebWorkerService { get; set; } = default!; |
24 | 49 |
|
| 50 | + static void CloseThis() |
| 51 | + { |
| 52 | + var JS = BlazorJSRuntime.JS; |
| 53 | + JS.Log("Closing..."); |
| 54 | + Toolbox.Async.Run(async () => |
| 55 | + { |
| 56 | + await Task.Delay(1000); |
| 57 | + using var frameElement = JS.Get<HTMLIFrameElement>("frameElement"); |
| 58 | + if (frameElement != null) |
| 59 | + { |
| 60 | + frameElement.Remove(); |
| 61 | + } |
| 62 | + try |
| 63 | + { |
| 64 | + JS.CallVoid("close"); |
| 65 | + } |
| 66 | + catch { } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
25 | 70 | private int currentCount = 0; |
26 | 71 | // holds the running instance of this component, if one |
27 | 72 | static Counter? instance = null; |
|
32 | 77 | var nmt = true; |
33 | 78 | } |
34 | 79 |
|
| 80 | + async Task CloseIt(AppInstance instance) |
| 81 | + { |
| 82 | + await instance.Run(() => CloseThis()); |
| 83 | + } |
| 84 | + |
| 85 | + async Task OpenNewIFrameWorker() |
| 86 | + { |
| 87 | + var instance = await WebWorkerService.GetIFrameWorker(); |
| 88 | + var nmt = true; |
| 89 | + } |
| 90 | + |
| 91 | + async Task OpenNewWebWorker() |
| 92 | + { |
| 93 | + var instance = await WebWorkerService.GetWebWorker(); |
| 94 | + var nmt = true; |
| 95 | + } |
| 96 | + |
| 97 | + async Task OpenNewSharedWorker() |
| 98 | + { |
| 99 | + var name = $"SHARED_{DateTime.Now.ToString("s")}"; |
| 100 | + var instance = await WebWorkerService.GetSharedWebWorker(name); |
| 101 | + var nmt = true; |
| 102 | + } |
| 103 | + |
| 104 | + protected override void OnInitialized() |
| 105 | + { |
| 106 | + WebWorkerService.OnInstanceFound += WebWorkerService_OnInstanceFound; |
| 107 | + WebWorkerService.OnInstanceLost += WebWorkerService_OnInstanceLost; |
| 108 | + WebWorkerService.OnInstancesChanged += WebWorkerService_OnInstancesChanged; |
| 109 | + } |
| 110 | + |
| 111 | + void WebWorkerService_OnInstancesChanged() |
| 112 | + { |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + void WebWorkerService_OnInstanceFound(AppInstance instance) |
| 117 | + { |
| 118 | + StateHasChanged(); |
| 119 | + } |
| 120 | + |
| 121 | + void WebWorkerService_OnInstanceLost(AppInstance instance) |
| 122 | + { |
| 123 | + StateHasChanged(); |
| 124 | + } |
| 125 | + |
35 | 126 | // this static method can be called by other running instances |
36 | 127 | static void SetInstanceCount(int count) |
37 | 128 | { |
|
71 | 162 | { |
72 | 163 | // unset this instance on the static var |
73 | 164 | instance = null; |
| 165 | + WebWorkerService.OnInstanceFound -= WebWorkerService_OnInstanceFound; |
| 166 | + WebWorkerService.OnInstanceLost -= WebWorkerService_OnInstanceLost; |
| 167 | + WebWorkerService.OnInstancesChanged -= WebWorkerService_OnInstancesChanged; |
74 | 168 | } |
75 | 169 | private async Task IncrementCount() |
76 | 170 | { |
|
0 commit comments