Skip to content

Refactor and improve HMR code #1770

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

Merged
merged 3 commits into from
Aug 21, 2018
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
54 changes: 28 additions & 26 deletions build_runner/lib/src/server/hot_reload_client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,11 @@ class DartLoader {
external JsMap<String, List<String>> get moduleParentsGraph;

@JS()
external void forceLoadModule(String moduleId,
void Function(Object module) callback, void Function(JsError e) onError);
external void forceLoadModule(String moduleId, void Function() callback,
void Function(JsError e) onError);

@JS()
external void loadModule(String moduleId,
void Function(Object module) callback, void Function(JsError e) onError);
external Object getModuleLibraries(String moduleId);
}

@JS(r'$dartLoader')
Expand All @@ -149,27 +148,30 @@ List<K> keys<K, V>(JsMap<K, V> map) {
return List.from(_jsArrayFrom(map.keys()));
}

Future<Module> Function(String) _futurifyLoaderFunction(
void Function(String, void Function(Object), void Function(JsError))
loaderFunction) =>
(moduleId) {
var completer = Completer<Module>();
var stackTrace = StackTrace.current;
loaderFunction(moduleId, allowInterop((Object moduleObj) {
var moduleKeys = List<String>.from(_jsObjectKeys(moduleObj));
var moduleValues =
List<HotReloadableLibrary>.from(_jsObjectValues(moduleObj));
var moduleLibraries = moduleValues.map((x) => LibraryWrapper(x));
var module = Module(Map.fromIterables(moduleKeys, moduleLibraries));
completer.complete(module);
}),
allowInterop((e) => completer.completeError(
HotReloadFailedException(e.message), stackTrace)));
return completer.future;
};

var _reloadModule = _futurifyLoaderFunction(dartLoader.forceLoadModule);
var _loadModule = _futurifyLoaderFunction(dartLoader.loadModule);
Module _moduleLibraries(String moduleId) {
var moduleObj = dartLoader.getModuleLibraries(moduleId);
if (moduleObj == null) {
throw HotReloadFailedException("Failed to get module '$moduleId'. "
"This error might appear if such module doesn't exist or isn't alredy loaded");
}
var moduleKeys = List<String>.from(_jsObjectKeys(moduleObj));
var moduleValues =
List<HotReloadableLibrary>.from(_jsObjectValues(moduleObj));
var moduleLibraries = moduleValues.map((x) => LibraryWrapper(x));
var module = Module(Map.fromIterables(moduleKeys, moduleLibraries));
return module;
}

Future<Module> _reloadModule(String moduleId) {
var completer = Completer<Module>();
var stackTrace = StackTrace.current;
dartLoader.forceLoadModule(moduleId, allowInterop(() {
completer.complete(_moduleLibraries(moduleId));
}),
allowInterop((e) => completer.completeError(
HotReloadFailedException(e.message), stackTrace)));
return completer.future;
}

void _reloadPage() {
window.location.reload();
Expand All @@ -188,7 +190,7 @@ main() async {

var manager = ReloadingManager(
_reloadModule,
_loadModule,
_moduleLibraries,
_reloadPage,
(module) => dartLoader.moduleParentsGraph.get(module),
() => keys(dartLoader.moduleParentsGraph));
Expand Down
Loading