@@ -37,13 +37,28 @@ class _InternalDisposable implements _Disposable {
37
37
}
38
38
}
39
39
40
+ /// Managers for disposable members.
41
+ ///
42
+ /// This interface allows consumers to exercise more control over how
43
+ /// disposal is implemented for their classes.
44
+ ///
45
+ /// When new management methods are to be added, they should be added
46
+ /// here first, then implemented in [Disposable] .
47
+ abstract class DisposableManager {
48
+ void manageDisposable (Disposable disposable);
49
+ void manageDisposer (Disposer disposer);
50
+ void manageStreamController (StreamController controller);
51
+ void manageStreamSubscription (StreamSubscription subscription);
52
+ }
53
+
40
54
/// A function that, when called, disposes of one or more objects.
41
55
typedef Future <dynamic > Disposer ();
42
56
43
57
/// Allows the creation of managed objects, including helpers for common patterns.
44
58
///
45
- /// There are three ways to consume this class: as a mixin, a base class,
46
- /// and an interface. All should work fine but the first is the simplest
59
+ /// There are four ways to consume this class: as a mixin, a base class,
60
+ /// an interface, and a concrete class used as a proxy. All should work
61
+ /// fine but the first is the simplest
47
62
/// and most powerful. Using the class as an interface will require
48
63
/// significant effort.
49
64
///
@@ -102,7 +117,33 @@ typedef Future<dynamic> Disposer();
102
117
/// myDisposable.didDispose.then((_) {
103
118
/// // External cleanup
104
119
/// });
105
- abstract class Disposable implements _Disposable {
120
+ ///
121
+ /// Below is an example of using the class as a concrete proxy.
122
+ ///
123
+ /// class MyLifecycleThing implements DisposableManager {
124
+ /// Disposable _disposable = new Disposable();
125
+ ///
126
+ /// MyLifecycleThing() {
127
+ /// _disposable.manageStreamSubscription(someStream.listen(() => null));
128
+ /// }
129
+ ///
130
+ /// @override
131
+ /// void manageStreamSubscription(StreamSubscription sub) {
132
+ /// _disposable.manageStreamSubscription(sub);
133
+ /// }
134
+ ///
135
+ /// // ...more methods
136
+ ///
137
+ /// Future<Null> unload() async {
138
+ /// await _disposable.dispose();
139
+ /// }
140
+ /// }
141
+ ///
142
+ /// In this case, we want `MyLifecycleThing` to have its own lifecycle
143
+ /// without explicit reference to [Disposable] . To do this, we use
144
+ /// composition to include the [Disposable] machinery without changing
145
+ /// the public interface of our class or polluting its lifecycle.
146
+ class Disposable implements _Disposable , DisposableManager {
106
147
Completer <Null > _didDispose = new Completer <Null >();
107
148
bool _isDisposing = false ;
108
149
List <_Disposable > _internalDisposables = [];
@@ -155,7 +196,7 @@ abstract class Disposable implements _Disposable {
155
196
///
156
197
/// The parameter may not be `null` .
157
198
@mustCallSuper
158
- @protected
199
+ @override
159
200
void manageDisposable (Disposable disposable) {
160
201
_throwIfNull (disposable, 'disposable' );
161
202
_internalDisposables.add (disposable);
@@ -165,7 +206,7 @@ abstract class Disposable implements _Disposable {
165
206
///
166
207
/// The parameter may not be `null` .
167
208
@mustCallSuper
168
- @protected
209
+ @override
169
210
void manageDisposer (Disposer disposer) {
170
211
_throwIfNull (disposer, 'disposer' );
171
212
_internalDisposables.add (new _InternalDisposable (disposer));
@@ -175,7 +216,7 @@ abstract class Disposable implements _Disposable {
175
216
///
176
217
/// The parameter may not be `null` .
177
218
@mustCallSuper
178
- @protected
219
+ @override
179
220
void manageStreamController (StreamController controller) {
180
221
_throwIfNull (controller, 'controller' );
181
222
_internalDisposables.add (new _InternalDisposable (() {
@@ -190,7 +231,7 @@ abstract class Disposable implements _Disposable {
190
231
///
191
232
/// The parameter may not be `null` .
192
233
@mustCallSuper
193
- @protected
234
+ @override
194
235
void manageStreamSubscription (StreamSubscription subscription) {
195
236
_throwIfNull (subscription, 'subscription' );
196
237
_internalDisposables
0 commit comments