Skip to content

Instantly share code, notes, and snippets.

@mob-sakai
Created August 1, 2017 01:46
Show Gist options
  • Save mob-sakai/7e11fb7a02f524a51fa9c6010f9b9981 to your computer and use it in GitHub Desktop.
Save mob-sakai/7e11fb7a02f524a51fa9c6010f9b9981 to your computer and use it in GitHub Desktop.
ReactiveCollection拡張メソッド.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniRx;
using System.Linq;
using System;
/// <summary>
/// ReactiveCollection拡張メソッド.
/// </summary>
public static class ReactiveCollectionExtensionsEx
{
/// <summary>
/// 変更があったことを通知するObservable.
/// </summary>
public static IObservable<Unit> ObserveAnyChanged<T> (this ReactiveCollection<T> self)
{
return self.ObserveAdd ()
.AsUnitObservable ()
.Merge (
self.ObserveCountChanged ().AsUnitObservable (),
self.ObserveMove ().AsUnitObservable (),
self.ObserveRemove ().AsUnitObservable (),
self.ObserveReplace ().AsUnitObservable (),
self.ObserveReset ().AsUnitObservable ()
)
.ThrottleFrame (1, FrameCountType.EndOfFrame);
}
/// <summary>
/// 中身だけを全て入れ替える.
/// </summary>
public static ReactiveCollection<T> Set<T> (this ReactiveCollection<T> self, IList<T> source)
{
int before = self.Count;
int after = source.Count;
int minCount = before < after ? before : after;
//Replace.
for (int i = 0; i < minCount; i++) {
self [i] = source [i];
}
//Add.
for (int i = before; i < after; i++) {
self.Add (source [i]);
}
//Remove.
for (int i = before - 1; after <= i; i--) {
self.RemoveAt (i);
}
return self;
}
/// <summary>
/// 条件マッチするインデックスを取得する.
/// </summary>
public static int FindIndex<T> (this ReactiveCollection<T> self, Predicate<T> match)
{
for (int i = 0; i < self.Count; i++)
{
if (match(self[i]))
return i;
}
return -1;
}
}
/// <summary>
/// ReactiveDictionary拡張メソッド.
/// </summary>
public static class ReactiveDictionaryExtensionsEx
{
/// <summary>
/// 変更があったことを通知するObservable.
/// </summary>
public static IObservable<Unit> ObserveAnyChanged<TKey,TValue> (this ReactiveDictionary<TKey,TValue> self)
{
return self.ObserveAdd ()
.AsUnitObservable ()
.Merge (
self.ObserveCountChanged ().AsUnitObservable (),
self.ObserveRemove ().AsUnitObservable (),
self.ObserveReplace ().AsUnitObservable (),
self.ObserveReset ().AsUnitObservable ()
)
.ThrottleFrame (1);
}
/// <summary>
/// 中身だけを全て入れ替える.
/// </summary>
public static void Set<TKey,TValue> (this ReactiveDictionary<TKey,TValue> self, IEnumerable<TValue> source, System.Func<TValue,TKey> selector)
{
HashSet<TKey> activeKeys = new HashSet<TKey> ();
foreach (TValue value in source) {
TKey key = selector (value);
activeKeys.Add (key);
self [key] = value;
}
//アクティブリストに入っていないアイテムを除外.
TKey[] removeKeys = self.Keys
.Where (key => !activeKeys.Contains (key))
.ToArray ();
foreach (var key in removeKeys) {
self.Remove (key);
}
}
/// <summary>
/// 中身だけを全て入れ替える.
/// </summary>
public static void Set<TKey, TValue, TRValue> (this ReactiveDictionary<TKey,TRValue> self, IEnumerable<TValue> source, System.Func<TValue,TKey> selector)
where TRValue : IReactiveProperty<TValue>, new()
{
HashSet<TKey> activeKeys = new HashSet<TKey> ();
foreach (TValue value in source) {
TKey key = selector (value);
activeKeys.Add (key);
//valueが存在しない場合、新しく生成.
if (!self.ContainsKey (key))
self [key] = new TRValue ();
self [key].Value = value;
self [key] = self [key];
}
//アクティブリストに入っていないアイテムを除外.
TKey[] removeKeys = self.Keys
.Where (key => !activeKeys.Contains (key))
.ToArray ();
foreach (var key in removeKeys) {
self.Remove (key);
}
}
}
/// <summary>
/// ReactiveCollection拡張メソッド.
/// </summary>
public static class IDisposableeEx
{
/// <summary>
/// 安全なDisposeを実行します.
/// </summary>
public static void SafeDispose (this IDisposable self)
{
if(self != null)
self.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment