@@ -2,6 +2,7 @@ package com.klyx.extension.nodegraph
22
33import androidx.compose.runtime.Immutable
44import androidx.compose.ui.graphics.Color
5+ import com.klyx.core.logging.log
56import com.klyx.nodegraph.EvaluateScope
67import com.klyx.nodegraph.InputPin
78import com.klyx.nodegraph.NodeRegistry
@@ -13,6 +14,7 @@ import com.klyx.nodegraph.SequentialEventNode
1314import com.klyx.nodegraph.customPinType
1415import com.klyx.nodegraph.extension.GraphExtension
1516import com.klyx.nodegraph.extension.Variable
17+ import kotlinx.coroutines.CancellationException
1618import kotlinx.serialization.Serializable
1719
1820internal const val EXTENSION_SCHEMA_VERSION = " 1.0.0"
@@ -119,6 +121,52 @@ internal object OnInitializeMetadataNode : SequentialEventNode() {
119121 override val pins = listOf (OutputHeaderPin (defaultNextLabel))
120122}
121123
124+ private object OnInstall : SequentialEventNode() {
125+ override val key = " klyx.extension.on_install"
126+ override val title = " On Install"
127+ override val category = " Extension"
128+ override val description = " Runs once when the extension is first installed."
129+ override val headerColor = Color (0xFF4CAF50 )
130+ override val triggerName = key
131+
132+ override val pins = listOf (OutputHeaderPin (defaultNextLabel))
133+ }
134+
135+ private object OnUninstall : SequentialEventNode() {
136+ override val key = " klyx.extension.on_uninstall"
137+ override val title = " On Uninstall"
138+ override val category = " Extension"
139+ override val description = " Runs once when the extension is being uninstalled."
140+ override val headerColor = Color (0xFFD32F2F )
141+ override val triggerName = key
142+
143+ override val pins = listOf (OutputHeaderPin (defaultNextLabel))
144+ }
145+
146+ suspend fun ExtensionManager.onInstall (extensionId : String ) {
147+ val graph = getById(extensionId)?.graph ? : return
148+
149+ try {
150+ graph.trigger(OnInstall .triggerName, emptyMap(), listener)
151+ } catch (e: CancellationException ) {
152+ throw e
153+ } catch (e: Exception ) {
154+ log.error { " Extension crash on 'OnInstall': ${e.message} " }
155+ }
156+ }
157+
158+ suspend fun ExtensionManager.onUninstall (extensionId : String ) {
159+ val graph = getById(extensionId)?.graph ? : return
160+
161+ try {
162+ graph.trigger(OnUninstall .triggerName, emptyMap(), listener)
163+ } catch (e: CancellationException ) {
164+ throw e
165+ } catch (e: Exception ) {
166+ log.error { " Extension crash on 'OnUninstall': ${e.message} " }
167+ }
168+ }
169+
122170object MetadataExtension : GraphExtension {
123171
124172 override val name = " klyx-extension-metadata"
@@ -144,7 +192,8 @@ object MetadataExtension : GraphExtension {
144192 registry.register(
145193 MakeMetadataNode ,
146194 BreakMetadataNode ,
147- OnInitializeMetadataNode
195+ OnInitializeMetadataNode ,
196+ OnInstall , OnUninstall
148197 )
149198 }
150199}
0 commit comments