Skip to content

Commit

Permalink
New methods to manage lifecycle hooks (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjaros committed Jan 6, 2024
1 parent 70044ae commit ed9a07e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 13 deletions.
54 changes: 50 additions & 4 deletions src/jsMain/kotlin/io/kvision/core/Component.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,67 @@ interface Component {
getElementD()?.blur()
}


/**
* The supplied function is called before the component is disposed.
* The supplied function is called after the component DOM element is created.
*/
fun addBeforeDisposeHook(hook: () -> Unit): Boolean
fun addAfterCreateHook(hook: (VNode) -> Unit): Boolean

/**
* The supplied function is called after the component is removed from the DOM.
* Remove the after create hook function (the same function reference must be used).
*/
fun addAfterDestroyHook(hook: () -> Unit): Boolean
fun removeAfterCreateHook(hook: (VNode) -> Unit): Boolean

/**
* Remove all after create hooks functions.
*/
fun clearAfterCreateHooks()

/**
* The supplied function is called after the component is inserted into the DOM.
*/
fun addAfterInsertHook(hook: (VNode) -> Unit): Boolean

/**
* Remove the after insert hook function (the same function reference must be used).
*/
fun removeAfterInsertHook(hook: (VNode) -> Unit): Boolean

/**
* Remove all after insert hooks functions.
*/
fun clearAfterInsertHooks()

/**
* The supplied function is called after the component is removed from the DOM.
*/
fun addAfterDestroyHook(hook: () -> Unit): Boolean

/**
* Remove the after destroy hook function (the same function reference must be used).
*/
fun removeAfterDestroyHook(hook: () -> Unit): Boolean

/**
* Remove all after destroy hooks functions.
*/
fun clearAfterDestroyHooks()

/**
* The supplied function is called before the component is disposed.
*/
fun addBeforeDisposeHook(hook: () -> Unit): Boolean

/**
* Remove the before dispose hook function (the same function reference must be used).
*/
fun removeBeforeDisposeHook(hook: () -> Unit): Boolean

/**
* Remove all before dispose hooks functions.
*/
fun clearBeforeDisposeHooks()

/**
* Executes given function within a single rendering process.
*/
Expand Down
61 changes: 52 additions & 9 deletions src/jsMain/kotlin/io/kvision/core/Widget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ open class Widget(internal val className: String? = null, init: (Widget.() -> Un

protected var lastLanguage: String? = null

protected var afterCreateHooks: MutableList<(VNode) -> Unit>? = null
protected var afterInsertHooks: MutableList<(VNode) -> Unit>? = null
protected var afterDestroyHooks: MutableList<() -> Unit>? = null
private var beforeDisposeHooks: MutableList<() -> Unit>? = null
Expand All @@ -135,13 +136,39 @@ open class Widget(internal val className: String? = null, init: (Widget.() -> Un
}

/**
* The supplied function is called before the widget is disposed.
* The supplied function is called after the widget DOM element is created.
*/
override fun addBeforeDisposeHook(hook: () -> Unit) = (beforeDisposeHooks ?: run {
beforeDisposeHooks = mutableListOf()
beforeDisposeHooks!!
override fun addAfterCreateHook(hook: (VNode) -> Unit) = (afterCreateHooks ?: run {
useSnabbdomDistinctKey()
afterCreateHooks = mutableListOf()
afterCreateHooks!!
}).add(hook)

override fun removeAfterCreateHook(hook: (VNode) -> Unit): Boolean {
return afterCreateHooks?.remove(hook) ?: false
}

override fun clearAfterCreateHooks() {
afterCreateHooks?.clear()
}

/**
* The supplied function is called after the widget is inserted into the DOM.
*/
override fun addAfterInsertHook(hook: (VNode) -> Unit) = (afterInsertHooks ?: run {
useSnabbdomDistinctKey()
afterInsertHooks = mutableListOf()
afterInsertHooks!!
}).add(hook)

override fun removeAfterInsertHook(hook: (VNode) -> Unit): Boolean {
return afterInsertHooks?.remove(hook) ?: false
}

override fun clearAfterInsertHooks() {
afterInsertHooks?.clear()
}

/**
* The supplied function is called after the widget is removed from the DOM.
*/
Expand All @@ -151,15 +178,30 @@ open class Widget(internal val className: String? = null, init: (Widget.() -> Un
afterDestroyHooks!!
}).add(hook)

override fun removeAfterDestroyHook(hook: () -> Unit): Boolean {
return afterDestroyHooks?.remove(hook) ?: false
}

override fun clearAfterDestroyHooks() {
afterDestroyHooks?.clear()
}

/**
* The supplied function is called after the widget is inserted into the DOM.
* The supplied function is called before the widget is disposed.
*/
override fun addAfterInsertHook(hook: (VNode) -> Unit) = (afterInsertHooks ?: run {
useSnabbdomDistinctKey()
afterInsertHooks = mutableListOf()
afterInsertHooks!!
override fun addBeforeDisposeHook(hook: () -> Unit) = (beforeDisposeHooks ?: run {
beforeDisposeHooks = mutableListOf()
beforeDisposeHooks!!
}).add(hook)

override fun removeBeforeDisposeHook(hook: () -> Unit): Boolean {
return beforeDisposeHooks?.remove(hook) ?: false
}

override fun clearBeforeDisposeHooks() {
beforeDisposeHooks?.clear()
}

override fun <T> singleRender(block: () -> T): T {
val root = getRoot()
return if (root != null) {
Expand Down Expand Up @@ -362,6 +404,7 @@ open class Widget(internal val className: String? = null, init: (Widget.() -> Un
create = { _, v ->
vnode = v
afterCreate(v)
afterCreateHooks?.forEach { it(v) }
}
insert = { v ->
vnode = v
Expand Down

0 comments on commit ed9a07e

Please sign in to comment.