Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of Worker class break unit tests #36

Closed
Sylphide opened this issue Mar 30, 2021 · 5 comments
Closed

Usage of Worker class break unit tests #36

Sylphide opened this issue Mar 30, 2021 · 5 comments

Comments

@Sylphide
Copy link

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
Copy link

syleishere commented Mar 30, 2021

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
Copy link

syleishere commented Mar 30, 2021

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
Copy link
Author

Sylphide commented Mar 31, 2021

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 added a commit that referenced this issue Apr 2, 2021
@terrier989
Copy link
Member

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

@syleishere
Copy link

Works perfectly, thank-you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants