Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web: reuse attrsScope instance per Element instance #1804

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ open class AttrsScopeBuilder<TElement : Element>(
internal var refEffect: (DisposableEffectScope.(TElement) -> DisposableEffectResult)? = null
internal val classes: MutableList<String> = mutableListOf()

internal fun clear() {
eventsListenerScopeBuilder.clear()
attributesMap.clear()
styleScope.clear()
propertyUpdates.clear()
refEffect = null
classes.clear()
}

/**
* [classes] adds all values passed as params to the element's classList.
* This method acts cumulatively, that is, each call adds values to the classList.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,8 @@ open class EventsListenerScopeBuilder : EventsListenerScope {
}

internal fun collectListeners(): List<SyntheticEventListener<*>> = listeners

internal fun clear() {
listeners.clear()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ open class StyleScopeBuilder : StyleScope, StyleHolder {
properties.addAll(sb.properties)
variables.addAll(sb.variables)
}

internal fun clear() {
properties.clear()
variables.clear()
}
}

data class StylePropertyDeclaration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ private class DomElementWrapper(override val node: Element): DomNodeWrapper(node
}
}


@OptIn(ComposeWebInternalApi::class)
@Composable
fun <TElement : Element> TagElement(
Expand All @@ -118,20 +117,20 @@ fun <TElement : Element> TagElement(
DomElementWrapper(node)
},
attrsSkippableUpdate = {
val attrsScope = AttrsScopeBuilder<TElement>()
val attrsScope = scope.attrsScope
attrsScope.clear()
applyAttrs?.invoke(attrsScope)

refEffect = attrsScope.refEffect

update {
set(attrsScope.classes, DomElementWrapper::updateClasses)
set(attrsScope.styleScope, DomElementWrapper::updateStyleDeclarations)
set(attrsScope.collect(), DomElementWrapper::updateAttrs)
set(
attrsScope.eventsListenerScopeBuilder.collectListeners(),
DomElementWrapper::updateEventListeners
)
set(attrsScope.propertyUpdates, DomElementWrapper::updateProperties)
reconcile {
updateClasses(attrsScope.classes)
updateStyleDeclarations(attrsScope.styleScope)
updateAttrs(attrsScope.collect())
updateEventListeners(attrsScope.eventsListenerScopeBuilder.collectListeners())
updateProperties(attrsScope.propertyUpdates)
}
}
},
elementScope = scope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.runtime.RememberObserver
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.currentComposer
import androidx.compose.runtime.remember
import org.jetbrains.compose.web.attributes.AttrsScopeBuilder
import org.w3c.dom.Element

/**
Expand Down Expand Up @@ -122,6 +123,8 @@ internal open class ElementScopeImpl<TElement : Element> : ElementScopeBase<TEle

override val DisposableEffectScope.scopeElement: TElement
get() = element

internal val attrsScope = AttrsScopeBuilder<TElement>()
}

interface DomEffectScope {
Expand Down