• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Willian199 / dart_ddi / 19187957026

08 Nov 2025 04:25AM UTC coverage: 83.318% (-2.2%) from 85.545%
19187957026

push

github

web-flow
Doc: Added support to Beans with WeakReference and support to Proxy (#31)

95 of 138 new or added lines in 4 files covered. (68.84%)

1 existing line in 1 file now uncovered.

934 of 1121 relevant lines covered (83.32%)

11.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/lib/src/features/instance_wrapper.dart
1
import 'dart:async';
2
import 'dart:core';
3

4
import 'package:dart_ddi/dart_ddi.dart';
5

6
/// Internal wrapper for Instance that uses DDI's getWith methods.
7
///
8
/// This implementation supports:
9
/// - `useWeakReference`: If `true`, maintains a weak reference to the instance.
10
///   This allows the instance to be garbage collected if no other strong references exist.
11
/// - `cache`: If `true`, maintains a strong reference to the instance (caching).
12
///   This prevents the instance from being garbage collected while the Instance wrapper exists.
13
///
14
/// **Important:** If both `useWeakReference` and `cache` are `true`, `cache` takes precedence
15
/// (strong reference is maintained).
16
class InstanceWrapper<BeanT extends Object> implements Instance<BeanT> {
17
  InstanceWrapper({
5✔
18
    required this.qualifier,
19
    required this.ddi,
20
    bool useWeakReference = false,
21
    bool cache = false,
22
  })  : _useWeakReference =
23
            useWeakReference && !cache, // cache takes precedence
24
        _cache = cache;
25

26
  final Object qualifier;
27
  final DDI ddi;
28

29
  /// Whether to use weak reference for the instance.
30
  /// Only effective if `cache` is `false`.
31
  final bool _useWeakReference;
32

33
  /// Whether to cache (strong reference) the instance.
34
  /// Takes precedence over `useWeakReference`.
35
  final bool _cache;
36

37
  /// Strong reference to the cached instance (used when `_cache` is true).
38
  BeanT? _cachedInstance;
39

40
  /// Weak reference to the instance (used when `_useWeakReference` is true and `_cache` is false).
41
  WeakReference<BeanT>? _weakCachedInstance;
42

43
  @override
2✔
44
  bool isResolvable() {
45
    return ddi.isRegistered<BeanT>(qualifier: qualifier);
6✔
46
  }
47

48
  @override
5✔
49
  BeanT get<ParameterT extends Object>({ParameterT? parameter}) {
50
    // Check cache first if enabled
51
    if (_cache && _cachedInstance != null) {
9✔
52
      return _cachedInstance!;
4✔
53
    }
54

55
    // Check weak reference if configured
56
    if (_useWeakReference && _weakCachedInstance != null) {
9✔
57
      final weakInstance = _weakCachedInstance?.target;
8✔
58
      if (weakInstance != null) {
59
        return weakInstance;
60
      }
61
      // Weak reference was collected, clear it
NEW
62
      _weakCachedInstance = null;
×
63
    }
64

65
    // Get instance from DDI
66
    final instance = ddi.getWith<BeanT, ParameterT>(
10✔
67
      qualifier: qualifier,
5✔
68
      parameter: parameter,
69
    );
70

71
    // Update cache/weak reference based on configuration
72
    _updateCache(instance);
5✔
73

74
    return instance;
75
  }
76

77
  @override
2✔
78
  Future<BeanT> getAsync<ParameterT extends Object>(
79
      {ParameterT? parameter}) async {
80
    // Check cache first if enabled
81
    if (_cache && _cachedInstance != null) {
3✔
82
      return _cachedInstance!;
1✔
83
    }
84

85
    // Check weak reference if configured
86
    if (_useWeakReference && _weakCachedInstance != null) {
3✔
87
      final weakInstance = _weakCachedInstance?.target;
2✔
88
      if (weakInstance != null) {
89
        return weakInstance;
90
      }
91
      // Weak reference was collected, clear it
NEW
92
      _weakCachedInstance = null;
×
93
    }
94

95
    // Get instance from DDI
96
    final instance = await ddi.getAsyncWith<BeanT, ParameterT>(
4✔
97
      qualifier: qualifier,
2✔
98
      parameter: parameter,
99
    );
100

101
    // Update cache/weak reference based on configuration
102
    _updateCache(instance);
2✔
103

104
    return instance;
105
  }
106

107
  /// Updates the cache/weak reference based on the configuration.
108
  void _updateCache(BeanT instance) {
5✔
109
    if (_cache) {
5✔
110
      // Cache takes precedence: maintain strong reference
111
      _cachedInstance = instance;
4✔
112
      _weakCachedInstance = null;
4✔
113
    } else if (_useWeakReference) {
5✔
114
      // Use weak reference: allow GC collection
115
      _weakCachedInstance = WeakReference(instance);
8✔
116
      _cachedInstance = null;
4✔
117
    } else {
118
      // No caching: clear both
119
      _cachedInstance = null;
5✔
120
      _weakCachedInstance = null;
5✔
121
    }
122
  }
123

124
  @override
3✔
125
  FutureOr<void> destroy() {
126
    // Clear cache before destroying
127
    _cachedInstance = null;
3✔
128
    _weakCachedInstance = null;
3✔
129
    return ddi.destroy<BeanT>(qualifier: qualifier);
9✔
130
  }
131

132
  @override
2✔
133
  Future<void> dispose() {
134
    // Clear cache before disposing
135
    _cachedInstance = null;
2✔
136
    _weakCachedInstance = null;
2✔
137
    return ddi.dispose<BeanT>(qualifier: qualifier);
6✔
138
  }
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc