Implement IDisposable for stack history

This commit is contained in:
Wiesław Šoltés 2017-01-06 16:55:39 +01:00
Родитель 0fb1f63725
Коммит c5c98d0974
2 изменённых файлов: 41 добавлений и 2 удалений

Просмотреть файл

@ -10,7 +10,7 @@ namespace ReactiveHistory
/// <summary>
/// Undo/redo stack based action history.
/// </summary>
public class StackHistory : IHistory
public class StackHistory : IHistory, IDisposable
{
private readonly Subject<bool> _canUndo;
private readonly Subject<bool> _canRedo;
@ -136,5 +136,17 @@ namespace ReactiveHistory
_canRedo.OnNext(false);
_canClear.OnNext(false);
}
/// <inheritdoc/>
public void Dispose()
{
Undos.Clear();
Redos.Clear();
Undos = null;
Redos = null;
_canUndo.Dispose();
_canRedo.Dispose();
_canClear.Dispose();
}
}
}

Просмотреть файл

@ -1,5 +1,6 @@
// Copyright (c) Wiesław Šoltés. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Linq;
using Xunit;
@ -26,6 +27,32 @@ namespace ReactiveHistory.UnitTests
}
}
[Fact]
[Trait("ReactiveHistory", "StackHistory")]
public void Dispose_Should_Release_Allocated_Resources()
{
var target = new StackHistory();
using (var helper = new HistoryHelper(target))
{
target.Snapshot(() => { }, () => { });
target.Snapshot(() => { }, () => { });
var result = target.Undo();
Assert.Equal(1, target.Undos.Count);
Assert.Equal(1, target.Redos.Count);
Assert.Equal(true, result);
target.Dispose();
Assert.Null(target.Undos);
Assert.Null(target.Redos);
Assert.Throws(typeof(ObjectDisposedException), () => target.CanUndo.Subscribe(_ => { }));
Assert.Throws(typeof(ObjectDisposedException), () => target.CanRedo.Subscribe(_ => { }));
Assert.Throws(typeof(ObjectDisposedException), () => target.CanClear.Subscribe(_ => { }));
}
}
[Fact]
[Trait("ReactiveHistory", "StackHistory")]
public void First_Snapshot_Should_Push_One_Undo_State()
@ -167,7 +194,7 @@ namespace ReactiveHistory.UnitTests
var target = new StackHistory();
target.Snapshot(
undo: () => Assert.True(target.IsPaused),
undo: () => Assert.True(target.IsPaused),
redo: () => Assert.True(target.IsPaused));
Assert.False(target.IsPaused);