dint-dev / universal_html

Cross-platform 'dart:html'.
https://pub.dev/packages/universal_html
Apache License 2.0
201 stars 63 forks source link

Usage of Worker class break unit tests #36

Closed Sylphide closed 3 years ago

Sylphide commented 3 years ago

Hi,

I am currently using version 1.2.4 of universal_html.

I tried using the Worker class in order to execute a js script to upload a large file to an external server. It works fine in practice but it broke my unit test. I guess since the tests doesn't execute in the same environment as a web browser, the Worker class isn't defined. My tests can't compile and I get this kind of errors:

lib/screens/at_edit.dart:114:39: Error: The method 'Worker' isn't defined for the class 'ATEditScreen'.
 - 'ATEditScreen' is from 'package:at_front/screens/at_edit.dart' ('lib/screens/at_edit.dart').
Try correcting the name to the name of an existing method, or defining a method named 'Worker'.
                Worker uploadWorker = Worker('upload_worker.js');

Should I upgrade universal_html to 2.X (hence my flutter version which is still in a pre release version)?

Thanks

syleishere commented 3 years ago

Having same problem on universal_html: ^2.0.7:

import "package:universal_html/html.dart" as html; html.Worker myWorker; myWorker = new html.Worker('upload_worker.js'); myWorker.onMessage.listen((e) { print("Received message from web worker ${e.data}"); }); myWorker.onError.listen((e) { print("Received error from web worker ${e.toString()}"); });

Building Windows application... lib/drawer.dart(94,10): error G3163D78C: 'Worker' isn't a type. [C:\flutter\musicplayer\build\windows\flutter\flutter_assemble.vcxproj] lib/drawer.dart(97,25): error GB1B8BC88: Method not found: 'Worker'. [C:\flutter\musicplayer\build\windows\flutter\flutter_assemble.vcxproj]

syleishere commented 3 years ago

I'm going to just post my complete function where I use your library I wrote other day so it hopefully helps you:

open_file3(BuildContext context) async { html.Worker myWorker; //html.File file; var url = 'https://X.com:8000/uploads/'; myWorker = new html.Worker('upload_worker.js'); myWorker.onMessage.listen((e) { print("Received message from web worker ${e.data}"); }); myWorker.onError.listen((e) { print("Received error from web worker ${e.toString()}"); });

html.InputElement uploadInput = html.FileUploadInputElement();
uploadInput.multiple = true;
uploadInput.click(); //this opens file explorer on their computer

//suppose I could use a different web worker for each file but fuckit
uploadInput.onChange.listen((e) {
  uploadInput.files.forEach((file) { //loop the uploadInput.files array for the files user selected
    //file = uploadInput.files.first;
    print("File Selected was ${file}");
    myWorker.postMessage({"file": file, "url": url});
  });
});

}

For calling it on WEB vs mobile/desktop I just use: GetPlatform.isWeb ? open_file3(context) : open_file(context);

with GetX framework, hope this helps.

Sylphide commented 3 years ago

For information, I tried to isolate my Worker usage in a specific file and tried using a deferred loading only on web but it still doesn't work.

Found a workaround:

import 'lib_mobile.dart'
  if(dart.library.js) 'lib_web.dart';
terrier989 commented 3 years ago

Hi! Thanks for the issue report! I fixed this in version 2.0.8.

syleishere commented 3 years ago

Works perfectly, thank-you.