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

Add scheduler extension for actions #518

Merged
merged 2 commits into from
May 29, 2018
Merged

Add scheduler extension for actions #518

merged 2 commits into from
May 29, 2018

Conversation

danielcweber
Copy link
Collaborator

This will allow to pass state to a scheduled action to avoid closure allocation and allow for delegate caching.

Note this is based on changes from #515 which should be merged before this PR.

/// <param name="state">A state object to be passed to <paramref name="action"/>.</param>
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
public static IDisposable Schedule<TState>(this IScheduler scheduler, Action<TState> action, TState state)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit test?

Also the typical signature is state, action.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the signature will create ambiguity with another overload. I will, for the moment, leave the signature as it is but make it internal so it can change without hassle.

@Yeah69
Copy link

Yeah69 commented May 18, 2018

In all of my projects (private and in the company) I also add a "MinimalSchedule" extension method which even dismisses the state parameter competely (as generic parameter for the action and parameter for the extension method) and uses Unit.Default instead. Its handy in all cases where the state isn't relevant and isn't used.

@danielcweber
Copy link
Collaborator Author

Could you post it?

@Yeah69
Copy link

Yeah69 commented May 18, 2018

@danielcweber yeah, sure. I made a gist for you: https://gist.github.com/Yeah69/41f6c3bcfcfde7cbe53ae178a58bfbbc

A little bonus is the async version ;)
I see that you declare the extension method internal. That way no one outside of this project can benefit of this extension methods.

@danielcweber
Copy link
Collaborator Author

Yes, the signature is up to debate because of possible ambiguity, therefore it is declared internal.

…Action<TState> and pass a corresponding state object. The ability to pass state will greatly help to reduce the allocations of closures.
@danielcweber danielcweber merged commit 5d9d856 into dotnet:master May 29, 2018
@danielcweber danielcweber deleted the AddSchedulerExtensionForActions branch May 29, 2018 12:37
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.

3 participants