Skip to content

Instantly share code, notes, and snippets.

@sigmundch
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sigmundch/11515fd23c18a9983e35 to your computer and use it in GitHub Desktop.
Save sigmundch/11515fd23c18a9983e35 to your computer and use it in GitHub Desktop.
defer imports + angular2 transformers
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 {}
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()));
}
import 'c.dart'; // normal import is ok, all imports to c.ng_deps.dart are deferred
initReflector() {
registerReflectiveType(C, ...);
}
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