zone_local 0.1.0

  • README.md
  • Example
  • Installing
  • Versions
  • new46

Introduction #

  • Zone-local values are similar to "ThreadLocal" values in multi-threading languages like Java. In terms of performance, creating a new Zone is much cheaper than creating a thread. See explanation of zones at dartlang.org.
  • This packages optimizes ease and performance of:
    • Getting a zone-scoped value.
    • Setting the default values. You can optionally prevent accidental mutations of default values.
    • Forking the current zone.
  • A minimalistic alternative to dependency injection.
    • Introduces a single, easy-to-understand class.
    • No compile-time code generation or other magic.

Example #

pubspec.yaml #

dependencies:
  zone_local: ^0.1.0

main.dart #

import 'package:zone_local/zone_local.dart';

void main() {
  // Configure a zone-local service.
  // The method also prevents later (accidental) modification of the default value.
  GreetingService.zoneLocal.freezeDefaultValue(GreetingService("Hello, world!"));

  // Use the service
  print(GreetingService.zoneLocal.value.greeting);
  // --> Hello, world!

  // Fork a zone with different value
  final forkedZone = GreetingService.zoneLocal.forkZoneWithValue(new GreetingService("Hi, forked zone!"));
  forkedZone.run(() {
    print(GreetingService.zoneLocal.value.greeting);
    // --> Hi forked, zone!
  });
}

class GreetingService {
  static final ZoneLocal<GreetingService> zoneLocal = ZoneLocal<GreetingService>();

  final String greeting;
  GreetingService(this.greeting);
}

example/zone_local.dart

import 'package:zone_local/zone_local.dart';

void main() {
  // Configure a zone-local service.
  // The method also prevents later (accidental) modification of the default value.
  GreetingService.zoneLocal
      .freezeDefaultValue(GreetingService("Hello, world!"));

  // Use the service
  print(GreetingService.zoneLocal.value.greeting);
  // --> Hello, world!

  // Fork a zone with different value
  final forkedZone = GreetingService.zoneLocal
      .forkZoneWithValue(GreetingService("Hi, forked zone!"));
  forkedZone.run(() {
    print(GreetingService.zoneLocal.value.greeting);
    // --> Hi forked, zone!
  });
}

class GreetingService {
  static final ZoneLocal<GreetingService> zoneLocal =
      ZoneLocal<GreetingService>();

  final String greeting;
  GreetingService(this.greeting);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  zone_local: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:zone_local/zone_local.dart';
  
Version Uploaded Documentation Archive
0.1.0 Mar 15, 2019 Go to the documentation of zone_local 0.1.0 Download zone_local 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
46
Learn more about scoring.

We analyzed this package on Mar 15, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:zone_local/zone_local.dart.

Maintenance issues and suggestions

Provide a file named CHANGELOG.md. (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
meta ^1.1.0 1.1.7
Dev dependencies
pedantic ^1.0.0
test ^1.4.0