Skip to content

Commit

Permalink
Organize extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
GallopingDino committed Apr 3, 2023
1 parent 8638450 commit 756506e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ end_of_line = crlf
# Sort using and Import directives with System.* appearing first
dotnet_sort_system_directives_first = true
# Put a blank line between System.* and Microsoft.*
dotnet_separate_import_directive_groups = true
dotnet_separate_import_directive_groups = false

# Avoid "this." and "Me." if not necessary
dotnet_style_qualification_for_field = false:suggestion
Expand Down
14 changes: 11 additions & 3 deletions Assets/Plugins/UniRx/Scripts/Observable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UniRx.InternalUtil;
using UniRx.Operators;

Expand Down Expand Up @@ -65,6 +63,16 @@ public static IObservable<T> Where<T>(this IObservable<T> source, Func<T, int, b
return new WhereObservable<T>(source, predicate);
}

public static IObservable<T> WhereNotNull<T>(this IObservable<T> observable) where T : class
{
return observable.Where(v => v != null);
}

public static IObservable<T> WhereNull<T>(this IObservable<T> observable) where T : class
{
return observable.Where(v => v == null);
}

/// <summary>
/// Lightweight SelectMany for Single Async Operation.
/// </summary>
Expand Down Expand Up @@ -289,4 +297,4 @@ public static IObservable<Unit> ForEachAsync<T>(this IObservable<T> source, Acti
return new ForEachAsyncObservable<T>(source, onNext);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,6 @@ namespace UniRx
{
public static partial class ObserveExtensions
{
public static IDisposable Subscribe<T>(this IObservable<T> observable, IReactiveProperty<T> reactiveProperty) {
return observable.SubscribeWithState(reactiveProperty, (value, prop) => prop.Value = value);
}

public static IObservable<T> Do<T>(this IObservable<T> observable, IReactiveProperty<T> reactiveProperty) {
return observable.Do(value => reactiveProperty.Value = value);
}

public static IDisposable Subscribe<T>(this IObservable<T> observable, IReactiveCommand<T> reactiveCommand) {
return observable.SubscribeWithState(reactiveCommand, (value, cmd) => cmd.Execute(value));
}

public static IDisposable Subscribe<T>(this IObservable<T> observable, IReactiveCommand<Unit> reactiveCommand) {
return observable.SubscribeWithState(reactiveCommand, (_, cmd) => cmd.Execute(Unit.Default));
}

public static IObservable<T> Do<T>(this IObservable<T> observable, IReactiveCommand<T> reactiveCommand) {
return observable.Do(value => reactiveCommand.Execute(value));
}

public static IObservable<T> Do<T>(this IObservable<T> observable, IReactiveCommand<Unit> reactiveCommand) {
return observable.Do(value => reactiveCommand.Execute(Unit.Default));
}

public static IObservable<T> WhereNotNull<T>(this IObservable<T> observable) where T : class {
return observable.Where(v => v != null);
}

public static IObservable<T> WhereNull<T>(this IObservable<T> observable) where T : class {
return observable.Where(v => v == null);
}

/// <summary>
/// Publish target property when value is changed. If source is destroyed/destructed, publish OnCompleted.
/// </summary>
Expand Down Expand Up @@ -293,4 +261,4 @@ static ObservableDestroyTrigger GetOrAddDestroyTrigger(UnityEngine.GameObject go
return dt;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,33 @@ public static ReactiveCommand ToReactiveCommand(this IObservable<bool> canExecut
}

/// <summary>
/// Create parametered comamnds. CanExecute is changed from canExecute sequence.
/// Create parametered commands. CanExecute is changed from canExecute sequence.
/// </summary>
public static ReactiveCommand<T> ToReactiveCommand<T>(this IObservable<bool> canExecuteSource, bool initialValue = true)
{
return new ReactiveCommand<T>(canExecuteSource, initialValue);
}

public static IDisposable Subscribe<T>(this IObservable<T> observable, IReactiveCommand<T> reactiveCommand)
{
return observable.SubscribeWithState(reactiveCommand, (value, cmd) => cmd.Execute(value));
}

public static IDisposable Subscribe<T>(this IObservable<T> observable, IReactiveCommand<Unit> reactiveCommand)
{
return observable.SubscribeWithState(reactiveCommand, (_, cmd) => cmd.Execute(Unit.Default));
}

public static IObservable<T> Do<T>(this IObservable<T> observable, IReactiveCommand<T> reactiveCommand)
{
return observable.Do(value => reactiveCommand.Execute(value));
}

public static IObservable<T> Do<T>(this IObservable<T> observable, IReactiveCommand<Unit> reactiveCommand)
{
return observable.Do(value => reactiveCommand.Execute(Unit.Default));
}

#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))

static readonly Action<object> Callback = CancelCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,16 @@ public static IObservable<T> SkipLatestValueOnSubscribe<T>(this IReadOnlyReactiv
return source.HasValue ? source.Skip(1) : source;
}

public static IDisposable Subscribe<T>(this IObservable<T> observable, IReactiveProperty<T> reactiveProperty)
{
return observable.SubscribeWithState(reactiveProperty, (value, prop) => prop.Value = value);
}

public static IObservable<T> Do<T>(this IObservable<T> observable, IReactiveProperty<T> reactiveProperty)
{
return observable.Do(value => reactiveProperty.Value = value);
}

// for multiple toggle or etc..

/// <summary>
Expand Down Expand Up @@ -607,4 +617,4 @@ public static IObservable<bool> CombineLatestValuesAreAllFalse(this IEnumerable<
});
}
}
}
}

0 comments on commit 756506e

Please sign in to comment.