Skip to content

Commit 72510c9

Browse files
committed
Only reload properties for root menus
This is a much safer optimization that does apply in many cases and doesn't require implementations to fully rewrite around the new structure
1 parent 3f7f5a9 commit 72510c9

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceProperties.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public open class InterfaceProperties<P : Pane> {
4646
/** Whether clicking on empty slots should be cancelled. */
4747
public var preventClickingEmptySlots: Boolean = true
4848

49+
/** If `true`, lazy and state properties are always re-evaluated instead of only initialized. */
50+
public var alwaysReloadProperties: Boolean = false
51+
4952
/**
5053
* Whether the player's own inventory should be editable if [preventClickingEmptySlots] is `true`. Only allowed for
5154
* [ChestInterfaceBuilder].

interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/LazyProperty.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public abstract class LazyProperty<T : Any>(
2828
private var updateJob: Deferred<Unit>? = null
2929
private var triggeringUpdate = false
3030
private var lastRefresh: Instant = Instant.MIN
31+
private var initialized: Boolean = false
3132

3233
/** A simple lazy property backed by [block]. */
3334
public class Simple<T : Any>(
@@ -50,6 +51,13 @@ public abstract class LazyProperty<T : Any>(
5051
/** Loads the value of this property. */
5152
public abstract suspend fun load(reload: Boolean = true): T
5253

54+
/** Initializes this property if it hasn't already. */
55+
public suspend fun initialize(view: InterfaceView? = null) {
56+
if (!initialized || value == null) {
57+
reevaluate(reload = !initialized, view = view)
58+
}
59+
}
60+
5361
/**
5462
* Triggers a re-evaluation of the property.
5563
* If [reload] is given, all data should be fully reloaded.
@@ -92,6 +100,7 @@ public abstract class LazyProperty<T : Any>(
92100
}
93101
}
94102
}
103+
initialized = true
95104
lastRefresh = Instant.now()
96105
updateJob = null
97106
}

interfaces/src/main/kotlin/com/noxcrew/interfaces/properties/StateProperty.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public abstract class StateProperty(
3939
private var initialized: Boolean = false
4040

4141
/** Initializes this property if it hasn't already. */
42-
public suspend fun initialize() {
42+
public suspend fun initialize(view: InterfaceView? = null) {
4343
if (!initialized) {
44-
refresh()
44+
refresh(view = view)
4545
}
4646
}
4747

interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,19 +352,31 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interfa
352352
this,
353353
),
354354
) {
355+
// Only fully re-evaluate all properties if this menu wants to, or if this menu is the first
356+
// in a chain (parent is not there or this is the first non-player interface)
357+
val fullyReload = builder.alwaysReloadProperties || parent == null || parent is PlayerInterfaceView
358+
355359
// Run the initialize method on any state properties to refresh them,
356360
// ensure we only refresh every property once even if we need it multiple
357361
// times!
358362
builder.transforms.flatMap { it.triggers }.filterIsInstance<StateProperty>().distinct().forEach {
359363
withTimeout(builder.defaultTimeout) {
360-
it.refresh(view = this@AbstractInterfaceView)
364+
if (fullyReload) {
365+
it.refresh(view = this@AbstractInterfaceView)
366+
} else {
367+
it.initialize(view = this@AbstractInterfaceView)
368+
}
361369
}
362370
}
363371

364372
// Also re-evaluate all lazy properties!
365373
builder.transforms.flatMap { it.triggers }.filterIsInstance<LazyProperty<*>>().distinct().forEach {
366374
withTimeout(builder.defaultTimeout) {
367-
it.reevaluate(view = this@AbstractInterfaceView)
375+
if (fullyReload) {
376+
it.reevaluate(view = this@AbstractInterfaceView)
377+
} else {
378+
it.initialize(view = this@AbstractInterfaceView)
379+
}
368380
}
369381
}
370382
}

0 commit comments

Comments
 (0)