reactiveui/ReactiveUI — pull request #268 by xpaulbettsx (2013-04-29 19:50:01+00:00)
Title: Auto-persistence helper

## What is This

This PR adds functionality to act on an object on an interval when it stops changing, usually to persist it to storage. The definition of "Persist to storage" is left entirely up to the client. So for example:

```cs
userSettings.AutoPersist(x => {
return Observable.Start(() => File.WriteAllText("settings.json", x.ToJson(), RxApp.TaskPoolScheduler);
});
```

The object `userSettings` above must implement `IReactiveNotifyPropertyChanged` (i.e. be a ReactiveObject), and must use DataContracts.

## Also for collections

This PR also adds this support for collections:

```cs
allTheTweets.AutoPersist(x => {
return Observable.Start(() => File.WriteAllText(x.Id + ".json", x.ToJson(), RxApp.TaskPoolScheduler);
});
```

Each `Tweet` object in `allTheTweets` will now persist its own data, until it is removed from the `allTheTweets` collection.

## This and That

This PR also includes a pretty handy method, `ActOnEveryItem`, which lets you run a method on every object either *entering* or *exiting* the collection:

```cs
someCollection.ActOnEveryObject(
x => Console.WriteLine("{0} added!", x),
x => Console.WriteLine("{0} removed!", x));
```

This method handles all of the edge cases of ReactiveCollection for you, such as:

* Replaces
* Resets
* Initialization (i.e. making sure to call `onAdd` when you initially call `ActOnEveryItem`)
* Destruction (i.e. on disconnecting `ActOnEveryItem`, `onRemove` runs on every object)