R3MotionExtensions.cs
Runtime/Integrations/R3/R3MotionExtensions.cs
1 using System;
2 using R3;
3
4 namespace MeatMotion
5 {
6 public static class R3MotionExtensions
7 {
8 public static Observable<float> ToObservable(this FloatMotionBuilder builder)
9 => builder.AsMotionBuilder().ToObservable();
10
11 public static Observable<TValue> ToObservable<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder)
12 where TValue : unmanaged where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
13 {
14 var subject = new Subject<TValue>();
15 void Finish() => subject.OnCompleted();
16 builder.WithOnCancel(Finish).WithOnComplete(Finish)
17 .Bind(subject, static (value, stream) => stream.OnNext(value));
18 return subject;
19 }
20
21 public static MotionHandle BindToReactiveProperty(this FloatMotionBuilder builder, ReactiveProperty<float> reactiveProperty)
22 => builder.AsMotionBuilder().BindToReactiveProperty(reactiveProperty);
23
24 public static MotionHandle BindToReactiveProperty<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder, ReactiveProperty<TValue> reactiveProperty)
25 where TValue : unmanaged where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
26 => builder.Bind(reactiveProperty ?? throw new ArgumentNullException(nameof(reactiveProperty)), static (v, property) => property.Value = v);
27
28 public static MotionHandle AddTo(this MotionHandle handle, CompositeDisposable disposable, DisposeBehavior disposeBehavior = DisposeBehavior.Cancel)
29 {
30 disposable.Add(handle.ToDisposable(disposeBehavior));
31 return handle;
32 }
33
34 public static MotionHandle AddTo(this MotionHandle handle, SerialDisposable disposable, DisposeBehavior disposeBehavior = DisposeBehavior.Cancel)
35 {
36 disposable.Disposable = handle.ToDisposable(disposeBehavior);
37 return handle;
38 }
39
40 public static MotionHandle AddTo(this MotionHandle handle, ref DisposableBag disposable, DisposeBehavior disposeBehavior = DisposeBehavior.Cancel)
41 {
42 disposable.Add(handle.ToDisposable(disposeBehavior));
43 return handle;
44 }
45
46 public static MotionHandle AddTo(this MotionHandle handle, ref DisposableBuilder disposable, DisposeBehavior disposeBehavior = DisposeBehavior.Cancel)
47 {
48 disposable.Add(handle.ToDisposable(disposeBehavior));
49 return handle;
50 }
51 }
52 }