Skip to content

Commit

Permalink
Optimize intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
Iliya-usov committed Oct 19, 2023
1 parent 941e40e commit dc4ff6b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sealed class Lifetime {
* Creates an intersection of some lifetimes: new lifetime that terminate when either one terminates.
* Created lifetime inherits the smallest [terminationTimeoutKind]
*/
fun intersect(lifetime1: Lifetime, lifetime2: Lifetime): Lifetime = defineIntersection(lifetime1, lifetime2).lifetime
fun intersect(lifetime1: Lifetime, lifetime2: Lifetime): Lifetime = lifetime1.intersect(lifetime2)

/**
* Creates an intersection of some lifetimes: new lifetime that terminate when either one terminates.
Expand Down Expand Up @@ -642,7 +642,13 @@ operator fun Lifetime.plusAssign(action : () -> Unit) = onTermination(action)
* Creates an intersection of some lifetimes: new lifetime that terminate when either one terminates.
* Created lifetime inherits the smallest [terminationTimeoutKind]
*/
fun Lifetime.intersect(lifetime: Lifetime): LifetimeDefinition = Lifetime.defineIntersection(this, lifetime)
fun Lifetime.intersect(lifetime: Lifetime): Lifetime {
if (lifetime === this)
return this
return this.defineIntersection(lifetime).lifetime
}

fun Lifetime.defineIntersection(lifetime: Lifetime): LifetimeDefinition = Lifetime.defineIntersection(this, lifetime)

inline fun <T> Lifetime.view(viewable: IViewable<T>, crossinline handler: Lifetime.(T) -> Unit) {
viewable.view(this) { lt, value -> lt.handler(value) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class WriteOnceProperty<T : Any> : IOptProperty<T> {
if (def.isNotAlive || lifetime.isNotAlive) return

val nestedDef = def.intersect(lifetime)
super.advise(nestedDef.lifetime, handler)
super.advise(nestedDef, handler)
}

override fun fire(value: T) {
Expand Down

0 comments on commit dc4ff6b

Please sign in to comment.