Example #1
0
        /// <summary>
        /// Roll back the versions of the last changed tiles
        /// </summary>
        public void Undo()
        {
            // for multi-undo, we could store prev. changed tiles set in the metadata store, or stack up change sets in ram.

            var changed     = _lastChangedTiles.ToArray();
            var changesMade = false;

            foreach (var tile in changed)
            {
                // if tiles exist for version-1:
                // - roll the version back in meta
                // - reload tile (purge from cache and flip the reload trigger)
                var path = tile.ToString();
                var node = _tileStore.Exists(path);
                if (node.IsFailure)
                {
                    continue;
                }
                if (node.ResultData == null)
                {
                    continue;
                }

                var prevVersion = node.ResultData.CurrentVersion - 1;

                if (prevVersion < 1)
                {
                    // undoing the first stroke. Mark it deleted.
                    var deadNode = new StorageNode {
                        CurrentVersion = node.ResultData.CurrentVersion, Id = node.ResultData.Id, IsDeleted = true
                    };
                    _tileStore.UpdateNode(path, deadNode);
                    changesMade = true;
                }
                else
                {
                    var ok = _tileStore.Read(path, "img", prevVersion);
                    if (ok.IsFailure)
                    {
                        continue;
                    }

                    var newNode = new StorageNode {
                        CurrentVersion = prevVersion, Id = node.ResultData.Id, IsDeleted = false
                    };
                    _tileStore.UpdateNode(path, newNode);
                    changesMade = true;
                }
            }

            if (changesMade)
            {
                ResetCache();
            }

            // Reset the last update set
            // For multi-undo, we should roll back to a previous undo set.
            _lastChangedTiles.Clear();
        }