protected static TType GetOpenCollection <TType> (string fileName)
            where TType : PersistentCollection <T, TCollection>
        {
            if (OpenCollections.TryOpen(fileName, out var collection))
            {
                if (collection is TType result)
                {
                    return(result);
                }
                throw new InvalidCastException(
                          $"{fileName} already has an open collection of type {collection.GetType ( )}");
            }

            return(null);
        }
        protected PersistentCollection(string fileName,
                                       JsonSerializerSettings serializerSettings,
                                       TimeSpan flushInterval)
        {
            FileName           = fileName;
            SerializerSettings = serializerSettings;
            FlushInterval      = flushInterval;

            OpenCollections.Add(this);

            if (!Load( ))
            {
                Collection = new TCollection( );
                Save( );
            }

            disposable = Observable.Interval(FlushInterval).Subscribe(l => ForceSave( ));
        }
 public void Dispose( )
 {
     ForceSave( );
     OpenCollections.Remove(FileName);
     disposable?.Dispose( );
 }