|
| 1 | +using SpawnDev.BlazorJS; |
| 2 | +using SpawnDev.BlazorJS.JSObjects; |
| 3 | +using SpawnDev.BlazorJS.WebWorkers; |
| 4 | +using System.Collections.Specialized; |
| 5 | +using System.Web; |
| 6 | + |
| 7 | +namespace SpawnDev.BlazorJS.WebWorkers.Demo.Services |
| 8 | +{ |
| 9 | + public class AppServiceWorker : ServiceWorkerEventHandler |
| 10 | + { |
| 11 | + public AppServiceWorker(BlazorJSRuntime js) : base(js) |
| 12 | + { |
| 13 | + |
| 14 | + } |
| 15 | + // called before any ServiceWorker events are handled |
| 16 | + protected override async Task OnInitializedAsync() |
| 17 | + { |
| 18 | + // This service may start in any scope. This will be called before the app runs. |
| 19 | + // If JS.IsWindow == true be careful not stall here. |
| 20 | + // you can do initialization based on the scope that is running |
| 21 | + Log("GlobalThisTypeName", JS.GlobalThisTypeName); |
| 22 | + } |
| 23 | + |
| 24 | + protected override async Task ServiceWorker_OnInstallAsync(ExtendableEvent e) |
| 25 | + { |
| 26 | + Log($"ServiceWorker_OnInstallAsync"); |
| 27 | + _ = ServiceWorkerThis!.SkipWaiting(); // returned task can be ignored |
| 28 | + } |
| 29 | + |
| 30 | + protected override async Task ServiceWorker_OnActivateAsync(ExtendableEvent e) |
| 31 | + { |
| 32 | + Log($"ServiceWorker_OnActivateAsync"); |
| 33 | + await ServiceWorkerThis!.Clients.Claim(); |
| 34 | + } |
| 35 | + |
| 36 | + protected override async Task<Response> ServiceWorker_OnFetchAsync(FetchEvent e) |
| 37 | + { |
| 38 | + Log($"ServiceWorker_OnFetchAsync", e.Request.Method, e.Request.Url); |
| 39 | + Response ret; |
| 40 | + try |
| 41 | + { |
| 42 | + ret = await JS.Fetch(e.Request); |
| 43 | + } |
| 44 | + catch (Exception ex) |
| 45 | + { |
| 46 | + ret = new Response(ex.Message, new ResponseOptions { Status = 500, StatusText = ex.Message, Headers = new Dictionary<string, string> { { "Content-Type", "text/plain" } } }); |
| 47 | + Log($"ServiceWorker_OnFetchAsync failed: {ex.Message}"); |
| 48 | + } |
| 49 | + return ret; |
| 50 | + } |
| 51 | + |
| 52 | + protected override async Task ServiceWorker_OnMessageAsync(ExtendableMessageEvent e) |
| 53 | + { |
| 54 | + Log($"ServiceWorker_OnMessageAsync"); |
| 55 | + } |
| 56 | + |
| 57 | + protected override async Task ServiceWorker_OnPushAsync(PushEvent e) |
| 58 | + { |
| 59 | + Log($"ServiceWorker_OnPushAsync"); |
| 60 | + } |
| 61 | + |
| 62 | + protected override async Task ServiceWorker_OnPushSubscriptionChangeAsync(PushSubscriptionChangeEvent e) |
| 63 | + { |
| 64 | + Log($"ServiceWorker_OnPushSubscriptionChangeAsync"); |
| 65 | + } |
| 66 | + |
| 67 | + protected override async Task ServiceWorker_OnSyncAsync(SyncEvent e) |
| 68 | + { |
| 69 | + Log($"ServiceWorker_OnSyncAsync"); |
| 70 | + } |
| 71 | + |
| 72 | + protected override async Task ServiceWorker_OnNotificationCloseAsync(NotificationEvent e) |
| 73 | + { |
| 74 | + Log($"ServiceWorker_OnNotificationCloseAsync"); |
| 75 | + } |
| 76 | + |
| 77 | + protected override async Task ServiceWorker_OnNotificationClickAsync(NotificationEvent e) |
| 78 | + { |
| 79 | + Log($"ServiceWorker_OnNotificationClickAsync"); |
| 80 | + } |
| 81 | + void Log(params object[] msg) => JS.Log(msg); |
| 82 | + } |
| 83 | +} |
0 commit comments