Skip to content

Commit

Permalink
WebF WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
MulverineX committed Oct 16, 2023
1 parent dc79da1 commit 4d3a452
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": [

]
],
}
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,15 @@
"java.configuration.updateBuildConfiguration": "interactive",
"cmake.sourceDirectory": "/home/mulverine/Documents/Workspace/personal/refracture/refracture-music/packages/client-flutter/linux",
"terminal.integrated.persistentSessionReviveProcess": "never",
"dart.flutterSdkPaths": [
"packages/client-flutter/.fvm/flutter_sdk"
],
"search.exclude": {
"**/.fvm": true
},
// Remove from file watching
"files.watcherExclude": {
"**/.fvm": true
},
"dart.flutterSdkPath": "/home/mulverine/Documents/Workspace/personal/refracture/refracture-music/packages/client-flutter/.fvm/flutter_sdk"
}
1 change: 1 addition & 0 deletions packages/client-flutter/.fvm/flutter_sdk
4 changes: 4 additions & 0 deletions packages/client-flutter/.fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"flutterSdkVersion": "3.10.1",
"flavors": {}
}
166 changes: 100 additions & 66 deletions packages/client-flutter/lib/app/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_router/shelf_router.dart' as shelf_router;
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:webf/devtools.dart';
import 'package:webf/webf.dart';


// ignore: must_be_immutable
Expand All @@ -30,12 +32,12 @@ class AppCore extends InheritedWidget {

String? loadFailure;

late JavascriptRuntime runtime;
late WebF runtime;

AppCore({
super.key,
required Widget child,
}) : super(child: child);
}) : super(child: Row(children: [child]));

@override
bool updateShouldNotify(AppCore oldWidget) => runtime != oldWidget.runtime;
Expand All @@ -59,78 +61,110 @@ class AppCore extends InheritedWidget {
}

