0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ReactiveCollectionの全変更を一括監視する拡張メソッド

Last updated at Posted at 2023-08-02

ReactiveCollectionを使ってると、
「うーん今回はObserveAddとObserveRemoveだけでいいかな?」
「それともObserveRemoveも入れたほうがいいかな?」
みたいなことで毎回ビミョーに悩んだりして無駄な思考を要してしまうので、
「いっそのこと最初から全部監視すればいいじゃん」
と思い立って作りました。

一応、大量のObserveが発動するのでその点ご留意ください(無視できるレベルだと思いますが)

	public static class UniRxExtension
	{
		/// <summary>
		/// ReactiveCollectionのあらゆる変更を監視して、1フレームに1回のみ通知する
		/// </summary>
		public static IObservable<Unit> ObserveAll<T>(
			this IReadOnlyReactiveCollection<T> source)
		{
			return Observable.Merge(
					source.ObserveCountChanged().AsUnitObservable(),
					source.ObserveAdd().AsUnitObservable(),
					source.ObserveRemove().AsUnitObservable(),
					source.ObserveReplace().AsUnitObservable(),
					source.ObserveMove().AsUnitObservable(),
					source.ObserveReset().AsUnitObservable())
				.BatchFrame();
		}
	}

↓ 使い方。返り値がIObservable<Unit>であることにご注意ください。

SomeReactiveColelctionInstance
	.ObserveAll()
	.Subscribe(_ => SomeMethod())
	.AddTo(this);
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?