Skip to content

how to implement toFuture from AsyncState #198

Answered by yousefak007
yousefak007 asked this question in Q&A
Discussion options

You must be logged in to vote

edit: after improvements

extension SignalAsyncStateExtensions<T> on ReadonlySignal<AsyncState<T>> {
  Future<T> toFuture() async {
    Completer<T>? completer = Completer<T>();

    // register disposers

    EffectCleanup? disposer;

    // when signal disposed before future finished
    onDispose(() {
      disposer?.call();

      if (completer?.isCompleted == false) {
        completer!.completeError('future not completed');
      }

      completer = null;
    });

    // when future finished and signal still alive
    completer!.future.whenComplete(() {
      disposer?.call();
      completer = null;
    }).ignore();

    disposer = subscribe((value) => _toFuture(completer!, value))…

Replies: 2 comments 13 replies

Comment options

You must be logged in to vote
1 reply
@yousefak007
Comment options

Comment options

You must be logged in to vote
12 replies
@yousefak007
Comment options

@jinyus
Comment options

@yousefak007
Comment options

@jinyus
Comment options

@rodydavis
Comment options

Answer selected by yousefak007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants