OLD | NEW |
(Empty) | |
| 1 # Initialize [](https://travis-ci.org/dart-lang/initialize) |
| 2 |
| 3 This package provides a common interface for initialization annotations on top |
| 4 level methods, classes, and libraries. The interface looks like this: |
| 5 |
| 6 ```dart |
| 7 abstract class Initializer<T> { |
| 8 dynamic initialize(T target); |
| 9 } |
| 10 ``` |
| 11 |
| 12 The `initialize` method will be called once for each annotation. The type `T` is |
| 13 determined by what was annotated. For libraries it will be a `LibraryIdentifier` |
| 14 representing that library, for a class it will be the `Type` representing that |
| 15 class, and for a top level method it will be the `Function` object representing |
| 16 that method. |
| 17 |
| 18 If a future is returned from the initialize method, it will wait until the futur
e |
| 19 completes before running the next initializer. |
| 20 |
| 21 ## Usage |
| 22 |
| 23 ### @initMethod |
| 24 |
| 25 There is one initializer which comes with this package, `@initMethod`. Annotate |
| 26 any top level function with this and it will be invoked automatically. For |
| 27 example, the program below will print `hello`: |
| 28 |
| 29 ```dart |
| 30 import 'package:initialize/initialize.dart'; |
| 31 |
| 32 @initMethod |
| 33 printHello() => print('hello'); |
| 34 |
| 35 main() => run(); |
| 36 ``` |
| 37 |
| 38 ### Running the initializers |
| 39 |
| 40 In order to run all the initializers, you need to import |
| 41 `package:initialize/initialize.dart` and invoke the `run` method. This should |
| 42 typically be the first thing to happen in your main. That method returns a Futur
e, |
| 43 so you should put the remainder of your program inside the chained then call. |
| 44 |
| 45 ```dart |
| 46 import 'package:initialize/initialize.dart'; |
| 47 |
| 48 main() { |
| 49 run().then((_) { |
| 50 print('hello world!'); |
| 51 }); |
| 52 } |
| 53 ``` |
| 54 |
| 55 ## Transformer |
| 56 |
| 57 During development a mirror based system is used to find and run the initializer
s, |
| 58 but for deployment there is a transformer which can replace that with a static l
ist |
| 59 of initializers to be ran. |
| 60 |
| 61 This will create a new entry point which bootstraps your existing app, this will |
| 62 have the same file name except `.dart` with be replaced with `.initialize.dart`. |
| 63 If you supply an html file to `entry_points` then it will bootstrap the dart |
| 64 script tag on that page and replace the `src` attribute to with the new |
| 65 bootstrap file. |
| 66 |
| 67 Below is an example pubspec with the transformer: |
| 68 |
| 69 name: my_app |
| 70 dependencies: |
| 71 initialize: any |
| 72 transformers: |
| 73 - initialize: |
| 74 entry_points: web/index.html |
| 75 |
| 76 ## Creating your own initializer |
| 77 |
| 78 Lets look at a slightly simplified version of the `@initMethod` class: |
| 79 |
| 80 ```dart |
| 81 class InitMethod implements Initializer<Function> { |
| 82 const InitMethod(); |
| 83 |
| 84 @override |
| 85 initialize(Function method) => method(); |
| 86 } |
| 87 ``` |
| 88 |
| 89 You would now be able to add `@InitMethod()` in front of any function and it |
| 90 will be automatically invoked when the user calls `run()`. |
| 91 |
| 92 For classes which are stateless, you can usually just have a single const |
| 93 instance, and that is how the actual InitMethod implementation works. Simply add |
| 94 something like the following: |
| 95 |
| 96 ```dart |
| 97 const initMethod = const InitMethod(); |
| 98 ``` |
| 99 |
| 100 Now when people use the annotation, it just looks like `@initMethod` without any |
| 101 parenthesis, and its a bit more efficient since there is a single instance. You |
| 102 can also make your class private to force users into using the static instance. |
| 103 |
| 104 ## Creating custom transformer plugins |
| 105 |
| 106 It is possible to create a custom plugin for the initialize transformer which |
| 107 allows you to have full control over what happens to your annotations at compile |
| 108 time. Implement `InitializerPlugin` class and pass that in to the |
| 109 `InitializeTransformer` to make it take effect. |
| 110 |
| 111 You will need to be familiar with the `analyzer` package in order to write these |
| 112 plugins, but they can be extremely powerful. See the `DefaultInitializerPlugin` |
| 113 in `lib/build/initializer_plugin.dart` as a reference. Chances are you may want |
| 114 to extend that class in order to get a lot of the default functionality. |
OLD | NEW |