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

Improved await support of ReactiveProperty, ReactiveCommand #86

Merged
merged 1 commit into from
Sep 28, 2018

Conversation

neuecc
Copy link
Collaborator

@neuecc neuecc commented Sep 27, 2018

I've fully refactored await support of ReactiveProperty and ReactiveCommand.
It can handle CancellationToken correctly and add GetAsyncHandler that can reuse(zero-allocate) await event.
Changed push event to awaiter from internal to external.

Breaking Change: AsyncReactiveCommand does not support await.

how to use from awaitable.md


Awaitable

You can use await operator to ReactiveProperty(includes Slim), ReadOnlyReactiveProperty(includes Slim), ReactiveCommand.
When use await operator to them, then wait a next value will be published.

For example:

// View with CancellationTokenSource
public partial class SampleWindow : Window
{
    CancellationTokenSource cts;
    SampleViewModel viewModel;

    public SampleWindow()
    {
        InitializeComponent();
        cts = new CancellationTokenSource();
        viewModel = new SampleViewModel(cts.Token);
    }

    protected override void OnClosed(EventArgs e)
    {
        // on finish, cancel all await.
        cts.Cancel();
        cts.Dispose();

        base.OnClosed(e);
    }
}

// ViewModel with CancellationToken
public class SampleViewModel
{
    public ReactiveCommand MyCommand { get; private set; }
    public ReactiveProperty<int> ClickCount { get; private set; }

    public SampleViewModel(CancellationToken closeToken)
    {
        MyCommand = new ReactiveCommand();
        ClickCount = new ReactiveProperty<int>();

        // handling event by async/await.
        SubscribeAsync(closeToken);
    }

    async void SubscribeAsync(CancellationToken closeToken)
    {
        using (var handler = MyCommand.GetAsyncHandler(closeToken))
        {
            while (true)
            {
                await handler; // await when clicked.
                ClickCount.Value += 1;
            }
        }
    }
}

If you await multiple times, you should get ObservableAsyncHandler<T> from GetAsyncHandler. it can await multiple times on zero allocation. If you await single time, you can use await command.WaitUntilValueChangedAsync(token).

Note: you can await ReactiveProperty directly but we recommend use GetAsyncHandler(multiple) or WaitUntilValueChangedAsync(one shot) with pass over CancellationToken.

@runceel runceel merged commit cbf2ca5 into master Sep 28, 2018
@runceel runceel deleted the awaitable-reactiveproperty-2 branch July 26, 2019 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants