|
| 1 | +> [!CAUTION] |
| 2 | +> This is an experimental package, and it's API can break at any time. Use at |
| 3 | +> your own discretion. |
| 4 | +
|
| 5 | +This package provides the data classes for the usage recording feature in the |
| 6 | +Dart SDK. |
| 7 | + |
| 8 | +Dart objects with the `@RecordUse` annotation are being recorded at compile |
| 9 | +time, providing the user with information. The information depends on the object |
| 10 | +being recorded. |
| 11 | + |
| 12 | +- If placed on a static method, the annotation means that arguments passed to |
| 13 | +the method will be recorded, as far as they can be inferred at compile time. |
| 14 | +- If placed on a class with a constant constructor, the annotation means that |
| 15 | +any constant instance of the class will be recorded. This is particularly useful |
| 16 | +when using the class as an annotation. |
| 17 | + |
| 18 | +## Example |
| 19 | + |
| 20 | +```dart |
| 21 | +import 'package:meta/meta.dart' show RecordUse; |
| 22 | +
|
| 23 | +void main() { |
| 24 | + print(SomeClass.stringMetadata(42)); |
| 25 | + print(SomeClass.doubleMetadata(42)); |
| 26 | + print(SomeClass.intMetadata(42)); |
| 27 | + print(SomeClass.boolMetadata(42)); |
| 28 | +} |
| 29 | +
|
| 30 | +class SomeClass { |
| 31 | + @RecordMetadata('leroyjenkins') |
| 32 | + @RecordUse() |
| 33 | + static stringMetadata(int i) { |
| 34 | + return i + 1; |
| 35 | + } |
| 36 | +
|
| 37 | + @RecordMetadata(3.14) |
| 38 | + @RecordUse() |
| 39 | + static doubleMetadata(int i) { |
| 40 | + return i + 1; |
| 41 | + } |
| 42 | +
|
| 43 | + @RecordMetadata(42) |
| 44 | + @RecordUse() |
| 45 | + static intMetadata(int i) { |
| 46 | + return i + 1; |
| 47 | + } |
| 48 | +
|
| 49 | + @RecordMetadata(true) |
| 50 | + @RecordUse() |
| 51 | + static boolMetadata(int i) { |
| 52 | + return i + 1; |
| 53 | + } |
| 54 | +} |
| 55 | +
|
| 56 | +@RecordUse() |
| 57 | +class RecordMetadata { |
| 58 | + final Object metadata; |
| 59 | +
|
| 60 | + const RecordMetadata(this.metadata); |
| 61 | +} |
| 62 | +
|
| 63 | +``` |
| 64 | +This code will generate a data file that contains both the `metadata` values of |
| 65 | +the `RecordMetadata` instances, as well as the arguments for the different |
| 66 | +methods annotated with `@RecordUse()`. |
| 67 | + |
| 68 | +This information can then be accessed in a link hook as follows: |
| 69 | +```dart |
| 70 | +import 'dart:convert'; |
| 71 | +
|
| 72 | +import 'package:hooks/hooks.dart'; |
| 73 | +import 'package:record_use/record_use_internal.dart'; |
| 74 | +
|
| 75 | +final methodId = Identifier( |
| 76 | + uri: 'myfile.dart', |
| 77 | + name: 'myMethod', |
| 78 | +); |
| 79 | +
|
| 80 | +final classId = Identifier( |
| 81 | + uri: 'myfile.dart', |
| 82 | + name: 'myClass', |
| 83 | +); |
| 84 | +
|
| 85 | +void main(List<String> arguments){ |
| 86 | + link(arguments, (config, output) async { |
| 87 | + final usesUri = config.recordedUses; |
| 88 | + final usesJson = await File,fromUri(usesUri).readAsString(); |
| 89 | + final uses = UsageRecord.fromJson(jsonDecode(usesJson)); |
| 90 | +
|
| 91 | + final args = uses.argumentsTo(methodId)); |
| 92 | + //[args] is an iterable of arguments, in this case containing "42" |
| 93 | +
|
| 94 | + final fields = uses.instancesOf(classId); |
| 95 | + //[fields] is an iterable of the fields of the class, in this case |
| 96 | + //containing |
| 97 | + // {"arguments": "leroyjenkins"} |
| 98 | + // {"arguments": 3.14} |
| 99 | + // {"arguments": 42} |
| 100 | + // {"arguments": true} |
| 101 | +
|
| 102 | + ... // Do something with the information, such as tree-shaking native assets |
| 103 | + }); |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Limitations |
| 108 | +As this is designed to work on both web and native platforms, we have to adapt |
| 109 | +to the platform pecularities. One of them is that javascript does not support |
| 110 | +named arguments, so the dart2js compiler rewrites functions to only accept named |
| 111 | +parameters. |
| 112 | +While you can use named parameters to record functions, we advise caution as the |
| 113 | +retrieval behavior might change once we work around this dart2js limitation and |
| 114 | +implement separate positional and named parameters. |
| 115 | + |
| 116 | +## Contributing |
| 117 | +Contributions are welcome! Please open an issue or submit a pull request. |
0 commit comments