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

Willian199 / dart_ddi / 24111353992

08 Apr 2026 12:36AM UTC coverage: 94.388% (+1.0%) from 93.35%
24111353992

push

github

web-flow
Feature/drop zone support (#35)

7 of 7 new or added lines in 3 files covered. (100.0%)

64 existing lines in 6 files now uncovered.

1396 of 1479 relevant lines covered (94.39%)

16.19 hits per line

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

91.67
/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({
8✔
18
    required this.qualifier,
19
    required this.ddi,
20
    required this.context,
21
    bool useWeakReference = false,
22
    bool cache = false,
23
  })  : _useWeakReference =
24
            useWeakReference && !cache, // cache takes precedence
25
        _cache = cache;
26

27
  final Object qualifier;
28
  final DDI ddi;
29
  final Object context;
30

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

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

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

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

45
  @override
3✔
46
  @pragma('vm:prefer-inline')
47
  bool isResolvable() {
48
    return ddi.isRegistered<BeanT>(qualifier: qualifier, context: context);
12✔
49
  }
50

51
  @override
8✔
52
  @pragma('vm:prefer-inline')
53
  BeanT get<ParameterT extends Object>({ParameterT? parameter}) {
54
    if (!_cache && !_useWeakReference) {
16✔
55
      return ddi.getWith<BeanT, ParameterT>(
16✔
56
        qualifier: qualifier,
8✔
57
        parameter: parameter,
58
        context: context,
8✔
59
      );
60
    }
61

62
    if (_cache && _cachedInstance != null) {
12✔
63
      return _cachedInstance!;
6✔
64
    }
65

66
    if (_useWeakReference && _weakCachedInstance != null) {
11✔
67
      final weakInstance = _weakCachedInstance?.target;
10✔
68
      if (weakInstance != null) {
69
        return weakInstance;
70
      }
UNCOV
71
      _weakCachedInstance = null;
×
72
    }
73

74
    final instance = ddi.getWith<BeanT, ParameterT>(
12✔
75
      qualifier: qualifier,
6✔
76
      parameter: parameter,
77
      context: context,
6✔
78
    );
79

80
    _updateCache(instance);
6✔
81

82
    return instance;
83
  }
84

85
  @override
4✔
86
  Future<BeanT> getAsync<ParameterT extends Object>(
87
      {ParameterT? parameter}) async {
88
    if (!_cache && !_useWeakReference) {
8✔
89
      return ddi.getAsyncWith<BeanT, ParameterT>(
8✔
90
        qualifier: qualifier,
4✔
91
        parameter: parameter,
92
        context: context,
4✔
93
      );
94
    }
95

96
    if (_cache && _cachedInstance != null) {
2✔
97
      return _cachedInstance!;
1✔
98
    }
99

100
    if (_useWeakReference && _weakCachedInstance != null) {
2✔
101
      final weakInstance = _weakCachedInstance?.target;
2✔
102
      if (weakInstance != null) {
103
        return weakInstance;
104
      }
UNCOV
105
      _weakCachedInstance = null;
×
106
    }
107

108
    final instance = await ddi.getAsyncWith<BeanT, ParameterT>(
2✔
109
      qualifier: qualifier,
1✔
110
      parameter: parameter,
111
      context: context,
1✔
112
    );
113

114
    _updateCache(instance);
1✔
115

116
    return instance;
117
  }
118

119
  @pragma('vm:prefer-inline')
6✔
120
  void _updateCache(BeanT instance) {
121
    if (_cache) {
6✔
122
      _cachedInstance = instance;
6✔
123
      _weakCachedInstance = null;
6✔
124
    } else if (_useWeakReference) {
5✔
125
      _weakCachedInstance = WeakReference(instance);
10✔
126
      _cachedInstance = null;
5✔
127
    } else {
UNCOV
128
      _cachedInstance = null;
×
UNCOV
129
      _weakCachedInstance = null;
×
130
    }
131
  }
132

133
  @override
3✔
134
  FutureOr<void> destroy() {
135
    _cachedInstance = null;
3✔
136
    _weakCachedInstance = null;
3✔
137
    return ddi.destroy<BeanT>(qualifier: qualifier, context: context);
12✔
138
  }
139

140
  @override
2✔
141
  Future<void> dispose() {
142
    _cachedInstance = null;
2✔
143
    _weakCachedInstance = null;
2✔
144
    return ddi.dispose<BeanT>(qualifier: qualifier, context: context);
8✔
145
  }
146
}
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