p-hatan/ mirror

pub repository

mirror.p-hatan.ir

record_use

0.4.1

The serialization logic and API for the usage recording SDK feature.

Download archive

Readme

[!CAUTION] This is an experimental package, and it's API can break at any time. Use at your own discretion.

This package provides the data classes for the usage recording feature in the Dart SDK.

Dart objects with the @RecordUse annotation are being recorded at compile time, providing the user with information. The information depends on the object being recorded.

  • If placed on a static method, the annotation means that arguments passed to the method will be recorded, as far as they can be inferred at compile time.
  • If placed on a class with a constant constructor, the annotation means that any constant instance of the class will be recorded. This is particularly useful when using the class as an annotation.

Example

import 'package:meta/meta.dart' show RecordUse;

void main() {
  print(SomeClass.stringMetadata(42));
  print(SomeClass.doubleMetadata(42));
  print(SomeClass.intMetadata(42));
  print(SomeClass.boolMetadata(42));
}

class SomeClass {
  @RecordMetadata('leroyjenkins')
  @RecordUse()
  static stringMetadata(int i) {
    return i + 1;
  }

  @RecordMetadata(3.14)
  @RecordUse()
  static doubleMetadata(int i) {
    return i + 1;
  }

  @RecordMetadata(42)
  @RecordUse()
  static intMetadata(int i) {
    return i + 1;
  }

  @RecordMetadata(true)
  @RecordUse()
  static boolMetadata(int i) {
    return i + 1;
  }
}

@RecordUse()
class RecordMetadata {
  final Object metadata;

  const RecordMetadata(this.metadata);
}

This code will generate a data file that contains both the metadata values of the RecordMetadata instances, as well as the arguments for the different methods annotated with @RecordUse().

This information can then be accessed in a link hook as follows:

import 'dart:convert';

import 'package:hooks/hooks.dart';
import 'package:record_use/record_use_internal.dart';

final methodId = Identifier(
  uri: 'myfile.dart',
  name: 'myMethod',
);

final classId = Identifier(
  uri: 'myfile.dart',
  name: 'myClass',
);

void main(List<String> arguments){
  link(arguments, (config, output) async {
    final usesUri = config.recordedUses;
    final usesJson = await File,fromUri(usesUri).readAsString();
    final uses = UsageRecord.fromJson(jsonDecode(usesJson));

    final args = uses.argumentsTo(methodId));
    //[args] is an iterable of arguments, in this case containing "42"

    final fields = uses.instancesOf(classId);
    //[fields] is an iterable of the fields of the class, in this case
    //containing
    // {"arguments": "leroyjenkins"}
    // {"arguments": 3.14}
    // {"arguments": 42}
    // {"arguments": true}

    ... // Do something with the information, such as tree-shaking native assets
  });
}

Limitations

As this is designed to work on both web and native platforms, we have to adapt to the platform pecularities. One of them is that javascript does not support named arguments, so the dart2js compiler rewrites functions to only accept named parameters. While you can use named parameters to record functions, we advise caution as the retrieval behavior might change once we work around this dart2js limitation and implement separate positional and named parameters.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

Changelog

0.4.1

  • Fix bug in signature parsing.

0.4.0

  • Update SDK constraint to ^3.5.0.
  • Rewrite API to expose less symbols.
  • Remove locations for easier caching.

0.3.0

  • Make InstanceConstant a Constant.
  • Separate import from location uri.

0.2.0

  • Use maps instead of lists in serialization.

0.1.1

  • Fix repository link.

0.1.0

  • Initial version.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add record_use
pubspec.yaml
name: record_use
description: >
  The serialization logic and API for the usage recording SDK feature.
version: 0.4.1
repository: https://github.com/dart-lang/sdk/tree/main/pkg/record_use

environment:
  sdk: ^3.7.0

resolution: workspace

dependencies:
  collection: ^1.18.0
  pub_semver: ^2.1.4

dev_dependencies:
  dart_flutter_team_lints: any
  lints: any
  test: any

Version history

VersionPublishedArchiveSHA-256
0.1.02024-09-06T13:16:07.619797ZDownload3aa9785ab984e967c6ab50bf1b55d18cdea258c2b9a7c0a275bd1cdd0a18ba41
0.2.02024-09-13T08:33:42.350999ZDownloadaa2630e495bf522df65248c2488b4b05790b9356743b41d5b24d39f47f6d6085
0.3.02024-09-13T09:43:31.776788ZDownload7c8e4f5ae82350f12d328e37892ca5874b69ab9393d14210d781ccce2e9a98f6
0.4.02025-05-05T11:25:28.405679ZDownload67febd9224ec96fc73522ffe3037e96a74589f6a11a6c99100b1850f159402b1
0.4.12025-06-24T10:35:55.925846ZDownloade2e0d735e843f81a8ab28572fcff9f30aeb0a5870b6450d4bc6778cbc919c8e8
0.4.22025-09-29T15:03:11.895281ZDownloadd3d59b18ca7aa1ce689bb3b3fd982bbebdfcd9e89f977576486a55e3b0f19a27
0.5.02026-03-06T11:26:11.668488ZDownloadbd4b242e3e16ad772db86581b40b3a5e0d159205211b02acff15974ac6c3b939
0.6.02026-03-25T12:19:08.173499ZDownload2551bd8eecfe95d14ae75f6021ad0248be5c27f138c2ec12fcb52b500b3ba1ed
1.0.02026-07-13T14:23:16.395334ZDownloadaf37186ff9ede46fa32f152526c48f46c5b3ad7d3d5a566140cb5df3dc4ed737
1.1.02026-08-10T09:34:54.558113ZDownload3b4ab682aff40175afca6ff090cc5aa7ce9dafe8dfbae1537a6c678e6678baee
1.1.12026-08-18T07:25:17.066900ZDownload1cb8564af8d43b464294411db9217f5ec04891c6f22ee2c32d73ae05e88a6bd2

Dependencies

collection "^1.18.0"

pub_semver "^2.1.4"