Esempio n. 1
0
        /// <summary>
        /// Apply AutoPersistence to all objects in a collection. Items that are
        /// no longer in the collection won't be persisted anymore.
        /// </summary>
        /// <param name="doPersist">The asynchronous method to call to save the
        /// object to disk.</param>
        /// <param name="manualSaveSignal">When invoked, the object will be saved
        /// regardless of whether it has changed.</param>
        /// <param name="interval">The interval to save the object on. Note that
        /// if an object is constantly changing, it is possible that it will never
        /// be saved.</param>
        /// <returns>A Disposable to disable automatic persistence.</returns>
        public static IDisposable AutoPersistCollection <T, TDontCare>(this IReactiveCollection <T> This, Func <T, IObservable <Unit> > doPersist, IObservable <TDontCare> manualSaveSignal, TimeSpan?interval = null)
            where T : IReactiveObject
        {
            var disposerList = new Dictionary <T, IDisposable>();

            var disp = This.ActOnEveryObject(
                x => {
                if (disposerList.ContainsKey(x))
                {
                    return;
                }
                disposerList[x] = x.AutoPersist(doPersist, manualSaveSignal, interval);
            },
                x => {
                disposerList[x].Dispose();
                disposerList.Remove(x);
            });

            return(Disposable.Create(() => {
                disp.Dispose();
                disposerList.Values.ForEach(x => x.Dispose());
            }));
        }