1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.kairos.internal 18 19 import kotlin.coroutines.CoroutineContext 20 import kotlin.coroutines.EmptyCoroutineContext 21 22 internal class Output<A>( 23 val context: CoroutineContext = EmptyCoroutineContext, <lambda>null24 val onDeath: () -> Unit = {}, 25 val onEmit: EvalScope.(A) -> Unit, 26 ) { 27 28 val schedulable = Schedulable.O(this) 29 30 @Volatile var upstream: NodeConnection<A>? = null 31 @Volatile var result: Any? = NoResult 32 33 private object NoResult 34 35 // invoked by network visitnull36 fun visit(evalScope: EvalScope) { 37 val upstreamResult = result 38 check(upstreamResult !== NoResult) { "output visited with null upstream result" } 39 result = NoResult 40 @Suppress("UNCHECKED_CAST") evalScope.onEmit(upstreamResult as A) 41 } 42 killnull43 fun kill() { 44 onDeath() 45 } 46 schedulenull47 fun schedule(logIndent: Int, evalScope: EvalScope) { 48 result = 49 checkNotNull(upstream) { "output scheduled with null upstream" } 50 .getPushEvent(logIndent, evalScope) 51 evalScope.scheduleOutput(this) 52 } 53 } 54 OneShotnull55internal inline fun OneShot(crossinline onEmit: EvalScope.() -> Unit): Output<Unit> = 56 Output<Unit>(onEmit = { onEmit() }).apply { result = Unit } 57