Skip to content

Commit

Permalink
Add auto saving to managed config
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed May 6, 2024
1 parent 2aabe8c commit c7b3694
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public String getTitle() {
}

public void saveNow() {
for (Runnable saveRunnable : saveRunnables) {
saveRunnable.run();
}
}

public DescriptionRendereringBehaviour getDescriptionBehaviour(ProcessedOption option) {
Expand All @@ -68,4 +71,6 @@ public boolean shouldAutoFocusSearchbar() {
public boolean shouldSearchCategoryNames() {
return true;
}

public transient List<Runnable> saveRunnables = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ class ManagedConfig<T : Config>(private val builder: ManagedConfigBuilder<T>) :
return ManagedConfig(ManagedConfigBuilder(file, clazz).apply(consumer))
}
}
// TODO: enforce the save callback, somehow

lateinit var processor: MoulConfigProcessor<T>
private set

override fun injectIntoInstance() {
instance.saveRunnables?.add(this::saveToFile)
}

fun rebuildConfigProcessor() {
rebuildConfigProcessor(builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ open class ManagedDataFile<T> internal constructor(
} catch (ex: Exception) {
loadFailed.accept(this, ex)
}
injectIntoInstance()
afterLoad.accept(this)
}

open fun injectIntoInstance() {}

private fun createUniqueExtraFile(identifier: String, directory: File = file.parentFile): File {
val jvmHash = ManagementFactory.getRuntimeMXBean().name.hashCode()
val timestamp = System.currentTimeMillis().toString()
Expand All @@ -86,4 +89,4 @@ open class ManagedDataFile<T> internal constructor(
}
afterSave.accept(this)
}
}
}

0 comments on commit c7b3694

Please sign in to comment.