Window is not defined when webworkerservice used by another thread

Đang mở
#3 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
49/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript, wasm
Lĩnh vực
backend, web-dev

Hướng nghiên cứu

Start with the browser service registration and initialization flow shown in the issue, especially the calls to AddWebWorkerService and StartBackgroundServices, then trace how WebWorkerService accesses the global scope when requested from another thread. Reproduce the failure through the Avalonia UI thread and check whether OPFS calls work there. Done means the service can be accessed from that thread without “Window is not defined” and OPFS calls function.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Attempting to get the web worker service throws with "Window is not defined"
I can fix two of the errors by replacing webWorkerService.GlobalScope == GlobalScope.Window ? 2 : 0 with 0
however, this does not fix functionality, and any calls to OPFS will still throw.

This code gets run by the Avalonia UI thread calling OnFrameworkInitializationCompleted (because my initialization requires async for many things)
Due to this, getting the WebWorkerService and attempting to use it will throw that error in the JS console it seems.

If I move the worker init code outside of the pipeline, I can use it however I like, but the second it is accessed via another thread (such as the Avalonia UI thread), it will throw.

(flow:)

  • Main
  • AppBuilder
  • UniScanApp.Initialize() (called by Avalonia)
  • OnFrameworkInitializationCompleted (called by Avalonia)
  • _ = InitializationPipeline.RunAsync(this); (runs a ton of tasks to initialize everything in a specific order)
  • The following code:

(ALSO VIEWABLE HERE: https://github.com/UniScanProject/UniScan/tree/main/src/Client/UniScan.Client.App.Platform.Browser/Program.cs)

                IServiceCollection browserServices = new ServiceCollection();
                
                browserServices.AddSpawnJSRuntime(out SpawnJSRuntime runtime);
                browserServices.AddWebWorkerService(webWorkerService =>
                {
                    webWorkerService.TaskPool.MaxPoolSize = -1;
                    webWorkerService.TaskPool.PoolSize = webWorkerService.GlobalScope == GlobalScope.Window ? 2 : 0;
                });

                browserServices.AddSingleton<BrowserDirectoryManager>();
                browserServices.AddSingleton<BrowserFileManager>();
                
                browserServices.AddSingleton<IOPFSWorkerService, OPFSWorkerService>();
                IServiceProvider browserServiceProvider = browserServices.BuildServiceProvider();
                await browserServiceProvider.StartBackgroundServices();
                
                ctx.StandardPaths = new BrowserPaths();
                ctx.SerilogInitializer = new BrowserPlatformSerilogInitializer(UniScan.Core.Constants.ConsoleOutputTemplate);
                ctx.FileManager = browserServiceProvider.GetRequiredService<BrowserFileManager>();
                ctx.DirectoryManager = browserServiceProvider.GetRequiredService<BrowserDirectoryManager>();
                
                ctx.ServiceCollection.AddSingleton(runtime);
                ctx.ServiceCollection.AddSingleton<WebWorkerService>(_ => browserServiceProvider.GetRequiredService<WebWorkerService>());
                
                ctx.ServiceCollection.AddSingleton<IPlatformStandardPaths>(_ => ctx.StandardPaths);
                ctx.ServiceCollection.AddSingleton<IPlatformSerilogInitializer>(_ => ctx.SerilogInitializer);
                ctx.ServiceCollection.AddSingleton<IPlatformDirectoryManager>(_ => ctx.DirectoryManager);
                ctx.ServiceCollection.AddSingleton<IPlatformFileManager>(_ => ctx.FileManager);
                ctx.ServiceCollection.AddSingleton<IOPFSWorkerService>(_ => browserServiceProvider.GetRequiredService<IOPFSWorkerService>());
Ngôn ngữ chính
JavaScript
Star
3
Fork
0
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của LostBeard/SpawnDev.SpawnJS.WebWorkers

Tất cả issue của LostBeard/SpawnDev.SpawnJS.WebWorkers

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.