RequiresDataStore

Optional interface for Module implementations that require a DataStoreDelegate.

Implementing this interface signals to rememberModules that this module needs a DataStore instance. The DataStore is initialised automatically via initDataStore before Module.initModule is called, so dataStoreDelegate is guaranteed to be ready when initModule runs.

Minimal usage — DataStore only, no extra init

For modules that only need a DataStore and no further Composable-context initialisation, implementing this interface is sufficient — no Module.initModule override is required:

object MyModule : Module, RequiresDataStore {
override val dataStoreName = "my_module.preferences_pb"
override val dataStoreDelegate = DataStoreDelegate()
// DataStore is ready via dataStoreDelegate.get() once rememberModules has run
}

Usage with extra Composable-context initialisation

For modules that need additional initialisation beyond DataStore (e.g. setting up repositories), override Module.initModule. The DataStore is guaranteed initialised before Module.initModule is called:

class MyModule(
private val resourceLoader: suspend (String) -> ByteArray
) : Module, RequiresDataStore {
override val dataStoreName = "my_module.preferences_pb"
override val dataStoreDelegate = DataStoreDelegate()

@Composable
override fun initModule() {
// dataStoreDelegate.get() is safe to call here
MyModuleInitializer.initialize(
dataStore = dataStoreDelegate.get(),
resourceLoader = resourceLoader
)
}
}

Sharing a DataStore across multiple modules

If a DataStore instance must be shared between two separate modules (e.g. a Ktor plugin module and a UI module), declare the delegate as a top-level val in the shared core module and point both modules at it:

// In the shared core module
public val myModuleDataStoreDelegate = DataStoreDelegate()

// In the UI module — drives initialisation
class MyModule(...) : Module, RequiresDataStore {
override val dataStoreName = "my_module.preferences_pb"
override val dataStoreDelegate = myModuleDataStoreDelegate
}

// In the Ktor plugin module — reads from the same delegate
val dataStore = myModuleDataStoreDelegate.get()

See also

Properties

Link copied to clipboard

The DataStoreDelegate instance that holds this module's DataStore.

Link copied to clipboard
abstract val dataStoreName: String

The filename used for this module's DataStore preferences file.

Functions

Link copied to clipboard

Initialises the DataStore instance for this module.