{"id":13551183,"url":"https://github.com/deakjahn/flutter_isolate_web","last_synced_at":"2025-04-03T01:31:34.402Z","repository":{"id":53731117,"uuid":"269610429","full_name":"deakjahn/flutter_isolate_web","owner":"deakjahn","description":"Unified interface to isolates and web workers.","archived":false,"fork":false,"pushed_at":"2021-03-17T13:33:13.000Z","size":85,"stargazers_count":68,"open_issues_count":1,"forks_count":15,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-25T21:04:00.452Z","etag":null,"topics":["flutter","isolates","webworker"],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/deakjahn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-05T11:10:14.000Z","updated_at":"2025-01-17T06:27:11.000Z","dependencies_parsed_at":"2022-08-30T00:00:40.109Z","dependency_job_id":null,"html_url":"https://github.com/deakjahn/flutter_isolate_web","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deakjahn%2Fflutter_isolate_web","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deakjahn%2Fflutter_isolate_web/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deakjahn%2Fflutter_isolate_web/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deakjahn%2Fflutter_isolate_web/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deakjahn","download_url":"https://codeload.github.com/deakjahn/flutter_isolate_web/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246922091,"owners_count":20855341,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flutter","isolates","webworker"],"created_at":"2024-08-01T12:01:43.779Z","updated_at":"2025-04-03T01:31:34.073Z","avatar_url":"https://github.com/deakjahn.png","language":"Dart","readme":"# Flutter Isolate Web\n\nThe title is a misnomer. Of course, there are no isolates in Flutter Web. What this code provides\nis a unified interface to isolates *and* web workers so that each platform can use its own.\nIt's not a package on pub.dev and it won't be because you can't use it out of the box just like\na regular package or plugin. You have to copy it into your own code and modify it to suit your needs.\n\n## Dependencies\n\nIt depends on:\n\n* [isolate_handler](https://pub.dev/packages/isolate_handler), this is what provides\nthe isolates with communication already in place,\n\n* [js](https://pub.dev/packages/js), this is what provides the connection to JavaScript.\n\n## Usage\n\nCreate a worker first:\n\n```dart\nfinal worker = BackgroundWorker();\n```\n\nand start it when needed:\n\n```dart\nworker.spawn(\n  doWork,\n  name: 'some-unique-name',\n  onInitialized: onInitialized,\n  onFromWorker: onReceive,\n);\n```\n\nYou can start any amount of workers, just give a unique name to all so that you can reference them later when sending\nor receiving messages.\n\n`doWork()` is a function taking a `Map` argument (the context of the isolate/worker). As customary with standard isolates,\nit has to be a top-level or static function. The most usual activity here is to start listening to messages the isolate/worker\nwill receive from the main app (the actual message structure is completely up to you, this is just an example):\n\n```dart\nvoid doWork(Map\u003cString, dynamic\u003e context) {\n  worker.listen((args) {\n    switch (args['command']) {\n      case 'start':\n        // worker starts its job\n        break;\n    }\n  }, context: context);\n}\n```\n\nWhen the isolate/worker actually gets initialized, the main app will be notified. You might simply use this to send a message\nback to the worker to start the actual work (the actual message structure is completely up to you, this is just an example):\n\n```dart\nvoid onInitialized() {\n  worker.sendTo('some-unique-name', {\n    // tell the worker to start its job\n    'command': 'start',\n    'data': ...,\n  });\n}\n```\n\nThere is an important difference between the two that must be understood. `doWork()` runs in the worker/isolate,\nthis is the main entry point of the worker/isolate code. `onInitialized` and `onFromWorker` run in the main app,\nthis is where the main app receives messages from the workers/isolates.\n\nThis second is the main messaging mechanism. Make sure the isolate/worker also knows the main `worker` object\nand its own unique name (the name was returned to the `entryPoint()` as `context['name']`) because it needs those\nto send its messages back:\n\n```dart\n// isolate/worker sends to main app:\nworker.sendFrom('unique-name', message);\n\n// main app sends to isolate/worker:\nworker.sendTo('unique-name', message);\n\n// main app receives from isolate/worker:\nvoid onReceive(T message) {\n  //...\n}\n```\n\nIf you need to kill the worker/isolate, use:\n\n```dart\nworker.kill('unique-name');\n```\n\nTo kill all of them, use the `names` list:\n\n```dart\nfor (String name in worker.names) worker.kill(name);\n```\n\n## Web\n\nEverything described above works on mobile with isolates. However, the Flutter Web side is not that nice yet.\n\nIf you have the JavaScript code you want to run when compiling your app for the web, it works. Call your code with\nthe `importScripts()` in `worker_web.dart` and that's all. You can even do away with all that `createObjectUrlFromBlob`\nstuff and construct the `Worker` directly with the JS file from your app assets or downloaded from a CDN. The same applies\nif your isolate code from your mobile app is simple enough so that you can and want to replicate it in JavaScript.\n\nThe missing link in the chain is if you want to use the *same* Dart code you used for your isolate in your web worker.\nIf your code is free from dependencies, you could get away with simply compiling it with:\n\n    dart2js --libraries-spec=$HOME/flutter/bin/cache/flutter_web_sdk/libraries.json -o worker.js worker.dart\n\ncopying it to your assets and loading it. But we are unable to simply re-use our existing Dart code, even if it is actually\ncompiled to JavaScript, anyway. There's no way to compile part of your app (your isolate/worker) into a separate file and\nto include it separately for the worker to use, and, by obvious limitation of web workers, we can't simply call into our\nmain app code from the worker, either.\n\n### Intermediate solution for the web\n\nThe package also has a `worker_async.dart` file. This is an async solution to the problem. It implements the same interface,\nso it can be used as is to make the isolate/worker scheme work on the web. It won't be a real web worker, though, it won't run\nin parallel but it works. These \"fake\" workers will be called in the usual asnychronous way. While not yet the real McCoy,\nit's a functional replacement.\n","funding_links":[],"categories":["Dart"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeakjahn%2Fflutter_isolate_web","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeakjahn%2Fflutter_isolate_web","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeakjahn%2Fflutter_isolate_web/lists"}