Skip to content

Commit 40596b3

Browse files
committed
moved
0 parents  commit 40596b3

File tree

11 files changed

+507
-0
lines changed

11 files changed

+507
-0
lines changed

.gitignore

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
.dart_tool/
26+
.flutter-plugins
27+
.flutter-plugins-dependencies
28+
.packages
29+
.pub-cache/
30+
.pub/
31+
build/
32+
33+
# Android related
34+
**/android/**/gradle-wrapper.jar
35+
**/android/.gradle
36+
**/android/captures/
37+
**/android/gradlew
38+
**/android/gradlew.bat
39+
**/android/local.properties
40+
**/android/**/GeneratedPluginRegistrant.java
41+
42+
# iOS/XCode related
43+
**/ios/**/*.mode1v3
44+
**/ios/**/*.mode2v3
45+
**/ios/**/*.moved-aside
46+
**/ios/**/*.pbxuser
47+
**/ios/**/*.perspectivev3
48+
**/ios/**/*sync/
49+
**/ios/**/.sconsign.dblite
50+
**/ios/**/.tags*
51+
**/ios/**/.vagrant/
52+
**/ios/**/DerivedData/
53+
**/ios/**/Icon?
54+
**/ios/**/Pods/
55+
**/ios/**/.symlinks/
56+
**/ios/**/profile
57+
**/ios/**/xcuserdata
58+
**/ios/.generated/
59+
**/ios/Flutter/App.framework
60+
**/ios/Flutter/Flutter.framework
61+
**/ios/Flutter/Flutter.podspec
62+
**/ios/Flutter/Generated.xcconfig
63+
**/ios/Flutter/app.flx
64+
**/ios/Flutter/app.zip
65+
**/ios/Flutter/flutter_assets/
66+
**/ios/Flutter/flutter_export_environment.sh
67+
**/ios/ServiceDefinitions.json
68+
**/ios/Runner/GeneratedPluginRegistrant.*
69+
70+
# Exceptions to above rules.
71+
!**/ios/**/default.mode1v3
72+
!**/ios/**/default.mode2v3
73+
!**/ios/**/default.pbxuser
74+
!**/ios/**/default.perspectivev3
75+
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

.metadata

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: b176042c1a8a9fdb5c96051b613895e7f0ab167b
8+
channel: master
9+
10+
project_type: package

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## 1.1.0-nullsafety.0 - Dec 11, 2020
2+
3+
* Migrate this package to null safety.
4+
* Sdk constraints: `>=2.12.0-0 <3.0.0` based on beta release guidelines.
5+
6+
## 1.0.0 - Aug 12, 2020
7+
8+
* Initial release.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Petrus Nguyễn Thái Học
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# flutter_disposebag
2+
3+
## Author: [Petrus Nguyễn Thái Học](https://github.com/hoc081098)
4+
5+
[![Pub](https://img.shields.io/pub/v/flutter_disposebag)](https://pub.dev/packages/flutter_disposebag)
6+
7+
* A package to help disposing Streams and closing Sinks easily for Flutter.
8+
* Automatically disposes `StreamSubscription` and closes `Sink` when disposing `State<T>`.
9+
10+
## Usage
11+
12+
A simple usage example:
13+
14+
```dart
15+
import 'dart:async';
16+
17+
import 'package:flutter/material.dart';
18+
import 'package:flutter_disposebag/flutter_disposebag.dart';
19+
20+
class HomePage extends StatefulWidget {
21+
@override
22+
_HomePageState createState() => _HomePageState();
23+
}
24+
25+
class _HomePageState extends State<HomePage> with DisposeBagMixin {
26+
final controller = StreamController<int>();
27+
28+
@override
29+
void initState() {
30+
super.initState();
31+
32+
Stream.periodic(const Duration(milliseconds: 500), (i) => i)
33+
.listen((event) {})
34+
.disposedBy(bag);
35+
36+
controller.stream.listen((event) {}).disposedBy(bag);
37+
controller.disposedBy(bag);
38+
}
39+
40+
@override
41+
Widget build(BuildContext context) {
42+
return Container();
43+
}
44+
}
45+
```
46+
47+
## Features and bugs
48+
49+
Please file feature requests and bugs at the [issue tracker][tracker].
50+
51+
[tracker]: https://github.com/hoc081098/disposebag/issues/new

example/main.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter_disposebag/flutter_disposebag.dart';
5+
6+
class HomePage extends StatefulWidget {
7+
@override
8+
_HomePageState createState() => _HomePageState();
9+
}
10+
11+
class _HomePageState extends State<HomePage> with DisposeBagMixin {
12+
final controller = StreamController<int>();
13+
14+
@override
15+
void initState() {
16+
super.initState();
17+
18+
Stream.periodic(const Duration(milliseconds: 500), (i) => i)
19+
.listen((event) {})
20+
.disposedBy(bag);
21+
22+
controller.stream.listen((event) {}).disposedBy(bag);
23+
controller.disposedBy(bag);
24+
}
25+
26+
@override
27+
Widget build(BuildContext context) {
28+
return Container();
29+
}
30+
}

lib/flutter_disposebag.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library flutter_disposebag;
2+
3+
export 'package:disposebag/disposebag.dart';
4+
5+
export 'src/disposebag_mixin.dart';

lib/src/disposebag_mixin.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import 'dart:async';
2+
3+
import 'package:disposebag/disposebag.dart';
4+
import 'package:flutter/widgets.dart';
5+
import 'package:meta/meta.dart';
6+
7+
/// A mixin that provides the [DisposeBag] that helps disposing Streams and closing Sinks.
8+
@optionalTypeArgs
9+
mixin DisposeBagMixin<T extends StatefulWidget> on State<T> {
10+
Completer<bool>? _mockBagDisposed;
11+
12+
/// Set mock [DisposeBag] for testing purpose.
13+
/// Returns a [Future] that completes when [DisposeBag.dispose] completes.
14+
@visibleForTesting
15+
Future<bool> setMockBag(DisposeBag bag) {
16+
_bag = bag;
17+
return (_mockBagDisposed = Completer<bool>()).future;
18+
}
19+
20+
//
21+
//
22+
//
23+
24+
DisposeBag? _bag;
25+
26+
/// Get [DisposeBag]
27+
@protected
28+
DisposeBag get bag {
29+
if (!mounted) {
30+
throw StateError('Invalid when getting bag after disposed.');
31+
}
32+
return _bag ??= DisposeBag();
33+
}
34+
35+
@override
36+
void dispose() {
37+
final future = _bag?.dispose();
38+
_bag = null;
39+
40+
if (future != null) {
41+
_mockBagDisposed?.complete(future);
42+
}
43+
44+
super.dispose();
45+
}
46+
}

0 commit comments

Comments
 (0)