loadRuntime() async {
runtime = getJavascriptRuntime();

await runtime.enableFetch();

await runtime.enableHandlePromises();

runtime.onMessage('initDatabase', (init) async {
unit[init.name]!.initDB(init);
});

runtime.addXhrHandler(XhrHandlerStage.preRequest, (url, method, {body, headers, response}) async {
print(url);
if (url.host == 'app.music') {
print({
url, method, body, headers, response
});
return XhrHandle(XhrHandleType.respond, XhtmlHttpResponseInfo(statusCode: 200, statusText: 'OK'), "");
}
return XhrHandle(XhrHandleType.skip);
});

runtime.onMessage('print', (message) {
print(message);
});
runtime = WebF(
devToolsService: ChromeDevToolsService(),
bundle: WebFBundle.fromContent("""
<html><head>
${unit.values.map((unit) {
if (unit.bundle == null) {
return '<!--Failed to load ${unit.name}-->';
}
return """
<script>
${/*unit.bundle*/ "setInterval(() => console.warn('hi'), 2000);"}
</script>
""";
}).join('\n')}
</head><body><p>test</p></body></html>
""", contentType: ContentType.html, url: 'http://app.music'),
onLoad: (controller) {
print(runtime.devToolsService!.isolateServer);
},
onLoadError: (error, trace) {
logger.f(error, stackTrace: trace);
},
onJSError: (error) {
logger.f(error);
},
viewportHeight: 0,
viewportWidth: 0,
);

runtime.onMessage('addPlugin', (plugin) async {
final String index = ['plugin', ...plugin.index].join('.');

unit[index] = UnitManager(
core: this,
logger: logger,
name: plugin.name,
index: ['plugin', ...plugin.index],
src: plugin.src,
tag: plugin.tag,
hash: plugin.hash,
);

String? errored;

unit[index]!.loaded.future.catchError((error) {
if (plugin.name == 'Base') {
logger.f('[Music UI] fatal: $error');
loadFailure = error;
} else {
logger.e('[Music UI] error: $error');
}
runApp(runtime);

print(runtime);

// runtime = getJavascriptRuntime();

// await runtime.enableFetch();

// await runtime.enableHandlePromises();

// runtime.onMessage('initDatabase', (init) async {
// unit[init.name]!.initDB(init);
// });

// runtime.addXhrHandler(XhrHandlerStage.preRequest, (url, method, {body, headers, response}) async {
// print(url);
// if (url.host == 'app.music') {
// print({
// url, method, body, headers, response
// });
// return XhrHandle(XhrHandleType.respond, XhtmlHttpResponseInfo(statusCode: 200, statusText: 'OK'), "");
// }
// return XhrHandle(XhrHandleType.skip);
// });

// runtime.onMessage('print', (message) {
// print(message);
// });

// runtime.onMessage('addPlugin', (plugin) async {
// final String index = ['plugin', ...plugin.index].join('.');

// unit[index] = UnitManager(
// core: this,
// logger: logger,
// name: plugin.name,
// index: ['plugin', ...plugin.index],
// src: plugin.src,
// tag: plugin.tag,
// hash: plugin.hash,
// );

// String? errored;

// unit[index]!.loaded.future.catchError((error) {
// if (plugin.name == 'Base') {
// logger.f('[Music UI] fatal: $error');
// loadFailure = error;
// } else {
// logger.e('[Music UI] error: $error');
// }

errored = error;
});
// errored = error;
// });

await unit[index]!.load();
// await unit[index]!.load();

if (unit[index]!.bundle != null) {
await loadRuntime();
// if (unit[index]!.bundle != null) {
// await loadRuntime();

return { "success": true };
}
// return { "success": true };
// }

return { "success": false, "error": errored };
});
// return { "success": false, "error": errored };
// });

await runtime.evaluateAsync("""var window = global = globalThis;""");
// await runtime.evaluateAsync("""var window = global = globalThis;""");

for (final unit in unit.values) {
if (unit.bundle != null) {
await runtime.evaluateAsync("""var MusicVersion = ${await unit.secure.storage['databaseVersion'] ?? '0'}');""");
await runtime.evaluateAsync("""var ${unit.name};""");
print(await runtime.evaluateAsync(unit.bundle!));
runtime.executePendingJob();
}
}
// for (final unit in unit.values) {
// if (unit.bundle != null) {
// await runtime.evaluateAsync("""var MusicVersion = ${await unit.secure.storage['databaseVersion'] ?? '0'}');""");
// await runtime.evaluateAsync("""var ${unit.name};""");
// print(await runtime.evaluateAsync(unit.bundle!));
// }
// }
}

Future<void> load() async {
Expand Down
4 changes: 4 additions & 0 deletions packages/client-flutter/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:refracture_music/app/core.dart';
import 'package:webf/webf.dart';

import 'views/navigation.dart';

void main() {
HttpCacheController.mode = HttpCacheMode.NO_CACHE;


runApp(AppCore(child: const MusicApp())..load());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <flutter_acrylic/flutter_acrylic_plugin.h>
#include <flutter_js/flutter_js_plugin.h>
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
#include <webf/webf_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) bitsdojo_window_linux_registrar =
Expand All @@ -24,4 +25,7 @@ void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
g_autoptr(FlPluginRegistrar) webf_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "WebfPlugin");
webf_plugin_register_with_registrar(webf_registrar);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
flutter_acrylic
flutter_js
flutter_secure_storage_linux
webf
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import flutter_js
import flutter_secure_storage_macos
import macos_window_utils
import path_provider_foundation
import shared_preferences_foundation
import sqflite
import webf

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
BitsdojoWindowPlugin.register(with: registry.registrar(forPlugin: "BitsdojoWindowPlugin"))
FlutterJsPlugin.register(with: registry.registrar(forPlugin: "FlutterJsPlugin"))
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
MacOSWindowUtilsPlugin.register(with: registry.registrar(forPlugin: "MacOSWindowUtilsPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
WebFPlugin.register(with: registry.registrar(forPlugin: "WebFPlugin"))
}
Loading

0 comments on commit 4d3a452

Please sign in to comment.