搜尋套件、儲存庫與 APP 分類

GitHub 儲存庫d-markey/using

using

此套件提供 Releasable 混入,可為任何類別升級自動清除與終結功能,類似於 C# 的 IDisposable 與 Java 的 AutoCloseable。擴展方法透過接近 C# 使用方式及 Java 的 try-with-resource 結構,提升開發體驗。

using 專案封面
星標
2
分支
0
最近推送(UTC)
2025年11月2日
專案狀態
未封存

原始 README

以下為英文專案原文快照,最新內容請造訪 GitHub。

Using

This package provides a Releasable mixin to upgrade classes with automatic resource cleanup and finalization features.

Examples

class Resource with Releasable {
  // manage resource

  @override
  void release() {
    // clean-up resource in this method
    // and make sure super.release() is called!
    super.release();
  }
}

void main() {
  Resource? res;
  try {
    res = Resource(); // allocate
    // use resource
  } finally {
    // release in a finally block to ensure resources are released
    // even when an error occurs
    res?.release();
  }
}

Extension methods are provided to support syntaxes similar to C#'s using and Java's try-with-resource. They also help make sure resources are properly released!

void main() {
  final data = Resource().use((res) {
    // use `res` to load and return some data
  });
  // the instance created by `Resource()` is guaranteed to be released

  final asyncData = await Resource().useAsync((res) async {
    // use `res` to load and return some data
  });
  // the instance created by `Resource()` is guaranteed to be released
}

Streams are also supported via the consume() extension method:

void main() {
  final outputs = await Resource().consume(inputStream, (res, input) async {
    // use `res` to process inputs and return outputs
  }).toList();
  // the instance created by `Resource()` is guaranteed to be released
}

Automatic finalization is also available to try ensure resources will be released even in situations where release() has not been called. However, no promises are made that this will really happen. For more information, see Dart's documentation for Finalizer.

class ResourceOwner with Releasable {
  final _res1 = Resource();
  final _res2 = Resource();

  ResourceOwner() {
    // non-deterministic clean-up relying on Dart's `Finalizer` class. If
    // the resource owner goes out of scope and `release()` was not called,
    // the two resources should eventually be released and reclaimed by a
    // `Finalizer`.
    autoRelease([_res1, _res2]);
  }

  // deterministic clean-up by explicitly calling `release()` on this
  // instance, or when the instance is used via extension methods (`use()`,
  // `consume()`...).
  @override
  void release() {
    // clean-up resources
    _res1.release();
    _res2.release();
    // call super method
    super.release();
  }
}
David MARKEY GitHub avatar
GitHub 作者

David MARKEY ↗

France
語言Dart

此儲存庫發佈的套件

使用的依賴

依賴清單 3 項

如何選擇適合專案的儲存庫?

先依使用情境篩選,再比較專案活躍情況,最後到原始儲存庫確認使用要求。

  1. 透過儲存庫名稱、作者或描述搜尋,再依技術分類縮小範圍。
  2. 綜合星標、分支與最近推送比較專案;星標反映社群關注,不保證品質。
  3. 使用前在 GitHub 確認 README、授權條款與 Issues。

來源與維護

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