Class AsyncLazy<T>
Provides support for asynchronous lazy initialization.
Implements
Inherited Members
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncLazy<T> : ISupplier<CancellationToken, Task<T>>, IFunctional<Func<CancellationToken, Task<T>>>, IResettable
Type Parameters
| Name | Description |
|---|---|
| T | The type of object that is being asynchronously initialized. |
Constructors
| Edit this page View SourceAsyncLazy(Func<CancellationToken, Task<T>>, bool)
Initializes a new instance of lazy value.
Declaration
public AsyncLazy(Func<CancellationToken, Task<T>> valueFactory, bool resettable = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<CancellationToken, Task<T>> | valueFactory | The function used to compute actual value. |
| bool | resettable | true if previously computed value can be removed and computation executed again when it will be requested; false if value can be computed exactly once. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException |
|
AsyncLazy(T)
Initializes a new instance of lazy value which is already computed.
Declaration
public AsyncLazy(T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | Already computed value. |
Properties
| Edit this page View SourceIsValueCreated
Gets a value that indicates whether a value has been computed.
Declaration
public bool IsValueCreated { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Value
Gets value if it is already computed.
Declaration
public Result<T>? Value { get; }
Property Value
| Type | Description |
|---|---|
| Result<T>? |
Methods
| Edit this page View SourceReset()
Removes already computed value from the current object.
Declaration
public bool Reset()
Returns
| Type | Description |
|---|---|
| bool | true if previous value is removed successfully; false if value is still computing or this instance is not resettable. |
ToString()
Returns textual representation of this object.
Declaration
public override string? ToString()
Returns
| Type | Description |
|---|---|
| string | The string representing this object. |
Overrides
| Edit this page View SourceWithCancellation(CancellationToken)
Gets already completed task or invokes the factory.
Declaration
public Task<T> WithCancellation(CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| Task<T> | Lazy representation of the value. |