Last active
August 29, 2015 14:24
-
-
Save sigmundch/11515fd23c18a9983e35 to your computer and use it in GitHub Desktop.
defer imports + angular2 transformers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library a; | |
import 'loader.dart'; | |
// Normal code dependencies we discover automatically. | |
import 'b.dart'; | |
import 'c.dart' deferred as c; | |
foo() { | |
// Instead of c.loadLibrary() we do: | |
load(#a.c, c.loadLibrary).then((_) { | |
// here c is loaded and initialized! | |
}); | |
} | |
@Injectable() class A {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library a.ng_deps; | |
import 'loader.dart'; | |
import 'a.dart'; | |
import 'b.ng_deps.dart' as b; | |
import 'c.ng_deps.dart' deferred as c; | |
initReflector() { | |
// do a's initialization | |
registerReflectiveType(A, ...); | |
// initialize sync dependencies | |
b.initReflector(); | |
// register initialization for deferred dependencies (to be run when `a` loads them). | |
register(#a.c, () => c.loadLibrary().then((_) => c.initReflector())); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'c.dart'; // normal import is ok, all imports to c.ng_deps.dart are deferred | |
initReflector() { | |
registerReflectiveType(C, ...); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
var _registry = {}; | |
// adds something to run when loading `key` | |
register(key, Future f()) => _registry.putIfAbsent(key, () => []).add(f); | |
// run `f` to load what's registered under `key`, and then run everything | |
// that was registered under it: | |
load(key, loadLibrary) => loadLibrary() | |
.then((_) => Future.forEach(_registry[key], (f) => f())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment