CommunityToolkit / CommunityToolkit/dotnet

ObservableProperty attribute doesn't generate proper code for TaskNotifier type

Open
#418 0 comments 1 reaction 1 assignee Claimed by @Sergio0694 View on GitHub
bug :bug: mvvm-toolkit :toolbox:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug

When trying to observe a Task using the MVVM Toolkit, there's a special helper type called `TaskNotifier`, [as called out in the docs here](https://docs.microsoft.com/dotnet/communitytoolkit/mvvm/observableobject#handling-taskt-properties).

However, this currently doesn't generate the proper output when using the source generator helper:

```cs
public global::CommunityToolkit.Mvvm.ComponentModel.ObservableObject.TaskNotifier? InitializeTask
{
get => _initializeTask;
set
{
if (!global::System.Collections.Generic.EqualityComparer?>.Default.Equals(_initializeTask, value))
{
OnInitializeTaskChanging(value);
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.InitializeTask);
_initializeTask = value;
OnInitializeTaskChanged(value);
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.InitializeTask);
}
}
}
```

Note how the value is set directly and no monitor is setup as is normally done in the ]`SetPropertyAndNotifyOnCompletion` method](https://github.com/CommunityToolkit/dotnet/blob/9d1a525d04299e5421cb3e5917825fee2bc11a18/CommunityToolkit.Mvvm/ComponentModel/ObservableObject.cs#L485).

Also note how the property is using `TaskNotifier` as the type instead of `Task`.

### Regression

_No response_

### Steps to reproduce

```text
1. Create an Observable object
2. Add a task field with a SG attribute:

[ObservableProperty]
private TaskNotifier? _initializeTask;
```

### Expected behavior

Generated property should be of type `Task`.

Generated method should call or be the same code as `SetPropertyAndNotifyOnCompletion`.

Have an additional partial method that represents the callback for task completion `OnInitiaizeTaskCompleted`?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.