dispose

Creator: coderz1093

Last updated:

0 purchases

dispose Image
dispose Images

Languages

Categories

Add to Cart

Description:

dispose

Are you tired of having to cancel subscriptions, timers and close controllers? #
Have you ever created an Angular Component or Flutter Widget that has some Stream being listened or Timer that are still executed after the component or widget destruction?
Then you have to bind a variable to the subscription so you can cancel it on the component destruction?
Not to mention that if your Timer/Stream is unique you have to cancel it before reassigning it again.
This all happens because there isn't really a connection between your component/widget to the stream/timer/controller.
Well, this package aims to help you with that.
The package exposes a Disposable class that has a dispose() function that clears any listeners, timers, controllers etc that were created within it. You can make the objects unique by setting a paramater Symbol uniqueId in your calls.
There is also a Angular class that already disposes stuff on OnDestroy (package:dispose/angular.dart).
In the examples below you have to call the dispose() method of SomeClass somewhere. Usually in widget/component destruction.
Examples:
import 'package:dispose/dispose.dart';
copied to clipboard
each #
It iterates through the stream and disposes the listener within dispose().
Example:

class SomeClass extends Disposable {
void listenToInts(Stream<int> myIntStream) {
each(myIntStream, (int value) {
print('someInt $value');
}, uniqueId: #MyUniqueListener);
}
}
copied to clipboard
controller #
It creates a StreamController<T> that is closed within dispose().
Example:

class SomeClass extends Disposable {
late StreamController<int> _intController;

SomeClass() {
_intController = controller(broadcast: false);
}
}
copied to clipboard
timer #
It creates a Timer that is cancelled within dispose() if it wasn't already executed.
Example:

class SomeClass extends Disposable {
void method() {
timer(Duration(seconds: 5), () => print('Executed!'));
// the above timer will be free in dispose() if not executed
}
}
copied to clipboard
periodic #
It creates a Timer.periodic that is cancelled within dispose().
Example:

class SomeClass extends Disposable {
void method() {
var x = 0;
periodic(Duration(seconds: 5), ()
=> print('Executed ${++x}'));
}
}
copied to clipboard
bind #
Binds another Disposable object that will be freed along in dispose().
Example:

class SomeOtherClass extends Disposable {
Timer myPeriodic;
SomeOtherClass() {
myPeriodic = periodic(Duration(seconds: 30));
}
}
class SomeClass extends Disposable {
final prop = SomeOtherClass();

SomeClass() {
// prop will be disposed in SomeClass.dispose() if
// it wasn't already.
bind(prop);
}
}
copied to clipboard
cancelBind & cancelTimer #
Use these methods to cancel unique bindings and timers.

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.