refactor: remove manual plugin registration

This commit is contained in:
Cayden Tisdale 2026-09-08 11:39:44 -04:00
parent e97e3ab53b
commit 62827cdfd2
No known key found for this signature in database
2 changed files with 8 additions and 12 deletions

View File

@ -107,7 +107,7 @@ package plugin
internal fun createPlugin(): PumpkinPlugin = example.MyPlugin() internal fun createPlugin(): PumpkinPlugin = example.MyPlugin()
``` ```
This replaces the API's placeholder factory during consumer compilation. On the first exported callback, the API bridge constructs and caches your plugin instance. Later callbacks use the same instance. There is no need for `main()`, a top-level registration initializer, or a call to `registerPlugin(...)`. The package also supplies the generator-specific `PluginRootFunctionsExportsImpl` and `MetadataImpl` bridges. This replaces the API's placeholder factory during consumer compilation. On the first exported callback, the API bridge constructs and caches your plugin instance. Later callbacks use the same instance. The package supplies the generator-specific `PluginRootFunctionsExportsImpl` and `MetadataImpl` bridges.
## Server compatibility ## Server compatibility

View File

@ -69,24 +69,20 @@ abstract class PumpkinPlugin {
typealias PluginMetadata = Metadata.PluginMetadata typealias PluginMetadata = Metadata.PluginMetadata
private var registeredPlugin: PumpkinPlugin? = null private var pluginInstance: PumpkinPlugin? = null
/** Registers the plugin instance exported by this WebAssembly component. */ private fun requirePlugin(): PumpkinPlugin = pluginInstance ?: createPlugin().also {
fun registerPlugin(plugin: PumpkinPlugin) { pluginInstance = it
check(registeredPlugin == null) { "A Pumpkin plugin has already been registered." }
registeredPlugin = plugin
}
private fun requirePlugin(): PumpkinPlugin = registeredPlugin ?: createPlugin().also {
registeredPlugin = it
} }
private fun <T> unsupportedCallback(name: String): T = private fun <T> unsupportedCallback(name: String): T =
error("PumpkinPlugin.$name has not been implemented.") error("PumpkinPlugin.$name has not been implemented.")
/** /**
* WIT export bridge used by the generated bindings. Plugin authors use [PumpkinPlugin] and * WIT export bridge used by the generated bindings. Plugin authors extend [PumpkinPlugin]
* [registerPlugin] instead of implementing this generated interface directly. * and configure pumpkin.pluginClass.
*
* The generated bootstrap supplies the instance automatically.
*/ */
internal class PluginRootFunctionsExportsImpl { internal class PluginRootFunctionsExportsImpl {
companion object : PluginRootFunctions.Exports { companion object : PluginRootFunctions.Exports {