Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
appdevelpo committed Apr 13, 2024
1 parent edf0eaa commit ef07942
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 321 deletions.
85 changes: 7 additions & 78 deletions lib/data/services/extension_jscore_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,7 @@ import 'dart:async';
import 'dart:convert';

import 'package:flutter_js/flutter_js.dart';

// class DataExample {
// final String str;
// final int num;

// DataExample({
// required this.str,
// required this.num,
// });
// }

// class JsBridgeExample {
// JsBridgeExample() {
// example();
// }

// _print(message) {
// final d = DateTime.now();
// final ts = '${d.hour}:${d.minute}:${d.second}:${d.millisecond}';
// print('[$ts] $message');
// }

// example() async {
// const exampleJs = '''
// DartBridge.setHandler('TESTJS', async (args) => {
// await new Promise(resolve => setTimeout(resolve, 2000));
// return `JS code got args.name=\${args.name} and args.obj.num=\${args.obj.num}`;
// });

// const print = (message) => DartBridge.sendMessage('PRINT', message);

// setTimeout(async () => {
// print('Start async call to Dart');
// const asyncCallToDartResult = await DartBridge.sendMessage('TESTDART', { some: 'object' });
// print(`asyncCallToDartResult=\${asyncCallToDartResult}`);
// }, 4000);
// ''';

// final jsRuntime = getJavascriptRuntime();
// final jsBridge = JsBridge(jsRuntime: jsRuntime, toEncodable: _toEncodable);
// jsRuntime.evaluate(exampleJs);

// jsBridge.setHandler('PRINT', (message) async {
// _print('[JS] $message');
// });

// _print('Start async call to JS');
// final asyncCallToJsResult = await jsBridge.sendMessage('TESTJS',
// {'name': 'value', 'obj': DataExample(str: 'some string', num: 54321)});
// _print('asyncCallToJsResult=$asyncCallToJsResult');

// jsBridge.setHandler('TESTDART', (message) async {
// await Future.delayed(const Duration(seconds: 2));
// return 'Dart code got $message';
// });
// }

// Object? _toEncodable(Object? value) {
// if (value is DataExample) {
// return {
// 'num': value.num,
// 'str': value.str,
// };
// }
// return null;
// }
// }

// flutter: [11:47:39:524] Start async call to JS
// flutter: [11:47:41:540] asyncCallToJsResult=JS code got args.name=value and args.obj.num=54321
// flutter: [11:47:43:525] [JS] Start async call to Dart
// flutter: [11:47:45:531] [JS] asyncCallToDartResult=Dart code got {some: object}
import 'package:miru_app/utils/log.dart';

const DART_BRIDGE_MESSAGE_NAME = 'DART_BRIDGE_MESSAGE_NAME';

Expand All @@ -90,12 +19,12 @@ class JsBridge {
}) {
final bridgeScriptEvalResult = jsRuntime.evaluate(JS_BRIDGE_JS);
if (bridgeScriptEvalResult.isError) {
print('Error eval bridge script');
logger.info('Error eval bridge script');
}
final windowEvalResult =
jsRuntime.evaluate('var window = global = globalThis;');
if (windowEvalResult.isError) {
print('Error eval window script');
logger.info('Error eval window script');
}
jsRuntime.onMessage(DART_BRIDGE_MESSAGE_NAME, (message) {
_onMessage(message);
Expand All @@ -106,19 +35,19 @@ class JsBridge {
if (message['isRequest']) {
final handler = _handlers[message['name']];
if (handler == null) {
print('Error: no handlers for message $message');
logger.info('Error: no handlers for message $message');
} else {
final result = await handler(message['args']);
final jsResult = jsRuntime.evaluate(
'onMessageFromDart(false, ${message['callId']}, "${message['name']}",${jsonEncode(result, toEncodable: toEncodable)})');
if (jsResult.isError) {
print('Error sending message to JS: $jsResult');
logger.info('Error sending message to JS: $jsResult');
}
}
} else {
final completer = _pendingRequests.remove(message['callId']);
if (completer == null) {
print('Error: no completer for response for message $message');
logger.info('Error: no completer for response for message $message');
} else {
completer.complete(message['result']);
}
Expand All @@ -135,7 +64,7 @@ class JsBridge {
final jsResult = jsRuntime.evaluate(
'window.onMessageFromDart(true, $_messageCounter, "$name",${jsonEncode(message, toEncodable: toEncodable)})');
if (jsResult.isError) {
print('Error sending message to JS: $jsResult');
logger.info('Error sending message to JS: $jsResult');
}

return completer.future;
Expand Down
Loading

0 comments on commit ef07942

Please sign in to comment.