Zekfad / disposed

Automatically disposable wrappers for objects utilizing Finalizer. Auto closable StreamController is included!

Home Page:https://pub.dev/packages/disposed

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Disposed

Disposed library provides wrappers for automatic objects disposal utilizing Finalizer.

Notice

Inner Disposable objects of DisposableContainer MUST not have strong references to parent container, failed to follow this rule will cause memory leak and object would not only be never disposed, but also remain in memory as long as program is running.

Features

  • Using this package you can create object destructors.

Usage

Example:

// Container with disposable, but also disposable itself.
class NotificationsProvider extends DisposableContainer implements Disposable {
  final _notificationsController = DisposableStreamController(
    StreamController<String>.broadcast(),
  );

  Stream<String> get notifications => _notificationsController.controller.stream;
 
  void notify(String message) {
    _notificationsController.controller.add(message);
  }

  @override
  late final List<Disposable> disposables = [ _notificationsController, ];

  @override
  void dispose() {
    // Pay attention that this will be called **BEFORE** [disposables] are
    // disposed.
    print('NotificationsProvider is disposed.');
  }
}

// Main container
class NotificationsCenter extends DisposableContainer {
  NotificationsCenter(this.provider);

  final NotificationsProvider provider;

  void notify(String message) {
    provider.notify(message);
  }

  @override
  late final List<Disposable> disposables = [ provider, ];
}

When instance of NotificationsCenter from example above will become inaccessible internal Finalizer will dispose objects in following order:

  • NotificationsProvider - inner Disposable, also a Wrapper
  • DisposableStreamController - inner Disposable

Notice that NotificationsCenter itself is only a wrapper (main one) and it wont be disposed, only garbage collected by the language GC.

Such order is because parent object have references to children, therefore GC destroys parent first, then children will lose references and will be destroyed.

See example for more details.

About

Automatically disposable wrappers for objects utilizing Finalizer. Auto closable StreamController is included!

https://pub.dev/packages/disposed

License:ISC License


Languages

Language:Dart 100.0%