पैकेज, रिपॉज़िटरी और ऐप श्रेणियाँ खोजें

पैकेज विवरणv0.2.1

scoped_deps

dart:async के Zones पर आधारित स्कोप्ड निर्भरता के प्रबंधन के लिए एक सरल Dart पुस्तकालय।

AndroidiOSLinuxmacOSWebWindows
30-दिन डाउनलोड
1.2 लाख
लाइक
9
Pub अंक
160

पैकेज इंस्टॉल करें

flutter pub add scoped_deps

पैकेज अवलोकन

सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।

Scoped_deps एक हल्का Dart पुस्तकालय है जो dart:async के Zones का उपयोग करके सीमित निर्भरता को प्रबंधित करता है। यह विकासकर्ताओं को ऐप के विशिष्ट हिस्सों में अलग-अलग निर्भरता संदर्भ बनाने की अनुमति देता है, जिससे निर्भरता केवल उचित स्थान पर ही उपलब्ध होती है। यह ग्लोबल स्टेट के बचने के कारण कोड के स्पष्ट संगठन और बेहतर परीक्षण को बढ़ावा देता है। यह पुस्तकालय सभी प्लेटफॉर्म पर Flutter ऐप्स के साथ बिना किसी बाधा के एकीकृत होता है और सीमित ज़ोन में निर्भरता पंजीकरण और प्राप्ति के लिए एक सरल API प्रदान करता है।

उपयोग

  • UI-विशिष्ट निर्भरताओं का प्रबंधन
  • अलग-अलग घटकों का परीक्षण
  • वैश्विक राज्य प्रदूषण से बचें
  • मॉड्यूलर एप्लिकेशन खंड बनाना
  • बड़े ऐप में डिपेंडेंसी इंजेक्शन को सरल बनाना

मुख्य विशेषताएँ

  • क्षेत्र-आधारित निर्भरता स्कोपिंग
  • न्यूनतम API सतह
  • क्रॉस-प्लेटफॉर्म समर्थन
  • Flutter के साथ आसान एकीकरण
  • कोई रनटाइम ओवरहेड नहीं

इनके लिए उपयुक्त

  • हल्के DI की तलाश में Flutter विकासकर्ता
  • मॉड्यूलर आर्किटेक्चर वाले ऐप्स
  • स्पष्ट निर्भरता सीमाएं चाहने वाली टीमें
  • परीक्षण-आधारित विकास कार्य प्रवाह
  • भारी फ्रेमवर्क से बचने वाले प्रोजेक्ट

विचार योग्य बातें

  • जटिल निर्भरता ग्राफ के लिए उपयुक्त नहीं
  • Zones की समझ आवश्यक है
  • परिपक्व लाइब्रेरी की तुलना में सीमित दस्तावेज़
  • छोटे से मध्यम आकार के एप्लिकेशन के लिए सर्वोत्तम
  • मौजूदा निर्भरता पैटर्न को पुनर्गठित करने की आवश्यकता हो सकती है

मूल README

संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।

Scoped Deps

A simple dependency injection library built on Zones.

read resolves bindings from an internal registration table that tracks each [Zone] established by this library, so it is not fooled by [Zone]'s inherited [] behavior (which can copy a parent's [ScopedRef] into a middle zone and mask a child's override). For subprocess or other IO that may resume outside that zone, combine with [Zone.bindUnaryCallback] (or related APIs) when registering native callbacks.

Quick Start

import 'package:scoped_deps/scoped_deps.dart';

final value = create(() => 42);

void main() {
  runScoped(scopeA, values: {value});
}

void scopeA() {
  print(read(value)); // 42
  runScoped(scopeB, values: {value.overrideWith(() => 0)});
}

void scopeB() {
  print(read(value)); // 0
}

Recommended: hide read behind a getter

Calling read(valueProvider) at every call site works, but it spreads ScopedRefs through code that shouldn't need to know they exist. Pair each ScopedRef with a top-level getter instead, and let the rest of the app read it like an ordinary variable:

final valueProvider = create(() => 42);
int get value => read(valueProvider);
void main() {
  runScoped(() {
    print(value); // 42
  }, values: {valueProvider});
}

Anything that reads value — no matter how deeply nested — resolves against whichever scope is active when it runs. Callers never import ScopedRef or call read directly, which also makes the pattern straightforward to test (see Testing below).

API

  • create(() => value) — declare a ScopedRef<T>.
  • read(ref, {orElse}) — resolve ref in the current scope; throws a StateError (or calls orElse) if no scope provides it.
  • isRegistered(ref) — check whether ref is bound in the current scope, without the StateError/orElse path.
  • runScoped(body, {values}) — run body in a scope that provides values.
  • runMergedScoped(body, {override, includeIfAbsent}) — run body in a scope forked from the current one. override replaces the current binding for those refs; includeIfAbsent adds a binding only for refs the current scope chain doesn't already define.
  • runMergedScopedFuture(body, {override, includeIfAbsent, zoneSpecification}) — like runMergedScoped, but requires an explicitly async body and accepts an optional zoneSpecification (e.g. to intercept print). override / includeIfAbsent survive await in runMergedScoped too — reach for this variant when you want the stricter async signature or need zoneSpecification, not because bindings would otherwise be lost.
  • runScopedGuarded(body, {onError, values}) — like runScoped, but uncaught errors are routed to onError instead of thrown.
  • ref.overrideWith(() => other) — a ref with the same identity as ref but a different value factory, used to substitute a value for a nested scope (or in a test).

Testing

Bindings live on the Zone, so they don't exist until something establishes a scope — code under test has to run inside a runScoped / runMergedScoped call that provides whatever it reads. A setUp can't do this on its own: it runs to completion before the test body starts, so any scope it forks is already gone by the time your test executes. Wrap the test body itself:

final valueProvider = create(() => 42);
int get value => read(valueProvider);

int doubleValue() => value * 2;

test('doubleValue uses the overridden value', () {
  runScoped(() {
    expect(doubleValue(), equals(20));
  }, values: {valueProvider.overrideWith(() => 10)});
});

The override applies no matter how deep the call chain is — doubleValue() never has to know it's under test. The same works for async code under test; the override stays bound across every await:

Future<int> asyncDoubleValue() async {
  await Future<void>.delayed(Duration.zero);
  return value * 2;
}

test('asyncDoubleValue uses the overridden value', () async {
  await runScoped(() async {
    expect(await asyncDoubleValue(), equals(20));
  }, values: {valueProvider.overrideWith(() => 10)});
});

Reach for runMergedScopedFuture instead when a test also needs a zoneSpecification — for example, to capture print output for the duration of the test rather than letting it hit the console.

श्रेणियाँ

निर्भरताएँ

निर्भरता सूची 1 आइटम

सोर्स रिपॉज़िटरी

इस पैकेज का उपयोग करने वाले प्रोजेक्ट

अभी कोई डेटा उपलब्ध नहीं है।

रैंकिंग