-
-
Save back2dos/e9c629b649ae5193f814130d80b68601 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tink.state.internal; | |
class AsyncObservable<T> implements ObservableObject<Promised<T>> extends Dispatcher implements Observer { | |
final source:ObservableObject<Promise<T>>; | |
var promise:Promise<T> = null; | |
var hot = false; | |
var link:CallbackLink = null; | |
public function new(source:ObservableObject<Promise<T>> #if tink_state.debug , ?toString, ?pos #end) { | |
super( | |
active -> | |
if (hot = active) source.subscribe(this) | |
else { | |
link.cancel(); | |
source.unsubscribe(this); | |
} | |
#if tink_state.debug , toString, pos #end | |
); | |
this.source = source; | |
} | |
public function getValue():Promised<T> { | |
if (promise == null) | |
promise = source.getValue(); | |
if (hot) | |
link = promise.handle(_ -> trigger()); | |
return switch promise.status { | |
case Ready(l) if (l.computed): | |
switch l.get() { | |
case Success(v): Done(v); | |
case Failure(e): Failed(e); | |
} | |
default: Loading; | |
} | |
} | |
public function isValid():Bool | |
return promise != null; | |
public function getComparator():Comparator<Promised<T>> | |
return null; | |
#if tink_state.debug | |
public function getDependencies():Iterator<Observable<Any>> | |
return [(cast source:Observable<Any>)].iterator(); | |
#end | |
inline function trigger() | |
if (inline isValid()) { | |
promise = null; | |
fire(this); | |
} | |
public function notify<R>(from:ObservableObject<R>) { | |
link.cancel(); | |
trigger(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment