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

[WASM] migrate to package:web and dart:js_interop #74

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
web:
dependency: transitive
description:
name: web
sha256: d43c1d6b787bf0afad444700ae7f4db8827f701bc61c255ac8d328c6f4d52062
url: "https://pub.dev"
source: hosted
version: "1.0.0"
web_socket_channel:
dependency: transitive
description:
Expand Down Expand Up @@ -524,5 +532,5 @@ packages:
source: hosted
version: "3.1.1"
sdks:
dart: ">=3.3.0 <4.0.0"
dart: ">=3.4.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
12 changes: 1 addition & 11 deletions example/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@
<link rel="manifest" href="manifest.json">
</head>
<body>
<!-- This script installs service_worker.js to provide PWA functionality to
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('flutter-first-frame', function () {
navigator.serviceWorker.register('flutter_service_worker.js');
});
}
</script>
<script src="main.dart.js" type="application/javascript"></script>
<script src="flutter_bootstrap.js" async></script>
</body>
</html>
2 changes: 1 addition & 1 deletion lib/bridge/ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef Call = ffi.Pointer<FFIBytesReturn> Function(
int,
);

class FFIBytesReturn extends ffi.Struct {
final class FFIBytesReturn extends ffi.Struct {
external ffi.Pointer<ffi.Void> message;

@ffi.Int32()
Expand Down
71 changes: 55 additions & 16 deletions lib/web/rsa_web.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import 'dart:async';
import 'dart:collection';
import 'dart:html';
import 'dart:js_interop';

import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:web/web.dart';

class FastRsaPlugin {
var _counter = 0;
Worker worker = new Worker('assets/packages/fast_rsa/web/assets/worker.js');
Worker worker = Worker(
'assets/packages/fast_rsa/web/assets/worker.js'.toJS,
);
Map<String, Completer<Uint8List>> completers = {};

static void registerWith(Registrar registrar) {
final MethodChannel channel = MethodChannel(
'fast_rsa',
const StandardMethodCodec(),
// ignore: deprecated_member_use
registrar.messenger);
'fast_rsa',
const StandardMethodCodec(),
// ignore: deprecated_member_use
registrar.messenger,
);
final FastRsaPlugin instance = FastRsaPlugin();
instance.listen();
channel.setMethodCallHandler(instance.handleMethodCall);
Expand All @@ -26,27 +29,63 @@ class FastRsaPlugin {
}

void listen() async {
worker.onMessage.listen((event) {
LinkedHashMap<dynamic, dynamic> data = event.data;
var completer = completers[data['id']];
void _onMessage(Event event) {
final msgEvent = event as MessageEvent;
final data = msgEvent.data as RsaResponse;
var completer = completers[data.id];
if (completer == null) {
return;
}
if (data['error'] != null && data['error'] != "") {
completer.completeError(data['error']);
if (data.error != null && data.error! != '') {
completer.completeError(data.error!);
} else {
completer.complete(data['response']);
completer.complete(data.response?.toDart);
}
completers.remove(data['id']);
});
completers.remove(data.id);
}

worker.onmessage = _onMessage.toJS;
// worker.addEventListener('message', _onMessage.toJS);
}

Future<Uint8List> bridgeCall(String name, Uint8List? /*!*/ request) async {
_counter++;
var id = _counter.toString();
var completer = new Completer<Uint8List>();
completers[id] = completer;
worker.postMessage({'id': id, 'name': name, 'request': request});
worker.postMessage(RsaRequest(
id: id,
name: name,
request: request?.toJS,
));
return completer.future;
}
}

@JS()
@anonymous
extension type RsaRequest._(JSObject _) implements JSObject {
external String id;
external String name;
external JSUint8Array? request;

external RsaRequest({
String id,
String name,
JSUint8Array? request,
});
}

@JS()
@anonymous
extension type RsaResponse._(JSObject _) implements JSObject {
external String id;
external String? error;
external JSUint8Array? response;

external RsaResponse({
String id,
String? error,
JSUint8Array? response,
});
}
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 3.6.3
homepage: https://github.com/jerson/flutter-rsa

environment:
sdk: '>=2.17.0 <3.0.0'
sdk: '>=3.3.0 <4.0.0'
flutter: ">=1.10.0"

false_secrets:
Expand All @@ -18,6 +18,7 @@ dependencies:
ffi: ^2.0.1
flat_buffers: ^2.0.5
path: ^1.8.2
web: ">=0.5.0 <2.0.0"

dev_dependencies:
flutter_test:
Expand Down
Loading