using
자동 정리 및 최종화 기능을 통해 클래스를 업그레이드하세요. C#의 "using + IDisposable"과 Java의 "try-with-resource + AutoCloseable"와 유사합니다.
이 패키지는 C#의 IDisposable과 Java의 AutoCloseable와 유사하게 어떤 클래스에도 자동 정리 및 최종화 기능을 제공하는 Releasable 믹스인을 제공합니다. 확장 메서드는 C# 사용 방식과 Java의 try-with-resource 구문에 가까운 구조로 개발 경험을 향상시킵니다.
아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
This package provides a Releasable mixin to upgrade classes with automatic
resource cleanup and finalization features.
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();
}
}
사용 목적에 따라 검색하고 개발 활동을 비교한 뒤 원본 저장소에서 요구 사항을 확인하세요.
“A repository is the most basic element of GitHub.”
GitHub Docs — About repositories ↗