Skip to content

Commit 4e5469b

Browse files
committed
feat: async state interface & lazyAsyncState
1 parent b5dc1f7 commit 4e5469b

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package me.devnatan.inventoryframework.state;
2+
3+
public interface AsyncState<T> extends State<T> {}

inventory-framework-api/src/main/java/me/devnatan/inventoryframework/state/State.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
*/
1717
public interface State<T> {
1818

19+
/**
20+
* <p><b><i> This is an internal inventory-framework API that should not be used from outside of
21+
* this library. No compatibility guarantees are provided. </i></b>
22+
*/
23+
@ApiStatus.Internal
1924
AtomicLong ids = new AtomicLong();
2025

2126
/**
@@ -51,8 +56,12 @@ public interface State<T> {
5156
/**
5257
* Generates a new state id.
5358
*
59+
* <p><b><i> This is an internal inventory-framework API that should not be used from outside of
60+
* this library. No compatibility guarantees are provided. </i></b>
61+
*
5462
* @return A new unique state id.
5563
*/
64+
@ApiStatus.Internal
5665
static long next() {
5766
return ids.getAndIncrement();
5867
}

inventory-framework-api/src/main/java/me/devnatan/inventoryframework/state/StateAccess.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,40 @@ public interface StateAccess<
139139
*/
140140
<T> State<T> lazyState(@NotNull Supplier<T> computation);
141141

142+
/**
143+
* Creates an immutable lazy asynchronous state.
144+
* <p>
145+
* This method expects you to return a {@link CompletableFuture} in <code>computation</code>
146+
* parameter so elements in the view that are watching that state can hook to it and wait
147+
* until that future gets completed to proceed with whatever they are willing to do.
148+
* <p>
149+
* <b>This API is experimental and is not subject to the general compatibility guarantees such API
150+
* may be changed or may be removed completely in any further release.</b>
151+
*
152+
* @param computation The value factory.
153+
* @param <T> The state value type.
154+
* @return A new lazy asynchronous state instance.
155+
*/
156+
@ApiStatus.Experimental
157+
<T> State<T> lazyAsyncState(@NotNull Supplier<CompletableFuture<T>> computation);
158+
159+
/**
160+
* Creates an immutable lazy asynchronous state.
161+
* <p>
162+
* This method expects you to return a {@link CompletableFuture} in <code>computation</code>
163+
* parameter so elements in the view that are watching that state can hook to it and wait
164+
* until that future gets completed to proceed with whatever they are willing to do.
165+
* <p>
166+
* <b>This API is experimental and is not subject to the general compatibility guarantees such API
167+
* may be changed or may be removed completely in any further release.</b>
168+
*
169+
* @param computation The value factory.
170+
* @param <T> The state value type.
171+
* @return A new lazy asynchronous state instance.
172+
*/
173+
@ApiStatus.Experimental
174+
<T> State<T> lazyAsyncState(@NotNull Function<Context, CompletableFuture<T>> computation);
175+
142176
/**
143177
* Creates a mutable {@link #lazyState(Function) lazy state} whose value is always computed
144178
* from the initial data set by its {@link StateValueHost}.

inventory-framework-platform/src/main/java/me/devnatan/inventoryframework/PlatformView.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,18 @@ public final <T> State<T> lazyState(@NotNull Supplier<T> computation) {
664664
return stateAccess.lazyState(computation);
665665
}
666666

667+
@Override
668+
public <T> State<T> lazyAsyncState(@NotNull Supplier<CompletableFuture<T>> computation) {
669+
requireNotInitialized();
670+
return stateAccess.lazyAsyncState(computation);
671+
}
672+
673+
@Override
674+
public <T> State<T> lazyAsyncState(@NotNull Function<TContext, CompletableFuture<T>> computation) {
675+
requireNotInitialized();
676+
return stateAccess.lazyAsyncState(computation);
677+
}
678+
667679
@Override
668680
public final <T> MutableState<T> initialState() {
669681
requireNotInitialized();

0 commit comments

Comments
 (0)