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

Use generics in LoadComponentAsync to increase usefulness #863

Merged
merged 5 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/Containers/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Container(LifetimeList<Drawable> lifetimeList = null)

private Game game;

protected Task LoadComponentAsync(Drawable component, Action<Drawable> onLoaded = null) => component.LoadAsync(game, this, onLoaded);
protected Task LoadComponentAsync<TLoadable>(TLoadable component, Action<TLoadable> onLoaded = null) where TLoadable : Drawable => component.LoadAsync(game, this, onLoaded);

[BackgroundDependencyLoader(true)]
private void load(Game game, ShaderManager shaders)
Expand Down
10 changes: 7 additions & 3 deletions osu.Framework/Graphics/Drawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,21 @@ private void dispose(bool isDisposing)
/// </param>
/// <param name="onLoaded">Callback to be invoked asynchronously after loading is complete.</param>
/// <returns>The task which is used for loading and callbacks.</returns>
internal Task LoadAsync(Game game, Drawable target, Action<Drawable> onLoaded = null)
internal Task LoadAsync<T>(Game game, Drawable target, Action<T> onLoaded = null)
where T : Drawable
{
if (loadState != LoadState.NotLoaded)
throw new InvalidOperationException($@"{nameof(LoadAsync)} may not be called more than once on the same Drawable.");

if (!(this is T))
throw new InvalidOperationException($"The {nameof(T)} parameter of the {nameof(LoadAsync)} must be a subtype of {GetType().ReadableName()}.");

loadState = LoadState.Loading;

return loadTask = Task.Run(() => Load(target.Clock, target.Dependencies)).ContinueWith(task => game.Schedule(() =>
{
task.ThrowIfFaulted();
onLoaded?.Invoke(this);
onLoaded?.Invoke((T)this);

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

loadTask = null;
}));
}
Expand Down Expand Up @@ -2246,7 +2250,7 @@ public enum LoadState
NotLoaded,
/// <summary>
/// Currently loading (possibly and usually on a background
/// thread via <see cref="Drawable.LoadAsync(Game, Drawable, Action{Drawable})"/>).
/// thread via <see cref="Drawable.LoadAsync{T}(Game, Drawable, Action{T})"/>).
/// </summary>
Loading,
/// <summary>
Expand Down